【问题标题】:Why does netcat work just fine when using my listener but it doesnt work when I use JS为什么 netcat 在使用我的侦听器时可以正常工作,但在我使用 JS 时却不起作用
【发布时间】:2023-02-08 00:15:53
【问题描述】:

我写了一个 C# 侦听器,它侦听一个端口并打印它收到的所有内容,它工作得很好,但是当我使用 JS 客户端发送该数据时,收到了一些内容,但没有任何内容写入控制台

我的 C# 代码:

while (true)
{
    TcpClient client = null;
    NetworkStream stream = null;

    try
    {
        client = listener.AcceptTcpClient();
        stream = client.GetStream();

        using (StreamWriter writer = new StreamWriter(stream, Encoding.ASCII) { AutoFlush = false })
        {
            using (StreamReader reader = new StreamReader(stream, Encoding.ASCII))
            {
                while (true)
                {
                    string inputLine = "";
                    while (inputLine != null)
                    {
                        inputLine = reader.ReadLine();
                        Console.WriteLine(inputLine);
                        Console.WriteLine(Encoding.UTF8.GetString(Encoding.ASCII.GetBytes(inputLine)));
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    finally
    {
        if (stream != null)
        {
            stream.Close();
        }
        if (client != null)
        {
            client.Close();
        }
    }

    Console.WriteLine("Verbinding met client verbroken");
}
'use strict';
  var net = require('net');

  var client = new net.Socket();
  net.connect(1234, '192.168.2.13', function() {
      console.log('Connected');
      socket.write('Hello server');
  });


我尝试运行一个 netcat 监听器,它与我的 JS 程序一起工作,我还在我的代码中设置了断点,并得出结论,当我用我的 JS 代码发送一些东西时,它确实被我的服务器接收但没有被处理。

【问题讨论】:

    标签: javascript c# networking listener


    【解决方案1】:

    您的服务器/侦听器尝试读取完整的一行('reader.ReadLine()')。

    这意味着,您的 JS 客户端必须发送一些“行结束”标记。根据文档,ReadLine 接受“ ", " “ 或者 ” ”。

    所以 socket.write("Hello server ") 应该可以。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      • 2019-11-20
      • 2020-08-31
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      相关资源
      最近更新 更多