Search code examples
javasocketsclient-serverserversocket

How to make communication between one server and multiple client in Java Socket Programming?


I have two java applications, one is web app and another is simple java app, So I am using Socket programming for communication between them.

I made one SocketServer which is a Thread, in which I created ServerSocket serverSocket = new ServerSocket(6789) And in my web app I created Socket client = new Socket("localhost", 6789); My server sends some data to client and client will start some other work, but if I want to run another client i.e. server will send different parameters and client have to start processing what should I do?

Because server is already started on '6789' port and first client also with the same port. How can I start client with another port?

Every time Server must have to started first and then client.

I think client will not found server till both are having same ports.

Am I have to create another server instance with different port and then invoke client??? But How can my client will know on which port server is started?

For Example:

Imagine I have UI like:

start MIlind

start xyz

start abc

and click on strart it will call client and start process, If an start Milind first then How will I start xyz? because 'start Milind' started client and server at port 6789, How will other start process works?


Solution

  • It seems like a lot of overhead to create a server/client app just for a web app to communicate with a local java program (and even more so to duplicate this process to do more than one thing at a time). If you are looking for concurrent processing in the background of a web app, you can always just create a thread (or multiple threads) to do the work. Or is there a reason why the simple java app can't be embedded in the web app?