【发布时间】: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