milind_db
milind_db

Reputation: 1294

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?

Upvotes: 1

Views: 5751

Answers (2)

Tim Lamballais
Tim Lamballais

Reputation: 1054

You need to split off threads when accepting your socket connections server side. This is very easily done with serversocket. A very rudimentary (untested!) implementation:

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

class Server {

    private ServerSocket socket;

    public Server() {
        try {
            this.socket = new ServerSocket(6789);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void go() throws IOException {
        while(true) {
            Socket sock = socket.accept();
            new Thread(new ClientSession(sock)).start();
        }
    }

    public static void main(String[] args) {
        Server server = new Server();
        try {
            server.go();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    class ClientSession implements Runnable {

        private final Socket clientsocket;

        ClientSession(Socket sock) {
            this.clientsocket = sock;
        }

        @Override
        public void run() {
            //do stuff, like read from socket.
        }

    }

}

Note that you don't need to change the port at all.

Upvotes: 2

Jordan Denison
Jordan Denison

Reputation: 2727

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?

Upvotes: 4

Related Questions