簡體   English   中英

無法通過套接字連接到Linux計算機

[英]unable to connect to linux machine through socket

我有一台運行在具有centos 7的Linux機器上的服務器。在這台機器上,我運行一個代碼來管理與服務器的客戶端連接。 這是我的代碼:

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();}
}}

問題是代碼無法通過Socket socket = sSocket.accept(); 當客戶端嘗試連接到服務器時。

我應該注意,我在筆記本電腦上運行相同的代碼,從本地主機作為客戶端連接,並且運行良好。 我檢查了路由器中的端口轉發,並打開了指定的端口。

可能是什么問題呢? 任何想法如何解決? 任何幫助,將不勝感激。

sSocket.accept()是一個阻塞調用,它應該保持阻塞狀態,直到某些客戶端使用它正在偵聽的相同端口連接到它為止。

 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
            }
            }

在服務器上,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM