Reputation: 12207
My two clients have to communicate directly with each other. Both of them has ServerSocket
and Socket
, too. I can demonstrate it with this code snippet:
final ServerSocket serverSocket = new ServerSocket(12345);
new Thread(new Runnable() {
@Override
public void run() {
try {
Socket clientSocket = serverSocket.accept();
System.out.println(clientSocket.getInputStream().read());
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
Socket clientSocket = new Socket(InetAddress.getLocalHost(), 12345);
clientSocket.getOutputStream().write(1);
clientSocket.getOutputStream().flush();
It works OK, but i cannot test them, if they are on the same IP (for example on localhost), because the client will connect to itself. My question is, how can i test or rewrite this, to test two client connecting to each other on the same IP and port?
Upvotes: 0
Views: 112
Reputation: 321
Make the port number configurable via a command line argument, property file or some other way. For the client side you'll need to provide port number of the other instance.
Upvotes: 2