wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes Socket and threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Socket and threads" Watch "Socket and threads" New topic
Author

Socket and threads

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi!
I have a problem. I have a program using socket server communication and it works fine as long as I only have one user at time.
The flow of the program is
Receiver waiting for messenger
Sender send message, and start to wait for a response message
Receiver get message, and send a response message, start waiting for a now message.
And so on�
To day I can only send one question at time (can only have one user) but I want to have many�
Is it possible to solve with threads? And in that case, does anybody have an example of hove to solve it.
Thanks!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Yes, you can solve this using threads. Basically, what you do looks something like this:

Of course, there are complications. But that's the basic idea.


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Your example is pretty much exactly what I'm doing right now. Creating and discarding a thread per request is easy, but not the most efficient. What's the next step up the ladder of complexity, scalability and robustness? Thread pooling?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Socket and threads
 
Similar Threads
Multiple threads writing and reading from single socket
Using command pattern with a pool of receivers
jms correlation id
Who's using threads?
Multiple Midi Channels Problem