简体   繁体   中英

unable to connect to linux machine through socket

I have aa server running on a linux machine with centos 7. On the machine I run a code the manages client connections to the server. Here is my code:

public class ServerMain_mng {
final static int nPort = 3333;

public static void main(String[] args) throws IOException {
    new ServerMain_mng();
}

public ServerMain_mng(){
    try {
        ServerSocket sSocket = new ServerSocket(3333);
        System.out.println("Server started at: " + new Date());
        System.out.println("===============================================\n");

        while(true) {
            System.out.println("waiting for connection");
            //Wait for a client to connect
            Socket socket = sSocket.accept();
            socket.setSoTimeout(30000);
            //Create a new custom thread to handle the connection
            ClientThread cT = new ClientThread(socket, nPort);
            //Start the thread!
            new Thread(cT).start();  
        }
    } 
    catch(IOException ex) {ex.printStackTrace();}
}}

The problem is that the code does not get past the line Socket socket = sSocket.accept(); when a client tries to connect to the server.

I should note that i run the same code on my laptop, connecting as a client from a local host and it works fine. I checked the port forwarding in the router and the specified ports are open.

What could be the problem? Any ideas how to fix it? any help would be appreciated.

sSocket.accept() is a blocking call, it is supposed to remain blocked untill some client connects to it using the same port where it is listening.

 public class Client {
         final static int nPort = 3333;
         public static void main(String[] args) throws IOException {
         Socket sock=new Socket(IP of Server, nPort)
         System.out.println("connected to: +sock.getRemoteSocketAddress())
      //send additional data needed for the server using the socket's outputputStream
            }
            }

At the server,

 ClientThread cT = new ClientThread(socket);
  //Start the thread!
  new Thread(cT).start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM