设置套接字本地端口会引发异常

时间:2011-11-21 05:16:41

标签: java sockets

鉴于此:

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

3 个答案:

答案 0 :(得分:1)

这是一个WAG,但我会在Socket中尝试其他构造函数。

尝试新的Socket(INetAddress.getLocalHost(),“111”);

请参阅:

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

答案 1 :(得分:1)

你能做一个简单的测试用例并发布源代码吗?这将有助于我们更好地解决这个问题。

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

你到底想要做什么?为什么需要定义本地使用的本地主机/端口对?客户端通常应该只指定远程主机/端口,然后让操作系统选择本地端口。这可能就是问题所在。

答案 2 :(得分:0)

不是真正的解决方案,但我最终解决了设置本地端口的问题(因为似乎没有人喜欢这个想法)。