Skip to content
Advertisement

TCP threaded server/client

I’m trying to make a threaded TCP server that accept multiple client. It’s only accepting one client. Any idea on how to make the server accepting multiple client?

Here is what I tried so far:

I changed the server code. I ran both the server and the client, but it seems that it’s only one thread is working. Should I change the ports or something?

Server code:

JavaScript

}

And here is my Client code:

JavaScript

Advertisement

Answer

Right after you do this

connectionSocket = MySocket.accept();

you need to pass this connectionSocket instance to a new Thread and call start on that Thread.

Also, move all the connectionSocket processing logic to your Thread class’s run method
(this logic is currently in the main method after this line mentioned above).

For implementing your own Thread, you can either extend Thread or implement Runnable.

Check this link for some more details: Java Threads

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement