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.
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.
Use select() to determine if there is anything available to read, before calling recv()
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.
Code: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); } } }
Last edited by Mastadex; 12-13-2004 at 05:25 PM.
Read up on the correct usage of Select() here. 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
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?
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?
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'?
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.