I have a problem with the socket in java: I have a server on the desktop PC that I've a ServerSocket object Instantiated such:
ServerSocket socket = new ServerSocket(port);
where "port" is a number of port.
This serverSocket accept a client. This client is another application java that runs on another desktop PC that connected to the same net of the server PC. In this application java, I Instantiated a Socket object which connects with local IP of server PC (example: 192.168.1.11) and port of serverSocket. When I run the server and the client, the client does not connect to the server. If I run the server and the client on the same desktop PC, the client manages to connect to the server.
I was thinking of a problem with the firewall configuration but I did not manage to solve it. Help me please.
This is code of server:
//I will accept one client
ServerSocket server = new ServerSocket(19999);
Socket client = server.accept();
new Thread(new ManagerConnectionThread(client)).start(); //this thread manage the requests
ManagerConnectionThread is a class that manage the communication between server and client:
//This code is on the server
OutputStreamWriter out = new OutputStreamWriter(client.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
This is code of client:
public static void main(String[] args){
Socket client = new Socket(InetAddress.getByName("192.168.1.11"), 19999); //192.168.1.11 is IP of "server" pc
OutputStreamWriter out = new OutputStreamWriter(client.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
}
do you have a router involved? if yes you will need to configure some port-forwarding and try to access via the external address, meaning that you will try to connect through the router that will point to your machine.
Also, you might need to do some tweaking with the firewall, at some point I was doing something similar for school, but as a server, I was using a Linux based OS so the procedure might be different, but I definitely needed to allow in/out for the port that I was using.
you might find some answers in this post which is similar Java Chat with TCP/IP over LAN
Edit: I found my old homework, and I have definitely used the external address (especially because I wanted to access it from school, while the server was home) so I would connect to my external address of the laptop and then in the router's settings I would forward the port that I was using to the server, in the server allowing incoming traffic for the specific port.