Search code examples
javasocketshttpserver

Connect httpServer to client through a specific socket in java


I have made an httpServer which is connected with the client with Socket client = socket.accept(). Is there a command in order to choose which port I want to use to send messages to the client??


Solution

  • You want to have a fixed outbound port on the client? You can't do that.

    See How is source port for HTTP determined? Is there ever collision in NAT?

    Why do you want to do that?

    An httpServer only responds on client requests. It cannot send data to the client without a request from the client. If you want to have bi-directional communication, you need websockets.