2011-11-21 27 views
1

有鑑於此:設置插座的本地端口拋出異常

. . . 
ServerSocket serverSocket = new ServerSocket(1111); 

while (helperSockets.size() < Common.NUM_HELPERS) { 
    Socket helperSocket = serverSocket.accept(); 
. . . 

這引發異常:

new Socket("localhost", 1111, InetAddress.getLocalHost(), localPort); 

例外:

java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine 

我試圖用這個構造函數來設置當地的港口,所以我做錯了什麼?來源如下。

構造函數創建一個線程,該線程通過ServerSockets偵聽客戶端,同時嘗試連接到其他客戶端。該客戶端嘗試與自己進行連接以進行測試。在for循環的第一次迭代中會遇到問題。

public class DisSemHelper extends Thread { 
private int id; 
private int semaphore; 
private Clock clock; 
private Vector<Socket> helperSockets; 
private int localPort; 

private int receivedSender; 
private String receivedOperation; 
private int receivedTimestamp; 

/** 
*/ 
public DisSemHelper(int id) { 
    this.id = id; 
    this.semaphore = 0; 
    this.clock = new Clock(); 
    this.helperSockets = new Vector<Socket>(); 
    this.receivedSender = -1; 
    this.receivedOperation = null; 
    this.receivedTimestamp = -1; 
    this.localPort = Common.portMap.get(id); 

    new ConnectionListener().start(); 

    /* Create and store connections to all helpers */ 
    for (int i=0; helperSockets.size() < Common.NUM_HELPERS; i++) { 
     Socket helperSocket = null; 
     //    String portKey = "STREET_" + i; 

     /* Wait until target street socket is ready. Retry every second. */ 
     Exception e = new ConnectException(); 
     while (helperSocket == null) { 
      try { 
       Thread.sleep(1000); 
       helperSocket = new Socket("localhost", 2222, InetAddress.getLocalHost(), localPort); 

      } catch (ConnectException ce) { 
       e = ce; 
       e.printStackTrace(); 
      } catch (UnknownHostException uhe) { 
       uhe.printStackTrace(); 
      } catch (IOException ioe) { 
       ioe.printStackTrace(); 
      } catch (InterruptedException ie) { 
       e.printStackTrace(); 
      } 
     } 

     int remotePort = helperSocket.getPort(); 
     int connectedHelperID = Common.portMap.indexOf(remotePort); 
     if (this.helperSockets.size() <= connectedHelperID) { 
      this.helperSockets.add(helperSocket); 
      System.out.println("Helper " + id + " added socket from outgoing: local port: " + helperSocket.getLocalPort() + " remote port: " + helperSocket.getPort()); 
     } 
    } 

    System.out.println(this.helperSockets); 
} 

private class ConnectionListener extends Thread { 
    public void run() { 
     try { 
      ServerSocket serverSocket = new ServerSocket(2222); 

      /* Listen for connections from other helpers */ 
      while (helperSockets.size() < Common.NUM_HELPERS) { 
       Socket helperSocket = serverSocket.accept(); 
       // TODO Will indexof int in list of Integers work? 
       int remotePort = helperSocket.getPort(); 
       int connectedHelperID = Common.portMap.indexOf(remotePort); 
       // TODO Does this really work? 
       if (connectedHelperID == -1) { 
        helperSockets.add(helperSocket); 
        System.out.println("Helper " + id + " added socket from incoming: local port: " + helperSocket.getLocalPort() + " remote port: " + helperSocket.getPort()); 
       } 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

回答

0

不是一個真正的解決方案,但我最終在設置本地端口(因爲沒有人喜歡這個想法)。

1

這是一個WAG,但我會嘗試套接字中的其他構造函數。嘗試新的套接字(INetAddress.getLocalHost(),「111」);然後嘗試新套接字(INetAddress.getLocalHost(),「111」);

參見:

http://ashishmyles.com/tutorials/tcpchat/index.html

+0

是我的壞。上面的問題中添加了異常。忘記打印堆棧跟蹤。 – Rooster

+0

是的,那個實際上工作。但我仍然需要設置本地端口。 – Rooster

+0

不確定你設置本地端口的意思。對不起,我不能更有幫助。 \ –

1

你能否做一個簡單的測試案例和公佈源?這將幫助我們更好地排除故障。

new Socket("localhost", 1111, InetAddress.getLocalHost(), localPort); 

你究竟在做什麼?爲什麼您需要定義本地使用的本地主機/端口對?客戶端通常只需指定遠程主機/端口並讓OS選擇本地端口。這可能是問題所在。

+0

我這樣做是因爲我有客戶互相連接,跟蹤他們連接的人(使用端口號)。服務器套接字在localhost上進行測試;它可能以後會在不同的機器上。 – Rooster

+0

這很好。但我的問題仍然有效。爲什麼你必須選擇'localPort'?客戶端可以彼此連接,而無需指定用於連接服務器的localPort(在您的情況下,它可能是另一個客戶端)。如果您向我們展示您的完整代碼或測試案例,我們可以更輕鬆地進行調試。此外,如果您在短時間內重新使用「localPort」,您可能會得到該例外。 –

+0

您仍然可以在散列中跟蹤遠程主機/端口組合。看看我在你的另一篇文章上的答案,希望能更好地闡明我的想法。 –