UdpClient.Send 方法和 UdpClient.Client.Send 方法之间的区别
我可以使用 UdpClient.Send(byte array) 或 UdpClient.Client.Send(stream) 方法在整个 udp 协议中发送数据。两种方法都有效。唯一的区别是,在一种方法上我传递一个字节数组,在另一种方法上我传递一个流。
快速示例:
UdpClient udpClient = new UdpClient(localEndPoint);
// I can eather send data as:
udpClient.Send(new byte[] { 0, 1, 2 }, 3);
udpClient.Client.Send(new byte[5]);
哪种方法可以确保我的数据到达目的地而不会丢失信息?我读到,udp 协议不能确保所有字节都到达目的地,因此更适合流式传输视频、音频,但不适合像我一样传输文件。我使用 udp 而不是 tcp 的原因是因为在恰好位于路由器后面的两个用户之间建立 tcp 连接非常复杂。我知道如果其中一个用户在他的路由器上启用端口转发,这是可能的。我设法通过所谓的 udp 打孔来发送数据。 udp 打孔使您能够在服务器的帮助下在路由器后面的两个用户之间建立连接。解释它是如何工作的会很长,如果你用谷歌搜索,你可以找到很多信息。无论如何,我只是想让你知道为什么我使用 udp 而不是 tcp。我现在不知道是否可以使用此协议发送文件以确保没有数据丢失。也许我必须创建一个算法。或者,UdpClient.Client.Send 方法可能确保将接收数据,而 UdpClient.Send 方法不确保将接收数据。
I can either send data throughout the udp protocol with the UdpClient.Send(byte array) or the UdpClient.Client.Send(stream) method. both methods work. the only differences are that on one method I pass a byte array and on the other I pass a stream.
quick example:
UdpClient udpClient = new UdpClient(localEndPoint);
// I can eather send data as:
udpClient.Send(new byte[] { 0, 1, 2 }, 3);
udpClient.Client.Send(new byte[5]);
Also which method will ensure that my data reaches it's destination without loosing information? I have read that the udp protocol does not ensure that all bytes reach it's destination thus is better for streaming video, audio but not for transferring files like I am doing. The reason why I am using udp instead of tcp is because it is very complicated to establish a tcp connection between two users that happen to be behind a router. I know it will be possible if one of the users enables port forwarding on his router. I managed to send data by doing what is called udp punch holing. udp punch holing enables you to establish a connection between two users that are behind a router with the help of a server. It will be long to explain how that work in here you can find lot's of information if you google it. Anyways I just wanted to let you know why I was using udp instead tcp. I don't now if it will be possible to send a file with this protocol making sure that no data is lost. maybe I have to create an algorithm. or maybe the UdpClient.Client.Send method ensures that data will be received and the UdpClient.Send method does not ensure that data will be received.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UDP 不保证数据传送或数据顺序。它仅保证如果您成功接收数据包,则数据包是完整的。您需要通过自己的实施来确保网络通信的可靠性。这两个函数不应该有任何区别。
UNIX 网络编程有一章介绍此主题。 (22.5 向 UDP 应用程序添加可靠性)。您还可以查看libginble,它支持NAT穿越功能(与 STUN 或继电器)和通信的可靠性。
本文可靠性和流量控制可能会还可以帮助您了解一种可能的实现方法。祝你好运!
UDP does not guarantee data delivery or order of them. It only guarantees if you receive packet successfully, the packet is complete. You need to make your network communication reliable with your own implementation. The two functions should not make any difference.
UNIX Network Programming has a chapter for this topic. (22.5 Adding Reliability to a UDP Application). You can also take a look at libginble which supports NAT traversal function (with STUN or relay) and reliability of communication.
This article, Reliability and Flow Control, might also help you to understand one possible way to implement it. Good Luck!
这两种方法没有区别,UdpClient 是 Socket 的包装器。 UdpClient.Client 只是让您直接访问 UdpClient 正在使用的 Socket。它们最终都通过套接字发送数据。
两者均不保证会收到所有数据。 UDP 不保证 1. 数据将被传送,以及 2. 如果传送,它将按顺序传送。如果您需要这两者(并且不能使用 TCP),则必须自己编写该代码。
您可以使用的最简单的协议是向每个数据包添加一个序列。让服务器发送一个数据包并等待客户端收到它的响应,然后发送下一个数据包。该顺序很重要,因为如果客户端对服务器的响应丢失,客户端可能会从服务器收到相同的数据包两次。
您的序列号可以是文件大小(以字节为单位)的当前位置 - 这就是 TCP 使用的。
我建议阅读 TCP 协议,以便您了解它用于处理可靠性的机制(请记住 TCP 和 UDP 都是基于 IP 构建的 - TCP 只是一个更强大(且复杂)的协议,因此您可以自己实现其中的一部分)
There is no difference between the two methods, UdpClient is a wrapper around a Socket. UdpClient.Client just gives you direct access to the Socket that UdpClient is using. They both end up sending data through through the socket.
Neither one guarantees that all data will be received. UDP makes no guaranty that 1. data will be delivered and 2. if it is delivered that it will be delivered in order. If you need both of those (and can't use TCP) you'll have to write that code yourself.
The simplest protocol you can use is to add a sequence to each of your packets. Have the server send one packet and wait for the response from the client that it received it an then send the next packet. The sequence is important because the client may receive the same packet twice from the server, if the client's response to the server is lost.
Your sequence number can be the current position of the file's size in bytes - that's what TCP uses.
I'd recommend reading up on the TCP protocol so you understand the mechanisms it uses to handle reliability (remember both TCP and UDP are built on IP - TCP is just a more robust (and coplex) protocol so you can implement parts of it yourself)