Thread: Can't seem to receive UDP packets

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    127

    Can't seem to receive UDP packets

    I'm trying to get a UDP server working and just can't seem to get it to read in any UDP packets. I can see the packet coming in on the correct interface and port from tcpdump, but my program never logs that it got the data.

    I just keep getting a log showing that my recvfrom timed out. I'm pretty sure I'm setting everything up correctly. Anyone have an idea as to what I'm missing?

    Code:
    int create_listen_socket()
    {
    	struct sockaddr_in si_me;
    	int sock_fd;
    	struct ifaddrs * ifAddrStruct=NULL;
    	struct ifaddrs * ifa=NULL;
    	void * tmpAddrPtr=NULL;
    	struct timeval tv;
    
    	memset((char *) &si_me, 0, sizeof(si_me));
    	if ((sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    	{
    		printf("Create socket error %d.\n", errno);
    		return -1;
    	}
    
    	tv.tv_sec = 5;
    	tv.tv_usec = 0;
    	if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
    	{
    		printf("setsockopt error %d.\n", errno);
    		return -1;
    	}
    
    	getifaddrs(&ifAddrStruct);
    	for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next)
    	{
    		if ((ifa->ifa_addr->sa_family == AF_INET) &&
    				(strcmp(ifa->ifa_name, "eth0") == 0))
    		{ // check it is IP4
    			// is a valid IP4 Address
    			si_me.sin_addr.s_addr = ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr.s_addr;
    
    			tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
    			char addressBuffer[INET_ADDRSTRLEN];
    			inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
    			printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
    			break;
    		}
    	}
    	if (ifAddrStruct != NULL)
    	{
    		freeifaddrs(ifAddrStruct);
    	}
    
    	si_me.sin_family = AF_INET;
    	si_me.sin_port = htons(LISTEN_PORT);
    	if (bind(sock_fd, (struct sockaddr*) &si_me, sizeof(si_me)) == -1)
    	{
    		printf("Bind error %d.\n", errno);
    		return -1;
    	}
    
    	return sock_fd;
    }
    
    void * listen_thread(void * args)
    {
    	char buf[BUFLEN];
    	struct sockaddr_in si_other;
    	int slen = sizeof(si_other);
    	int num_bytes;
    	int sock_fd = create_listen_socket();
    
    	while(sock_fd > 0 &&
    			running)
    	{
    		num_bytes = recvfrom(sock_fd, (void*) buf, BUFLEN, 0, (struct sockaddr*) &si_other, (socklen_t *) &slen);
    		if(num_bytes > 0)
    		{
    			printf("Got data.\n");
    		}
    		printf("Got %d bytes. errno %d\n", num_bytes, errno);
    	}
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    127
    Mods, close this thread if you can. I was running this on a fresh install of CentOS and forgot to turn my firewall off! I didn't realize tcpdump could pick up packets before the firewall.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Send/Receive
    By C_ntua in forum Networking/Device Communication
    Replies: 1
    Last Post: 01-19-2010, 12:17 PM
  2. TCP/IP packets
    By Zairus in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2006, 05:35 AM
  3. function to receive all
    By neandrake in forum Networking/Device Communication
    Replies: 19
    Last Post: 11-19-2003, 07:20 AM