大概只要看这个视频就可以了:http://screencast.com/t/OWE1OWVkO 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)