.NET TCP客户端/服务器太慢了? [英] .NET TCP Client/Server are too slow?

查看:92
本文介绍了.NET TCP客户端/服务器太慢了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Codeproject,



你可能知道或者可能不知道我在过去一年左右开发游戏,我终于准备开始让它在线。



然而,在我完成基础工作(工作服务器和客户端)后,我开始研究移动系统,以便两个客户都知道他们都在搬家。



这段代码基本上如下:



>客户1移动。

>客户端1将数据包发送到已移至的服务器。

>服务器将相同的数据包发送给客户端2.

>客户端2读取数据包并更新另一个客户端的实体。



一切都完成后,我看到了结果,他们有点让我跳了起来。另一个玩家的FPS似乎大约是5,如果不是更少,这是因为将数据包发送到服务器并返回到另一个客户端。



现在为什么.NET这么慢?因为这不是我的朋友,这是我的电脑到我的电脑到我的电脑。



有没有人有任何可能有助于解决这个问题的解决方案?因为我看到像Minecraft,Team Fortress 2这样的游戏具有更复杂的功能,并且它们在适当的服务器上没有任何移动延迟。



所有的问候,



- Deviant

Hello Codeproject,

As you may or may not know I've been developing a game for the past year or so, and I am finally ready to start making it Online.

However, after I had gotten the basics done(Working Server, and Client) I started working on the movement system so that both clients were aware that they both were moving.

This code is basically the following:

> Client 1 Moves.
> Client 1 Sends a packet to the server where it has moved to.
> Server sends the same packet to Client 2.
> Client 2 reads the packet and updates the other client's entity.

After everything was done, and ready I saw the results, and they kinda made me jump. The FPS of the other player seemed to be about 5 if not less, and this is because of the sending of the packets to the server and back to the other client.

Now why is .NET so slow? Because this wasn't me to my friend, this was my computer to my computer to my computer.

Does anyone have any solutions that might help fix this problem? Because I see games such as Minecraft, Team Fortress 2 whom have more complex features, and they don't have any movement lag on a proper server.

With all the regards,

- Deviant

推荐答案

从你说的话来说很难说。

如果你查看我的文章:编写多人游戏(在WPF中) [ ^ ]我谈论可能的游戏开发,我把它作为服务器中心(我认为这就是你想要做的)。



一个重要的要注意的是,如果你的沟通不一样:

客户端发送数据,客户端接收数据,客户端发送数据,客户端接收数据。



您可能需要将TcpClient.NoDelay设置为true。

默认情况下,此类NoDelay为false,其目的是避免发送许多小包。也就是说,第一个发送直接进行,但第二个,第三个等等将等待直到tcp缓冲区满(通常为4kb)或200毫秒。这证明每秒只有5帧。



此外,通过这个特性,你不应该直接序列化/写入数据到tcp / ip堆栈。如果需要多次写入调用,则应使用自己的缓冲区,完成后,必须在一次调用中复制整个缓冲区(将其写入tcpclient / stream)。这是为了避免相反的问题,如果你使用tcpdelay为false,WriteByte调用足以开始发送数据。
From what you say it is very hard to tell.
If you look at my article: Writing a Multiplayer Game (in WPF)[^] I talk about possible game developments and I did it as server centric (and I think that's what you want to do).

An important thing to note is that if your communication is not like:
Client send data, client receive data, client send data, client receive data.

You will probably need to use the TcpClient.NoDelay set to true.
Such NoDelay is false by default and its purpose is to avoid many small packages to be sent. That is, the first send goes directly, but the second, third, etc will wait either until the tcp buffer is full (usually 4kb) or 200 milliseconds. That justifies having only 5 frames per second.

Also, by this same trait, you should never serialize/write data directly to the tcp/ip stack. If you need more than one write call, then you should use your own buffer and, when you finish, you must copy the entire buffer (write it to the tcpclient/stream) in a single call. This is to avoid the opposite problem if you use tcpdelay to false, a WriteByte call is enough to start sending data.


这篇关于.NET TCP客户端/服务器太慢了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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