PDA

View Full Version : unblocked UDP reading



Mastadex
12-13-2004, 04:30 PM
Does anyone know any other way or reading UDP packets other then using the recvfrom() function. Execution stops at that function until it gets an answer. I want to check to see if there is anything in the buffer first, before running that function.

Salem
12-13-2004, 04:33 PM
Use select() to determine if there is anything available to read, before calling recv()

Mastadex
12-13-2004, 05:16 PM
Here is what I have Thus far, which is a portion of my UDP reading code. It doesnt work for some reason, I dont know why.



int nread, n = 0;
int listenLen;
struct sockaddr_in listenFrom;
char listenMessage [41], listenMsg [80];
fd_set origFds;
struct timeval timeout;

/* reset and set the File descriptor */
FD_ZERO(&origFds);
FD_SET(recv_l, &origFds);

/* Set the time out for the recieve value */
timeout.tv_sec = 5;
timeout.tv_usec = 5;


////////////////// some more code ///////////////////

/* Reading of the File Descriptor */
if (select(FD_SETSIZE, &origFds, (fd_set *)0, (fd_set *)0, &timeout))
{
if (FD_ISSET(recv_l, &origFds))
{
/* get input from the File Descriptor */
ioctl(recv_l, FIONREAD, &nread);

/* recieved something */
if (nread)
{
listenLen = sizeof(listenFrom);

if ((n = recvfrom(recv_l, listenMessage, 80, 0, (struct sockaddr*) &listenFrom, &listenLen)) < 0)
{
print(3, 0, "Client Read Error, Exiting", COLOR_GREEN);
exitFlag = 1;
}

print(12, 0, listenMessage, COLOR_YELLOW);
}
}
}

bithub
12-14-2004, 12:09 AM
Read up on the correct usage of Select() here (http://www.rt.com/man/select.2.html). The first parameter passed to select should be the highest file descripter you are watching plus one. You are instead passing the maximum value of any file descriptor that select can handle. In your case, the correct first parameter would be recv_l+1

Mastadex
12-14-2004, 04:51 PM
I have tracked down the problem to the ioctl line. It only returns the right value from the network when i press ENTER. else it will keep returning zero. Any suggestions?

Salem
12-14-2004, 05:44 PM
Perhaps it isn't being sent before you press enter.
Without also seeing the transmit code, it's hard to tell.

Have you got ethereal to spy on the actual network traffic?

Mastadex
12-15-2004, 09:41 AM
well when i send a bunch of characters through sendto(), its a null terminated character array. should i leave it as that or do i have to end it with a '\n'?

Salem
12-15-2004, 09:55 AM
send()/recv() are binary functions - it's only for the end points to care about what terminates a line. So long as they agree, it shouldn't matter.