0

我正在用 c# 制作应用程序。我不断在 24097 端口上获取数据,并将该数据记录为

UdpClient client = null;
 IPEndPoint ipep = null;
 client = new UdpClient(24097); 
 client.Client.ReceiveBufferSize = 25000;
 ipep = new IPEndPoint(IPAddress.Any,24097);
while(flag)
{
  byte[] data= = client.Receive(ref ipep);
}

但我的问题是我收到的任何数据包都不是按顺序排列的。我想按顺序接收它们。请帮助我。在此先感谢。

4

2 回答 2

5

UDP does not guarantee anything about the order of the data you send. It is "fire and forget". If you need to keep the data in an ordered stream, you need to use TCP.

Otherwise, you would need to implement some sort of sequence ID in your datagrams themselves.

于 2012-01-17T10:00:32.960 回答
1

此链接可能会有所帮助

http://www.codeproject.com/Articles/176722/Sending-messages-to-workstations-using-Socket-Prog/?display=PrintAll&fid=1618703&df=90&mpp=25&noise=3&prof=False&sort=Position&view=Quick

这个样本有 2 个部分。第一个是发送者(在某些电脑中),另一个是接收者(在客户端电脑中)。发送方中提到的端口应与接收方中的端口相同。

发件人应用程序中有一个文本框和按钮(发送)按钮。在各自的 PC 中运行这两个项目。

单击发送方中的发送按钮后,文本框中的数据将按顺序发送到接收方。

希望它有一些用处。谢谢你。

于 2012-01-17T10:36:15.553 回答