Java 环配置套接字通信变得混乱

问题描述 投票:0回答:1

我有这个代码:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;

public class Node {
    private String serverIpAddress;
    private String nextHopIpAddress;
    private int nextHopPort;

    private ServerSocket nodeServerSocket;
    private Socket nextNodeSocket;

    public Node(String serverIpAddress, int serverPort, String nextHopIpAddress, int nextHopPort) {
        this.serverIpAddress = serverIpAddress;
        this.nextHopIpAddress = nextHopIpAddress;
        this.nextHopPort = nextHopPort;

        try {
            nodeServerSocket = new ServerSocket(serverPort, 10, InetAddress.getByName(serverIpAddress));
            System.out.println("Server is listening on " + serverIpAddress + ":" + serverPort);

            new Thread(() -> {
               while (true) {
                   try {
                       Socket incomingConnection = nodeServerSocket.accept();
                       handleIncomingConnections(incomingConnection);
                   } catch (IOException exception) {
                       exception.printStackTrace();
                   }
               }
            }).start();

        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }

    private void handleIncomingConnections(Socket incomingConnection) {
        try {
            byte[] buffer = new byte[4];
            InputStream inputStream = incomingConnection.getInputStream();

            int bytesRead = inputStream.readNBytes(buffer, 0, buffer.length);

            int receivedValue = ByteBuffer.wrap(buffer).getInt();

            System.out.println(serverIpAddress + " received " + receivedValue + " from " + incomingConnection.getInetAddress());

            if (receivedValue == 100) {
                closeConnections();
                return;
            }

            sendData(++receivedValue);
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }

    public void sendData(int value) {
        try {
            if (nextNodeSocket == null || nextNodeSocket.isClosed() || !nextNodeSocket.isConnected()) {
                nextNodeSocket = new Socket(nextHopIpAddress, nextHopPort);
                System.out.println(serverIpAddress + " connected to " + nextHopIpAddress);
            }

            OutputStream outputStream = nextNodeSocket.getOutputStream();
            System.out.println(serverIpAddress + " sent " + value + " to " + nextHopIpAddress);

            outputStream.write(ByteBuffer.allocate(4).putInt(value).array());

        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }

    private void closeConnections() {
        try {
            if (nextNodeSocket != null && !nextNodeSocket.isClosed()) {
                nextNodeSocket.close();
            }

            if (nodeServerSocket != null && !nodeServerSocket.isClosed()) {
                nodeServerSocket.close();
            }
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Node node1 = new Node("127.0.0.1", 2345, "127.0.0.2", 3456);
        new Node("127.0.0.2", 3456, "127.0.0.3", 4567);
        new Node("127.0.0.3", 4567, "127.0.0.1", 2345);

        node1.sendData(1);
    }
}

此代码的目的是类似于这样的环形通信:

127.0.0.1 通过向下一个节点发送值 1 来开始通信,每个节点都会递增该值,直到达到 100,然后通信应停止并关闭连接。建立连接后,它应保持打开状态,直到通信停止。这是我运行该程序时的输出:

Server is listening on 127.0.0.1:2345
Server is listening on 127.0.0.2:3456
Server is listening on 127.0.0.3:4567
127.0.0.1 connected to 127.0.0.2
127.0.0.1 sent 1 to 127.0.0.2
127.0.0.2 received 1 from /127.0.0.1
127.0.0.2 connected to 127.0.0.3
127.0.0.2 sent 2 to 127.0.0.3
127.0.0.3 received 2 from /127.0.0.1
127.0.0.3 connected to 127.0.0.1
127.0.0.3 sent 3 to 127.0.0.1
127.0.0.1 received 3 from /127.0.0.1
127.0.0.1 sent 4 to 127.0.0.2

一切看起来都很好,直到“127.0.0.3 发送 3 到 127.0.0.1”和“127.0.0.1 从 /127.0.0.1 接收 3”。 127.0.0.1如何接收来自自身的信息?在最后一行之后,程序似乎被卡住了。我在这里做错了什么?

java sockets
1个回答
0
投票

发生的事情是,循环一次后,'nextNodeSocket'都建立了。但是您没有创建新的套接字,因此不会调用handleIncomingConnections,并且您没有侦听您创建的套接字上的未来数据。

您需要更改handleIncomingConnections以使用InputStream参数而不是Socket,并在发送数据后调用它(以及最初,就像您已经在做的那样)

© www.soinside.com 2019 - 2024. All rights reserved.