C#UDP客户端与服务器性能和设计之间的无连接通信

发布于 2025-01-23 21:38:57 字数 736 浏览 1 评论 0原文

嗨,男孩和女孩。

从我以前在这里的问题中可以看出,我是C#网络中的新手,所以如果我的问题看起来太容易或愚蠢,请不要努力地判断我。

我知道网络是一个相当大且复杂的话题,这就是为什么我会尽可能紧密而描述的问题。如果我在某个时候走得太宽,请纠正我。

因此,我正在C#中构建一个简单的网络应用程序,该应用程序将在服务器和客户端的Localhost上运行(暂时)。 其背后的想法是将随机INT从服务器发送到客户端,然后每个客户端根据某些条件将另一个随机INT回复。 问题是可能有200个客户,而发送/接收每1/60秒就会发生。 我将System.Sockets用作网络基础层,并且我正在为我的服务器和客户端使用非阻滞模式(均设置为带有DGRAM类型的UDP协议)。

到目前为止,我知道可以使用回答sendto读取或写数据。 这将读取/写入输入/发出数据,以指定为参数的字节[]缓冲区。

到目前为止一切都很好...但是 - 一旦接收/发送数据,我应该如何处理缓冲区? 我应该做一些诸如buffer.blockcopy()之类的事情,然后清洁缓冲区并开始再次读取/写作,还是应该使用像缓冲池之类的东西...你们该如何处理? 我的意思是说,每1/60秒4个字节我知道哪个客户写了哪个客户,我从缓冲区中读到的那4个字节,我该如何逃脱缓冲或错过消息,因为缓冲区我如何逃脱,因为缓冲区我如何逃脱,因为缓冲区如何,因为缓冲区,我如何知道200个端已经满了,没有写所有数据? 重要的是,我以非阻滞方式对此应用进行工作。 预先感谢所有意见/评论/答案!

Hi guys and girls.

As seen from my previous questions in here ,I am newbie in C# networking, so please don't judge me too hard if my question seems too easy or stupid.

I am aware that networking is quite a big and complicated topic, this is why I will try to as my question as tight and descriptive as possible. PLease correct me if I go too broad at a point.

So I am building a simple networking app in C#, which will run (for the moment) on the localhost for both server and client.
The idea behind it is to send random int from the server to the client and then each client replies with another random int based on some conditions.
The problem is that there might be like 200 clients , and the send/recieve happens every 1/60 second.
I am using System.Sockets as networking base layer , and I am using non-blocking mode for both my server and client ( both set to UDP protocol with Dgram type ).

So far I am aware that one can read or write data using RecieveFrom and SendTo .
This would read/write the incoming/outgoing data to a byte[] buffer specified as parameter.

So far so good... But - how do should I go about the buffer once I receive/send data ?
Should I do something like Buffer.BlockCopy() and then clean the buffer and begin read/write again, or should I use something like a buffer pool ... How would you guys go about this ?
I mean let's say 200 clients write to my buffer each 1/60 second 4 bytes how do I know which client wrote those 4 bytes I am reading from the buffer , how do I get away with not flooding the buffer or miss messages because the buffer was full and didn't write all the data?
It is important that I do this in non-blocking way for this app to work.
Thanks in advance for each and every opinion/comment/answer !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文