Networking Forums

Networking Forums > Computer Networking > Linux Networking > Linux Socket Problem?

Reply
Thread Tools Display Modes

Linux Socket Problem?

 
 
LaBird
Guest
Posts: n/a

 
      02-19-2004, 07:05 AM
Dear all,

I am writing a program for communcating data among
4 machines. I use UDP/IP, and for each pair of machines
I use 4 different sockets: one for sending msg, one for
recving msg, the other two for sending and recving acks.
Only the socket for recving msg is set to be asynchronous
(by fcntl), others are blocking. To avoid message
interrupts, I implement two macros called BEGINCS and
ENDCS, for blocking and unblocking SIGIO and SIGALRM
signals resp. (the only signals occurring should be these
two, and also the SIGSEGV, which will be captured in
another part of my program).

Here is the C++ code for getting an ack after a process
sends out the message (some declarations omitted):

int arrived = 0;
while (arrived != 1) {
BEGINCS;
FD_ZERO(&readfds);
FD_SET(ackrfd[toproc], &readfds);
polltime.tv_sec = 0;
polltime.tv_usec = 0;
res = select(maxackrfd, &readfds, NULL, NULL, &polltime);
if (res == -1) {
printf("Error in select(), exiting\n");
exit(1);
}
if (FD_ISSET(ackrfd[toproc], &readfds) != 0)
arrived = 1;
ENDCS;
}
if (arrived == 1) {
recv_again:
BEGINCS;
s = sizeof(from);
// The ack is always 4 bytes
res = recvfrom(ackrfd[toproc], (char *)&rep, 4, 0,
(struct sockaddr *)&from, (socklen_t *)&s);
if ((res == -1) && (errno == EINTR)) {
printf("Interrupted when recv, try again\n");
goto recv_again;
}
if (res > -1) {
success = 1;
}
else {
printf("send problem\n");
}
}

The problem is that, I found the processes sometimes
get waiting forever at the recvfrom() call. It seems that
the select() has reported that the socket has a message
received, but it is not the case when attempting to get it.

I am using Linux Redhat 7.3 (kernel 2.4.18-3). Do I
miss something in the program, or is there any bug in
Linux sockets causing this problem?

Any suggestion here is welcomed. Thanks!

--
LaBird (Benny).
Email: Pls remove all nos. from my above email addr.


 
Reply With Quote
 
 
 
 
Phil Frisbie, Jr.
Guest
Posts: n/a

 
      02-20-2004, 05:17 PM
LaBird wrote:

> Dear all,
>
> I am writing a program for communcating data among
> 4 machines. I use UDP/IP, and for each pair of machines
> I use 4 different sockets: one for sending msg, one for
> recving msg, the other two for sending and recving acks.
> Only the socket for recving msg is set to be asynchronous
> (by fcntl), others are blocking. To avoid message
> interrupts, I implement two macros called BEGINCS and
> ENDCS, for blocking and unblocking SIGIO and SIGALRM
> signals resp. (the only signals occurring should be these
> two, and also the SIGSEGV, which will be captured in
> another part of my program).


select() should always be used with non-blocking sockets. That is a general
sockets programming rule.

--
Phil Frisbie, Jr.
Hawk Software
http://www.hawksoft.com

 
Reply With Quote
 
LaBird
Guest
Posts: n/a

 
      02-23-2004, 07:04 AM
Dear Phil,

Thanks. I modified this code from an open-source
program segment. I believe the reason it is
written this way (but not just calling recv or recvfrom
without select) is to give a chance for the program
to get other signals and serve them between select()
(when some messages have arrived) and recvfrom()
(to get the actual message). If we just have written
recvfrom() without select(), but using BEGINCS (which
blocks other signals) before recvfrom() to prevent
message from being interrupted, there is no chance
for the signals from being served. This is just my
understanding on why the program is written that way.

After prolonged debugging efforts, I've just found out
the problem of the code. The error is due to a wrong
value of maxackrfd being passed in select().

--
LaBird (Benny).
Email: Pls remove all nos. from my above email addr.


"Phil Frisbie, Jr." <(E-Mail Removed)> wrote in message
news:SasZb.2472$_(E-Mail Removed)...
> select() should always be used with non-blocking sockets. That is a

general
> sockets programming rule.
>
> --
> Phil Frisbie, Jr.
> Hawk Software
> http://www.hawksoft.com
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
linux c socket read html problem step Linux Networking 2 06-13-2007 11:59 AM
Does Linux and embedded Linux provides RAW socket? GS Linux Networking 3 04-14-2007 05:26 AM
linux socket programming and HTTP Protocol Problem PGHULME Linux Networking 1 08-21-2006 09:35 PM
Linux (RH8) Socket Timeouts Otto Blomqvist Linux Networking 1 09-20-2004 01:37 PM
Disabling a Linux TCP/IP Socket B.Ravi Kumar Linux Networking 1 06-10-2004 07:01 PM



1 2 3 4 5 6 7 8 9 10 11