Networking Forums

Networking Forums > Computer Networking > Linux Networking > 2.2 select() on UDP Socket doesn't work as expected

Reply
Thread Tools Display Modes

2.2 select() on UDP Socket doesn't work as expected

 
 
rs
Guest
Posts: n/a

 
      07-08-2003, 09:52 AM
I hope somebody can help me with this: I have a UDP socket on which I send
data and then do a select() wiating indefinetely until data is sent back.
An example program copied from a previous newsgroup article attached shows
the kind of thing I'm trying to do.

This program works great on 2.4, exactly how I wish for it to work, however
on the 2.2 kernel the readset fires immediately without blocking. The same
behaviour is exhibited on 2.0.

Unfortunately, this program will have to run on an embedded device where
upgrading the kernel is not an option. So, does anybody know what I'm
doing wrong, and how I can get it to work the way I want with 2.2?

Thanks

--------
Attached file: test_sock.c




#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int main(int argc, char **argv)
{
int fd = -1;
int dest_port = -1;
int ret = -1;
struct sockaddr_in myaddr;
int addr_size = sizeof(myaddr);
struct hostent *hostname = NULL;
fd_set readSet;
fd_set errSet;
struct timeval timeout;
char buf[512];

if (argc < 3)
{
printf("\nUsage: test hostname port\n\n");
exit(-1);
}

hostname = gethostbyname(argv[1]);
dest_port = atoi(argv[2]);

if (hostname == NULL)
{
printf("Could not resolv hostname\n");
exit(-1);
}

fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
printf("Could not open socket\n");
}

addr_size = sizeof(myaddr);
memset(&myaddr, 0, addr_size);

myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(dest_port);
memcpy(&myaddr.sin_addr, hostname->h_addr_list[0], hostname->h_length);

ret = sendto(fd, "*", 1, 0, (const struct sockaddr *)&myaddr,
sizeof(myaddr));
if (ret < 0)
printf("send returned ret=%d, errno=%d\n", ret, errno);

FD_ZERO(&readSet);
FD_SET(fd, &readSet);

FD_ZERO(&errSet);
FD_SET(fd, &errSet);



ret = select(fd + 1, &readSet, NULL, &errSet, NULL);
if (ret < 0)
printf("select returned ret=%d, errno=%d\n", ret, errno);

if ( FD_ISSET(fd, &readSet) )
printf("readSet fired\n");
if ( FD_ISSET(fd, &errSet) )
printf("errSet fired\n");

if ( (ret > 0) && (FD_ISSET(fd, &readSet)) && (!FD_ISSET(fd, &errSet)) )
{
ret = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&myaddr,
(socklen_t *)&addr_size);
if (ret < 0)
printf("recvfrom returned ret=%d, errno=%d\n", ret, errno);
else
printf("buff is %s\n", buf);
}

close(fd);
return 0;
}

 
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
wireless connection (w\authentication) doesn't work, while wiredconnection does, on select laptops ekimack@gmail.com Wireless Internet 0 03-15-2009 04:16 AM
select() returns and indicates writability before a non-blocking TCP socket has actually connected. jaswantv@nortelnetworks.com Linux Networking 0 10-25-2006 04:09 PM
select doesn't work c plusplus Linux Networking 1 09-27-2006 08:06 AM
dhcp client not work how expected on my Suse linux 8.1 dundo@libero.it Linux Networking 1 11-16-2005 03:25 PM
select with timeout hangs when socket is closed eran borovik Linux Networking 0 10-19-2004 03:54 PM



1 2 3 4 5 6 7 8 9 10 11