gpt4 book ai didi

c# - ~1 秒 TcpListener Pending()/AcceptTcpClient() 滞后

转载 作者:行者123 更新时间:2023-11-30 15:11:05 32 4
gpt4 key购买 nike

可能只看这个视频:http://screencast.com/t/OWE1OWVkO如您所见,连接启动(通过 telnet 或 firefox)与我的程序第一次收到消息之间的延迟。

这是等待连接的代码

    public IDLServer(System.Net.IPAddress addr,int port)
{
Listener = new TcpListener(addr, port);

Listener.Server.NoDelay = true;//I added this just for testing, it has no impact

Listener.Start();

ConnectionThread = new Thread(ConnectionListener);
ConnectionThread.Start();


}

private void ConnectionListener()
{
while (Running)
{
while (Listener.Pending() == false) { System.Threading.Thread.Sleep(1); }//this is the part with the lag
Console.WriteLine("Client available");//from this point on everything runs perfectly fast
TcpClient cl = Listener.AcceptTcpClient();

Thread proct = new Thread(new ParameterizedThreadStart(InstanceHandler));
proct.Start(cl);


}

}

(我在将代码放入代码块时遇到了一些问题)

我已经尝试了几种不同的方法,可能是我使用的是 TcpClient/Listener 而不是原始 Socket 对象吗?我知道这不是强制性的 TCP 开销,我已经尝试在同一个线程中运行所有内容,等等。

最佳答案

也许是某种 dns 解析?您是使用 IP 地址来访问您的服务器的主机还是由您的 DNS 解析的某个名称? ParmesanCodice 提供的代码应该可以立即运行,除非客户端/网络端出现问题。

尝试将以下行添加到您的 windows\system32\drivers\etc\hosts:

127.0.0.1       localhost

它可能会解决您的问题或仅连接为 127.0.0.1:85

关于c# - ~1 秒 TcpListener Pending()/AcceptTcpClient() 滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2740664/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com