Problems with using same socket in different threads
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Problems with using same socket in different threads
I have server which listens to a port in the main thread. Once it receives something, it creates a new thread. I am able to send a message from the new thread to the same socket but i am not able to receive messages on the same socket in the new thread.
When i send a message from a client to the server, i am not able to get the message in the new thread when i call recvfrom(). I added recvfrom() to the main thread and i was able to get the messages. So, the messages can be read by the main thread but not by the newly created threads.
How can i allow the new threads to receive message on the same socket? Any ideas?
I am pretty sure you have to make a mistake in your program. Creating threads which handle the clients connections is typical solution. And there is no problem with socket communication.
Are you using stream or datagram sockets? I'm pretty sure recvfrom is for datagram sockets, so if you're using a stream socket (SOCK_STREAM) I don't think it will work.
How do you know that the send operations work? An error should show up as a (ssize_t) -1, which can't be detected with !, in case that's how you're checking.
ta0kira
I am pretty sure you have to make a mistake in your program. Creating threads which handle the clients connections is typical solution. And there is no problem with socket communication.
Ah, you are right. The problem was in my code. I was using recvfrom() in my main thread. So, when i added recvfrom() in the new thread, it failed to get any messages as all messages were already getting read by the recvfrom() in the main thread. Now, i have put a check in the main thread, so it is called only when my new thread is not executing.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.