LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-17-2005, 02:48 AM   #1
kranti
LQ Newbie
 
Registered: Oct 2005
Location: Pune
Posts: 17

Rep: Reputation: 0
Why does a multithreaded program gets hanged at recvfrom()?


I have written a client-server application using multithreading.

Initially I'm creating a thread to receive client requests in server. But only if the queue has registered clients, the code inside the thread will get executed.

Hence for the FIRST client-request,
I'm sending a buffer containing some pre-defined request to the server thru' client. But at server side, the program gets hanged at the first recvfrom() function call after the thread creation, in main() while at client side I'm able to perform recvfrom() and sendto() although there is problem at server side.

The flow goes like this when I try to debug....

Client: sendto() OK
Server: recvfrom() control gets stuck-up at this point

Server: sendto() ---
Client: recvfrom() OK

Client: sendto() OK
Server: recvfrom() ---

Server: sendto() --- this is inside another thread
Client: recvfrom() hang

How can the client communicate well if server is getting blocked?


Thanks,

Regards,
Kranti
 
Old 10-17-2005, 10:44 AM   #2
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
I assume you are using UDP since you use sendto/recvfrom. Are you sure that the sockets doing the receiving have been opened at the time that the side doing the sending is doing the sendto? For instance, in your last case there, does the client do a sendto then immediately open a new socket in that new thread to try and recvfrom on? And does the server immediately sendto that new socket? If so, you probably have a race condition going on. Could something like this be going on?

1. Client sends packet to server
2. Server receives packet
3. Server sends new packet to new socket on client
4. Client opens new socket and waits for server to sendto it. (Wait a minute, the server has already sent it's packet and since there wasn't a socket here to receive it yet, it was simply lost. We'll be waiting a long time for a packet unless the server is smart and resends it.)

A successful call to sendto just means that the packet was sent from the perspective of the socket doing the sending. UDP does not guarantee that it was actually delivered. Are your sockets all communicating on the same local network, (or better yet, the same machine)? Because UDP is unreliable, you could just be losing packets. It's not as likely if you are just testing on a local network, but still a possibility. If you cannot afford to lose packets, you will need to either code in some sort of redundancy, some sort of ACK system, or switch to using TCP stream based sockets.

However, if you are seeing this frequently enough and always in the same spot, UDP reliability (or lack thereof) probably isn't the problem. Most likely you have to just restructure the program somehow. Without seeing code or more details as to the layout of your program it's hard to say what else the problem might be, though.

Last edited by deiussum; 10-17-2005 at 10:52 AM.
 
  


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
Why does a multithreaded program gets hanged at recvfrom()? kranti Linux - Newbie 1 10-17-2005 04:40 AM
Why does a multithreaded program gets hanged at recvfrom()? kranti Linux - General 1 10-17-2005 04:09 AM
Probelm regarding multithreaded program? geethu Programming 2 08-10-2005 12:39 AM
Debugging Multithreaded Program villie Programming 2 08-17-2004 11:24 AM
Signal handling in Multithreaded program souvik Programming 4 08-23-2002 07:28 AM

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

All times are GMT -5. The time now is 12:28 AM.

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