Tag: udpclient

c#中的UDP客户端速率控制

我连续发送多个udp数据包到远程PC。 问题是,如果数据量太高,某些设备之间的某个通道会遇到缓冲区溢出。 我打算限制/限制/控制udp数据包的发送速率。 谁能给我一些关于如何找到最佳费率发送间隔的指导? 顺便说一句,请停止建议tcp over udp。 目标不是可靠地发送数据,而是测量最大吞吐量。

UdpClient.ReceiveAsync正确提前终止

美好的一天。 我使用UdpClient并使用它包装。 为了阅读我有异步方法: private async Task Receive(UdpClient client, CancellationToken breakToken) { // Выход из async, если произошёл CancellationRequest breakToken.ThrowIfCancellationRequested(); UdpReceiveResult result; try { result = await client.ReceiveAsync().WithCancellation(breakToken); } catch(OperationCanceledException) { // Штатная ситуация ручной остановки Task-а } return result.Buffer; } WithCancellation是我提前终止的扩展方法: public static async Task WithCancellation( this Task task, CancellationToken cancellationToken) { var tcs = […]

C#应用程序没有在UDPClient.Receive上接收数据包

我遇到了一个好奇的问题,我似乎无法调试。 我的应用程序从通过特定端口发送UDP数据包的设备收到数据包。 设置UDP侦听器后,while循环会定期触发Receive命令。 我应该在每个给定的时间间隔收到400个值,我甚至设置了一个过程来确保这些值正在通过。 以下是相关代码的片段: public UdpClient listener; IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort); //where listenPort is an int holding the port values should be received from listener.ExclusiveAddressUse = false; listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); listener.Client.Bind(groupEP); if (listener.Client.Connected) { listener.Connect(groupEP); } //I’ve actually never seen the app actually enter the code contained above try { while (!done) […]

如何指定UdpPacket的源端口?

我想将UdpPacket发送到特定的远程主机(我已经知道公共IP和端口)。 我想使用C#的UdpClient类。 static int Main() { UdpClient client = new UdpClient(); IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(“1.2.3.4”), 9999); byte[] data = GetData(); client.Send(data, data.Length, remoteEP); } 发送数据包时,UdpClient会自动选择可用端口。 我想手动设置端口,从中发送数据包。 感谢您的帮助!

localhost上UDP包丢失的原因?

在我的WPF 4.0应用程序中,我有一个UDP监听器,如下所示。 在我的Windows 7 PC上,我在localhost上运行服务器和客户端。 每个接收的数据报都是较大位图的扫描线,因此在接收到所有扫描线之后, UI线程上会显示位图。 这似乎有效。 但是,偶尔会丢失1-50%的扫描线。 我希望这在弱网络连接上,但在本地运行时不会。 使用以下代码可能导致UDP程序包丢失的原因是什么? IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, PORT); udpClient = new UdpClient(endPoint); udpClient.Client.ReceiveBufferSize = 65535; // I’ve tried many different sizes… var status = new UdpStatus() { u = udpClient, e = endPoint }; udpClient.BeginReceive(new AsyncCallback(UdpCallback), status); private void UdpCallback(IAsyncResult ar) { IPEndPoint endPoint = ((UdpStatus)(ar.AsyncState)).e; […]

UDP:从所有网络接口读取数据

我有以下代码来读取来自网络的多播消息,用于指定的IP +端口 private static void ReceiveMessages(int port, string ip, CancellationToken token) { Task.Factory.StartNew(() => { using (var mUdpClientReceiver = new UdpClient()) { var mReceivingEndPoint = new IPEndPoint(IPAddress.Any, port); mUdpClientReceiver.ExclusiveAddressUse = false; mUdpClientReceiver.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); mUdpClientReceiver.ExclusiveAddressUse = false; mUdpClientReceiver.Client.Bind(mReceivingEndPoint); mUdpClientReceiver.JoinMulticastGroup(IPAddress.Parse(ip), 255); while (!token.IsCancellationRequested) { byte[] receive = mUdpClientReceiver.Receive(ref mReceivingEndPoint); Console.WriteLine(“Message received from {0} “,mReceivingEndPoint); } } […]

使用udpClient连续接收消息

我正在寻找通过C# UdpClient类接收和处理消息的最佳解决方案。 有人有任何解决方案吗?

如果我在c#中发送0个有效载荷数据,udp数据包的大小是多少?

我已经找出了使用udp的2个端点之间的碎片之前的最大数据是1472(其他端点可能不同)。 这表明mtu是1500字节,每个数据包的头开销是28bytes。可以安全地假设如果我发送0字节数据(有效载荷),传输的实际数据是28字节? 我正在做一些bencchmark,所以对我来说知道频道中发生了什么至关重要。 谢谢。

将UDP消息广播到所有可用的网卡

我需要向特定的IP和端口发送UDP消息。 由于有3张网卡, 10.1.xx 10.2.xx 10.4.xx 当我发送UDP消息时,我只在一个网络适配器中收到消息…其余的ip没有收到。 我想在发送消息时检查网络适配器。 我怎样才能做到这一点? 目前我使用以下内容: IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(LocalIP), 0); IPEndPoint targetEndPoint = new IPEndPoint(TargetIP, iTargetPort); UdpClient sendUdpClient = new UdpClient(localEndPoint); int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);