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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Socket and threads" Watch "Socket and threads" New topic

Socket and threads

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

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
jQuery in Action, 3rd edition
subject: Socket and threads
It's not a secret anymore!