【问题标题】:TCP Server only works in localhost schemeTCP Server 仅适用于 localhost 方案
【发布时间】:2016-05-23 07:41:15
【问题描述】:

我可以在 localhost 中的 TCP 客户端和 TCP 服务器之间建立 TCP 连接,但是对于将不同计算机连接到相同网络范围(发送方 Windows Server 2012 x64 R2 和接收方 Windows 10)的连接,我不能重复相同的示例x64 专业版)。 TCP 服务器是一个 C# 应用程序,而 TCP 客户端位于 node.js 中。我已禁用防病毒和 Windows 防火墙。

//SERVER C#

void Receive() {
        //tcp_Listener = new TcpListener(IPAddress.Parse("192.168.1.62"), 212);
        IPAddress localAddr = IPAddress.Parse("0.0.0.0");
        tcp_Listener = new TcpListener(localAddr,212);
        TcpClient clientSocket = default(TcpClient);
        tcp_Listener.Start();
        print("Server Start");

        while (mRunning)
        {
            // check if new connections are pending, if not, be nice and sleep 100ms
            if (!tcp_Listener.Pending()){
                Thread.Sleep(10);
            }
            else {
                clientSocket = tcp_Listener.AcceptTcpClient();
                NetworkStream networkStream = clientSocket.GetStream();
                byte[] bytesFrom = new byte[10025];
                networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                if (dataFromClient != "") {
                    print ("Data from client: " + dataFromClient);
                } else {
                    print ("Client no data");
                }
                clientSocket.Close();

            }
        }
    }

   //CLIENT NodeJS

        var net = require('net');

        var HOST = '192.168.0.136';
        var PORT = 212;

        var client = new net.Socket();
        client.connect(PORT, HOST, function() {

            console.log('CONNECTED TO: ' + HOST + ':' + PORT);
            // Write a message to the socket as soon as the client is connected, the server will receive it as message from the client 
            client.write('MSG SENT!');

        });

        // Add a 'data' event handler for the client socket
        // data is what the server sent to this socket
        client.on('data', function(data) {

            console.log('DATA: ' + data);
            // Close the client socket completely
            client.destroy();

        });

        // Add a 'close' event handler for the client socket
        client.on('close', function() {
            console.log('Connection closed');
        });

关于整个连接的wireshark日志如下:

这是来自 TCP 客户端的日志:

【问题讨论】:

  • 与您当前的问题无关,但您的服务器代码已损坏。 Read 返回 多少 个字节已放入您的缓冲区,您需要确保不使用实际上不包含任何接收数据的缓冲区部分。
  • 为什么服务器和客户端都直接关闭destroy?我认为这会产生问题,尤其是客户端回调中的破坏。
  • @Bewar Salah 我已经删除了客户端的销毁并且错误仍然存​​在
  • @Damien_The_Unbeliever 感谢您的建议。
  • 我没有收到任何调试错误,但在networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize) 行中使用了“try catch”,我在该行中看到了整数溢出。我已经更换了networkStream.Read (bytesFrom, 0, bytesFrom.Length),问题就解决了。我不知道为什么它在 localhost 中工作......

标签: c# node.js sockets tcp


【解决方案1】:

我已经替换了 networkStream.Read (bytesFrom, 0, bytesFrom.Length)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多