C#套接字异常延迟 [英] C# socket abnormal latency

查看:111
本文介绍了C#套接字异常延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#和XNA开发一个在线多人乒乓球小游戏.

I am working on a little online multiplayer pong game with C# and XNA.

我使用套接字在个人LAN上的两台计算机之间传输数据.效果很好.

I use sockets to transfer data between two computers on my personnal LAN. It works fine.

问题在于速度:传输速度慢.

The issue is with speed : the transfer is slow.

当我对第二台计算机执行ping操作时,它显示2 ms的延迟. 我在代码中设置了一个小计时器,它显示了大约200毫秒的延迟. 即使服务器和客户端在同一台计算机上(使用127.0.0.1),延迟仍然约为15毫秒.我认为这很慢.

When I ping the second computer, it shows a latency of 2 ms. I set up a little timer inside my code, and it shows a latency of about 200 ms. Even when the server and the client are on the same computer (using 127.0.0.1), the latency still about 15 ms. I consider this as slow.

以下是我的代码的一部分:

Here are some fragment of my code :

server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(new IPEndPoint(IPAddress.Any, port));
server.Listen(1);
// Begin Accept
server.BeginAccept(new AsyncCallback(ClientAccepted), null);

在ClientAccepted中,我设置了NetworkStream,StreamReader和StreamWriter. 当我想更新播放器的位置时,这就是我发送消息的方式:

in ClientAccepted, I set up a NetworkStream, a StreamReader and a StreamWriter. This is how I send a message when I want to update the player's location :

string message = "P" + "\n" + player.Position + "\n";
byte[] data = Encoding.ASCII.GetBytes(message);
ns.BeginWrite(data, 0, data.Length, new AsyncCallback(EndUpdate), null);

EndUpdate唯一要做的就是调用EndWrite. 这就是我接收数据的方式:

the only thing EndUpdate do is to call EndWrite. This is how I receive data :

message = sr.ReadLine();

它不在第二个线程上,因此不会阻塞我的游戏.

It dosen't block my game since it's on a second thread.

这些是我尝试过的东西: -使用IP代替TCP -使用二进制消息代替文本 -使用IPv6代替IPv4 真的没有帮助.

These are the stuff I tried : - Use IP instead of TCP - Use binary message instead of text - Use IPv6 instead of IPv4 Nothing really did help.

关于如何改善延迟的任何想法?

Any thoughts about how I can improve the latency?

谢谢

推荐答案

您看到的延迟很可能是由于

The latency you're seeing is most likely due to Nagle's algorithm which is used to improve efficiency when sending lots of small packets using TCP. Essentially, the TCP stack collects small packets together and coalesces them into a larger packet before transmitting. This obviously means delaying for a small interval - up to 200ms - to see if any more data is sent, before sending what's waiting in the output buffers.

因此,请尝试通过设置关闭Nagle将套接字上的NoDelay 属性设置为true.

So try switching off Nagle by setting the NoDelay property to true on the socket.

但是,请注意,如果对套接字进行大量的小写操作,则可能会由于TCP标头开销和每个数据包的处理而导致性能下降.在这种情况下,如果可能的话,您希望尝试将您的写入内容分批收集.

However, be aware that if you do a LOT of small writes to the socket, that you may lose performance because of the TCP header overhead and processing of each packet. In that case, you'd want to try to collect your writes together into batches if possible.

这篇关于C#套接字异常延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆