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

标签 java sockets

鉴于此:

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

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

这会引发异常:

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

异常(exception):

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

最佳答案

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

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

参见:

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

关于java - 设置套接字本地端口会引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8207581/

相关文章:

java - 使用有界继承重写 Scala 中 Java 类的抽象方法

java - Android连续tcp轮询

c++ - 确定未绑定(bind)套接字的地址族

c - 为什么动态分配的数组不会随着新数据的到来而更新?

在文本字段中输入的 JavaScript 执行行为与 sendKeys 不同

Java 字节操作 - 将 3 字节转换为整数数据

python-3.x - 捕获套接字超时错误

c# - javascript和C#服务器通信

java - AccessibilityService剪贴板复制事件

java - 如何使用 Kubernetes Azure 和 AWS SDK for java 部署应用程序