8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

从客户端向客户端发送 TCP/fork/socket

Melloware 2月前

57 0

我正在尝试编写一个聊天程序,协议是 TCP,两个文件是 server.c 和 client.c,如果客户端和客户端是通过 fork 创建的并且具有相同的套接字描述符,那么如何从客户端向客户端发送消息。没有...

我正在尝试编写一个聊天程序,协议是 TCP,两个文件是 server.c 和 client.c如果它们是通过 fork 创建的并且具有相同的套接字描述符,则如何从客户端向客户端发送消息。

无论我如何尝试发送消息,它都会出现在发送消息的同一控制台窗口中

帖子版权声明 1、本帖标题:从客户端向客户端发送 TCP/fork/socket
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Melloware在本站《sockets》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 我有这段代码: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...

    我有这个代码:

    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 向 127.0.0.1 发送了 3 个消息\' 和 \'127.0.0.1 从 /127.0.0.1 接收到了 3 个消息\'。127.0.0.1 怎么能从自身接收消息呢?最后一行之后,程序似乎卡住了。我在这里做错了什么?

返回
作者最近主题: