TCP 或 UDP 对 C# 中的服务器/客户端有帮助吗?
任何人都可以帮忙,我试图弄清楚我需要做什么,我被赋予了用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
UDP 缺点:
UDP 优点:
如果您也在考虑 TCP,那么您甚至不应该考虑 UDP。 如果您正在考虑 TCP 意味着您正在考虑流(按顺序消息仅一次),并且使用 UDP 会给您的应用带来碎片、重试和确认、重复检测和排序的负担。 您很快就会在您的应用程序中重新发明 TCP,全世界所有工程师都花了 20 年的时间才能做到这一点(或者至少与 IPv4 中的一样正确)。
如果您不熟悉这些主题,我建议您顺其自然并使用 WCF,至少它为您提供了相对轻松地切换各种传输和协议的优势。 如果您使用原始 .Net 套接字组件做出了错误的选择,那么将代码库从 TCP 更改为 UDP 会更加困难,反之亦然。
UDP cons:
UDP Pros:
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.
我觉得你好像不清楚 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.
有必要在这么低的水平上做到这一点吗? 为什么不使用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.
您需要的一切都在 .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.