LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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-02-2010, 11:26 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


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-02-2010, 05:39 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I'd ask the mods to move this to the Programming forum (use the Report button)
 
  


Reply



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
Blocking UDP packet sailwind Linux - Software 3 04-08-2009 04:07 AM
Semaphore for UDP socket vtstmn Linux - Networking 0 04-02-2009 02:00 AM
UDP Socket Connection citiz3n Linux - Networking 2 05-26-2008 07:48 PM
How to receive UDP and ICMP packets, by one UDP socket(PMTUD) myself_rajat Linux - Networking 0 05-28-2004 05:43 AM
Using Raw Socket for UDP myself_rajat Linux - Networking 0 05-17-2004 03:35 AM

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

All times are GMT -5. The time now is 09:42 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