first i am creating socket, with that socket descriptor i am sending some no. of bytes through sendto(....) function.
At the time of receiving through recvfrom(....) function, my code was hanging if the specified destination address is not available. Suppose if destination is available the it's working correctly... Why this is happening

Recommended Answers

All 5 Replies

In the recvfrom() function, use "MSG_DONTWAIT" flag in the 4th argument(which is the flag) and see whether it solves your problem.


By default, the method will wait until it gets a message from the mentioned address, and your program will proceed only after that.

Read the manual for recvfrom()
http://man-wiki.net/index.php/2:recvfrom


Hope that helps.....

even if i am keeping MSG_DONTWAIT flag, it showing resource temporarily unavailable, but actually resource is available

can you share the code segment for both the sender and receiver programs where you are making the communication, also mention which compiler you are using and which platform(windows or Linux)

IN UNIX,QT tool
first creating socket with----> socket(AF_INET,SOCK_DGRAM,0),
then sending by ----> sendto(socketid,buffer,150,0,(struct sockaddr *)&sock,sizeof(sock))

then receiving by--------> recvfrm(socketid,buffer,150,0,(struct sockaddr*)&sock,sizeof(sock))

In struct sockaddr i am giving destination IP address ,, IF destination is available then immediately its replying other wise waiting for data to receive from destination

http://www.linuxquestions.org/questions/programming-9/non-blocking-recvfrom-644930/

It seems you have to make the socket non-blocking(Read more about blocking/non-blocking socket). Follow the steps mentioned in the above link and check whether your job is done or not.

I guess you will have to use the MSG_DONTWAIT as well while calling recvfrm().
Read the behavior in the man page of recvfrm() when you set this flag.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.