LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-17-2008, 03:52 AM   #1
montylee
Member
 
Registered: May 2003
Location: India
Distribution: Ubuntu 7.04, Fedora Core 9
Posts: 168

Rep: Reputation: 30
Question 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?
 
Old 08-17-2008, 07:41 AM   #2
dorian33
Member
 
Registered: Jan 2003
Location: Poland, Warsaw
Distribution: LFS, Gentoo
Posts: 591

Rep: Reputation: 32
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.
 
Old 08-17-2008, 06:30 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 08-18-2008, 04:45 AM   #4
montylee
Member
 
Registered: May 2003
Location: India
Distribution: Ubuntu 7.04, Fedora Core 9
Posts: 168

Original Poster
Rep: Reputation: 30
Talking

Quote:
Originally Posted by dorian33 View Post
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.

Thanks for the help!
 
  


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
Quake 4 Problems, other threads didnt help computergee Linux - Games 3 02-27-2006 05:40 PM
problems with synchronization of threads in updating a text buffer in gtk2.0 stephenwalter Programming 2 12-29-2005 11:31 PM
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
Problems creating a milisecond timeout timer for threads cardias Linux - Software 1 08-04-2004 03:10 PM
Audigy Problems (And yes, I've read all the other threads) gsibble Linux - Hardware 2 08-12-2003 03:13 PM

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

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