0

How can a Server open a TCP connection through a Client which was connected to server before? Let say we have simple server like the code shows below; server waits for client connections and a handler thread serves connected clients. Client as below, every time wants to do an operation with the server, reestablish the connection, communicate with server and then close the connection. So, if server would like to open a TCP connection through the client(when there is no an active connection from client to server), how it can be done?

Server Code

public class DServer {
    public static void main(String[] args) {
        ServerSocket serverSocket = new ServerSocket(7800);
        while(true) {
            Socket socket = serverSocket.accept();
            System.out.println("Server established a new connection");
            HandleConnection handler = new HandleConnection(socket);
            handler.start();
        }
    }
}

Client Code

public class DClient{
    public static void main(String[] args) {
        Socket socket = new Socket("localhost", 7800);
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
        ObjectInputStream in = new ObjectInputStream(socket.getInputStream());

        //for every operation you do, first reestablish the connection,
        //communicate with server, then close the connection.

    }
}

2 Answers 2

1

I think you have some fundamental misunderstandings about what a client is and what a server is in a networking sense.

The server is the application listening for connections. The client is the application creating connections to the server. If you want both of your applications to be able to initiate communication at will, then your applications will need to be both clients and servers.

So your "DClient" application will need to have a ServerSocket. You "DClient" application can then inform your "DServer" application of it's host and port for when "DServer" wants to create a connection (note that things like NATing can make this sort of thing problematic if both applications are not in the same network).

Alternately, depending on your use case, you could just leave the connection from client to server open. This is the more usual approach to letting the server push data.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the explanation. I didn't misunderstood client-server paradigm but i have a homework question which asks me to implement it that way. I tought to open a server socket on client as you said but it seemed ineffecient to me(when there are many clients) and wanted to understand if there is actually another way to do it.
0

There is solution to create proxy server which is manage the both request client and server which called middleware for both client request. In network programming it must required to fist server start and then client request because client server architecture.

1 Comment

but there is a java library already for that? I mean to create proxy server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.