LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-03-2010, 09:22 AM   #1
mhogue
LQ Newbie
 
Registered: Aug 2009
Posts: 9

Rep: Reputation: 0
Help with non blocking udp socket


Hi All,

Below is my code for a non blocking UDP socket, this code compiles fine with g++, however when I run it i get the error NON: recvfrom: Resource temporarily unavailable. Please help with any advice

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string>
#include <stdio.h>
#include <iostream>
#include <map>
#include <unistd.h>
#include <fcntl.h>

using namespace std;

int main(int argc, char *argv[])
{

struct sockaddr_in addr;
int sock, nbytes,addrlen, flags;
struct ip_mreq mreq;
char msgbuf[1024];

int yes = 1;


if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("socket");
exit(1);
}

flags = fcntl(sock,F_GETFL);
flags |= O_NONBLOCK;


// allow multiple sockets to use the same PORT number
if ( (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof( yes )) < 0))
{
perror("Reusing ADDR failed");
exit(1);
}

// setting up destination addres
memset(&addr, 0, sizeof( addr ));
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr(SOCK_GROUP);
addr.sin_port=htons(SOCK_PORT);

// bind to receive address
if (bind (sock, (struct sockaddr *) &addr, sizeof( addr )) < 0)
{
perror("bind");
exit(1);
}

//Join the multicast group
mreq.imr_multiaddr.s_addr=inet_addr(SOCK_GROUP);
mreq.imr_interface.s_addr=htonl(INADDR_ANY);
if (setsockopt (sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof( mreq )) < 0)
{
perror("setsockopt");
exit(1);
}


fcntl(sock, F_SETFL, flags);

// now just enter a read-print loop
while(1) {

addrlen=sizeof(addr);
if ((nbytes = recvfrom(sock, msgbuf, sizeof( msgbuf ), 0, (struct sockaddr *) &addr, (socklen_t*) &addrlen)) < 0)
{
perror("NON: recvfrom");
exit(1);

}//Closes the if
else
{
cout <<" got something "<<endl;
}
}
};
 
Old 02-03-2010, 12:02 PM   #2
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
Quote:
recvfrom: Resource temporarily unavailable
My guess is that there's nothing to read on the socket, and as you configure it as non-blocking, the "recvfrom" return with this error which isn't a real one...

2 remarks:
1°) Use "code" take to make your code more readable
2°) It looks like you're mixing C and C++, don't do that
 
Old 02-04-2010, 02:07 AM   #3
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
Since you are using non-blocking sockets, you would need to compare the errno against -EAGAIN to make sure that you can go back to sleep till there is something to receive. You can use select()/poll() to see if the socket is ready for data.

man 2 recvfrom

Last edited by shishir; 02-04-2010 at 02:09 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Non Blocking UDP Socket mhogue Programming 1 02-02-2010 05:39 PM
Blocking UDP packet sailwind Linux - Software 3 04-08-2009 04:07 AM
Poll() and UDP socket krizzz Programming 4 06-06-2006 10:55 AM
How to receive UDP and ICMP packets, by one UDP socket(PMTUD) myself_rajat Linux - Networking 0 05-28-2004 05:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration