TCP 或 UDP 对 C# 中的服务器/客户端有帮助吗?

发布于 2024-07-25 17:03:49 字数 497 浏览 3 评论 0原文

任何人都可以帮忙,我试图弄清楚我需要做什么,我被赋予了用 TCP (UDP) 编写服务器和客户端的任务。 基本上多个客户端将连接到服务器..并且服务器将消息发送到客户端。

我在创建服务器和客户端方面没有问题,但对于 tcp,我不确定该走哪条路。 .net 3.5 是否支持所有内容,还是我需要继续寻找某些组件?

我正在寻找一些关于 TCP 或 UDP 的 C# 好例子。 这是我不是 100% 确定的地方.. 据我所知有 UDP 和 TCP ... 1 个已连接,1 个未连接.. 那么我该走哪条路,c# 可以同时支持这两种方式吗? 优点缺点?

假设服务器必须支持多个客户端,我只需要打开 1 个端口还是需要打开 2 个端口?

另外,如果客户端崩溃,我需要它不影响服务器,因此服务器可以忽略它并关闭连接(如果连接打开)或超时连接...如果实际上再次需要连接,请返回到 tcp udp

任何想法我应该在哪里开始并选择我需要分配的协议和端口数量?

谢谢

Can anyone help, i trying to figure what i need to do, i have been given the tasks of writing a server and a client in TCP (UDP). basically multiple clients will connect to the server.. and the server sends MESSSAGES to the client.

I have no problem in creating the server and client but with tcp i am unsure whcih way to go. DOes the .net 3.5 support everything or do i need to go on the hunt for some component?

I am looking for soome good examples with c# for TCP or UDP. THis is where i am not 100% sure .. as far as i know there is UDP and TCP ... 1 is connected and 1 is not.. So which way do i go and can c# support both?? Advantages /Disadvantages?

Say if the server has to support multiple clients that i only need to open 1 port or do i need to open 2?

Also if a client crashes i need for it not to effect the SERVER hence the server can either ignore it and close connection if one is open or timeout a connection... If in fact a connection is needed again going back to tcp udp

Any ideas where i shoudl beging and choosing which protocol and amount of ports i am going to need to assign?

thanks

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

毁梦 2024-08-01 17:03:49

UDP 缺点:

  • 数据包大小限制意味着您只能发送小消息(小于约 1.5k 字节)。
  • 缺乏流使得保护 UDP 变得困难:很难制定适用于有损交换的身份验证方案,并且同样难以保护单个消息的完整性和机密性(没有可依赖的密钥状态)。
  • 没有传递保证意味着您的目标必须准备好应对消息丢失。 现在很容易争论,如果目标可以处理消息的完全丢失(这是可能的),那么为什么首先要费心发送它们呢?

UDP 优点:

  • 无需在服务器上为每个客户端存储系统端点(即无需套接字)。 这是连接数十万客户端的 MMO 游戏使用 UDP 的主要原因之一。
  • 速度:事实上,每条消息都是单独路由的,这意味着您不会像 TCP 那样遇到流拥塞。
  • 广播:UDP可以向一个网段上的所有监听者广播。

如果您也在考虑 TCP,那么您甚至不应该考虑 UDP。 如果您正在考虑 TCP 意味着您正在考虑流(按顺序消息仅一次),并且使用 UDP 会给您的应用带来碎片、重试和确认、重复检测和排序的负担。 您很快就会在您的应用程序中重新发明 TCP,全世界所有工程师都花了 20 年的时间才能做到这一点(或者至少与 IPv4 中的一样正确)。

如果您不熟悉这些主题,我建议您顺其自然并使用 WCF,至少它为您提供了相对轻松地切换各种传输和协议的优势。 如果您使用原始 .Net 套接字组件做出了错误的选择,那么将代码库从 TCP 更改为 UDP 会更加困难,反之亦然。

UDP cons:

  • packet size restriction means you can only send small messages (less than about 1.5k bytes).
  • Lack of stream makes it hard to secure UDP: hard to do an authentication scheme that works on lossy exchange, and just as hard to protect the integrity and confidentiality of individual messages (no key state to rely on).
  • No delivery guarantee means your target must be prepared to deal with message loss. Now is easy to argue that if the target can handle a total loss of messages (which is possible) then why bother to send them in the first place?

UDP Pros:

  • No need to store a system endpoint on the server for each client (ie. no socket). This is one major reason why MMO games connected to hundred of thousands of clients use UDP.
  • Speed: The fact that each message is routed individually means that you cannot hit a stream congestion like TCP can.
  • Broadcast: UDP can broadcast to all listeners on a network segment.

You shouldn't even consider UDP if you're considering TCP too. If you're considering TCP means you are thinking in terms of a stream (exactly once in order messages) and using UDP will put the burden of fragmentation, retry and acknowledgment, duplicate detection and ordering in your app. You'll be in no time reinventing TCP in your application and it took all engineers in the word 20 years to get that right (or at least as right as it is in IPv4).

If you're unfamiliar with these topics I recommend you go with the flow and use WCF, at least it gives you the advantage of switching in and out with relative ease various transports and protocols. Will be much harder to change your code base from TCP to UDP and vice versa if you made the wrong choice using raw .Net socket components.

蓝眼泪 2024-08-01 17:03:49

我觉得你好像不清楚 TCP 和 UDP 之间的区别。

TCP是面向连接的。 即 2 个对等点将有一个专用连接。 保证包裹递送和订购。 通常,服务器会提供一个端口,多个客户端可以连接到该端口(想象一下 HTTP 服务器和浏览器)。

UDP 是无连接的。 它不保证数据包交付,也不保证订购。 您可以非常轻松地实现广播和多播机制。 如果您需要某种可靠性,则必须在 UDP 之上实现。 有时您可能不在乎,只是发出请求并在没有响应时重试(SNMP 就是这样做的)。 因为它是无连接的,所以您不必真正担心同级的上线/下线。 如果需要,您只需重试。

因此,您对协议的选择取决于上述内容。 例如,您的客户端是否需要与服务器的专用连接? 您是否向多个客户端传输相同的数据? 您能否容忍丢包(例如实时价格更新等)。 也许在您的应用程序中使用 TCP 和 UDP 来满足不同的要求是可行的(例如 TCP 用于注册订单,UDP 用于传输价格更新/事件?)

我会考虑您的要求,并熟悉 TCP 和 UDP 的限制和功能。 这应该会让事情变得更清楚一些。

It sounds to me like you're not clear on the distinction between TCP and UDP.

TCP is connection oriented. i.e. 2 peers will have a dedicated connection. Packet delivery and ordering is guaranteed. Typically a server will present a port, and multiple clients can connect to that port (think of a HTTP server and browsers).

UDP is connectionless. It doesn't guarantee packet delivery, nor ordering. You can implement broadcast and multicast mechanisms very easily. If you need some sort of reliability, you will have to implement this on top of UDP. Sometimes you may not care, and simply issue requests and retry on no response (SNMP does this). Because it's connectionless, you don't really worry about peers being up/down. You just have to retry if required.

So your choice of protocol is dictated by the above. e.g. does your client require a dedicated connection to the server ? Are you transmitting the same data to multiple clients ? Can you tolerate packet loss (e.g. real time price updates etc.). Perhaps it's feasible to use both TCP and UDP for different requirements within your app (e.g. TCP for registering orders, UDP for transmitting price updates/events?)

I'd consider your requirements, and familiarise yourself with the limitations and features of TCP and UDP. That should make things a little clearer.

自此以后,行同陌路 2024-08-01 17:03:49

有必要在这么低的水平上做到这一点吗? 为什么不使用WCF? 它完全支持通过 TCP/IP 进行消息传递,使用二进制数据传输,但它的抽象级别比原始套接字高得多。

Is there a requirement to do this at such a low level? Why not use WCF? It fully supports messaging over TCP/IP, using binary data transfer, but it's at a much higher level of abstraction than raw sockets.

甜尕妞 2024-08-01 17:03:49

您需要的一切都在 .Net 3.5(可能更低)中。 使用 UdpClient MSDN 课程,深入了解如何编写客户端/服务器。 快速谷歌找到了一些 服务器客户端位于 www.java2s.com 以及许多其他 C# 网络示例。 不要被域名吓倒。

Everything you need is in .Net 3.5 (and probably below). Check out the documentation and examples with the UdpClient class at MSDN for insight into how to write your client/server. A quick google found some sample code for a server and client at www.java2s.com among many other networking examples in C#. Don't be put off by the domain name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文