javatcpserversocket

Send a socket with Socket java library


I am trying to open a TCP connection between two machines which are not within the same LAN. the server does not receive anything even though it has opened the port with port forwarding in the router settings. probably the client does not connect to the host, this is because having put a test sout after the socket initialization instruction, in the debug, in the console, nothing appears, on the contrary, the connection drops after a certain period (about 15 seconds ):

java.net.ConnectException: Connection timed out: connect
    

and I'm trying to figure out just where the error lies.

Client.java

        try {
           
            Socket s = new Socket("00.00.00.000", 6666);
            System.out.println("connected");
            s.getOutputStream().write((int) (Math.random() * 10));
            s.close();
          
        } catch (UnknownHostException uhe) {
            uhe.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

Server.java

 try {
    ServerSocket ss = new ServerSocket(6666);
    while (true) {
        Socket s = ss.accept();        
        System.out.println("Server: " + s.getInputStream().read());     
        s.close();
    }
} catch (IOException e) {
    e.printStackTrace();
}

Port Forwarding in server router settings: enter image description here


Solution

  • I solved allowing access to private and public networks in the Windows client firewall settings

    enter image description here