unblocked UDP reading

This is a discussion on unblocked UDP reading within the Linux Programming forums, part of the Platform Specific Boards category; Does anyone know any other way or reading UDP packets other then using the recvfrom() function. Execution stops at that ...

  1. #1
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195

    unblocked UDP reading

    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.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    Use select() to determine if there is anything available to read, before calling recv()

  3. #3
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    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 04:25 PM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    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

  5. #5
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    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?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    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?

  7. #7
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    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'?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with UDP
    By AlexS in forum C# Programming
    Replies: 0
    Last Post: 07-07-2009, 06:21 PM
  2. UDP socket partial reading
    By mynickmynick in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-25-2009, 11:43 AM
  3. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  4. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 08:53 AM
  5. Traceroute using UDP and ICMP
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-09-2002, 07:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21