锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 英语翻译 / 基本UDP接收端和简单UDP接收程序
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。”需要全文内容也请联系孙老师。

基本UDP接收端和简单UDP接收程序

Introduction 介绍

As I am testing some equipment that sends data as UDP, I need to confirm that the data is actually received at the desired computer, and not blocked on the way.我测试的一些设备会发送UDP数据,我需要确认数据在所需的计算机实际收到了,而不是阻止了。

When I searched the web, I did not find a really simple application that helps me. They were either hardcoded with a fixed port, or too complex. Thus, I decided to program one myself, and I would like to share it with you.当我搜索网络的时候,我没有发现一个非常简单的应用程序,可以帮助我。 他们要么硬编码到固定端口,或过于复杂。 因此,我决定自己开发一个,分享给大家。

Summary 总结

I have implemented this as a console application. It includes three switches to set the local port, remote port, and the remote IP. If data is received with the specified setting, the data is converted to ASCII and printed in the console.我实现的是一个控制台应用程序实现。 它包括三个选项:设置本地端口,远程端口,远程IP。 如果指定的设置匹配的数据接收到了,数据转换为ASCII和打印到控制台上。

udp接收器

The screen dump shows the result after I have started receiving data from an AIS-receiver that is set up to send data, using UDP, to an IP-range that covers my computer.后屏幕转储显示了结果,结果来自于我已经从一个AIS-receiver接收的数据,此AIS-receiver设置发送数据,使用UDP,ip范围覆盖了我的电脑。

User Datagram Protocol 用户数据报协议

User Datagram Protocol (UDP) is a protocol for sending data on an IP-network. It is an alternative to the more commonly used Transmission Control Protocol (TCP). The major difference between the two, related to sending data, is that while TCP has handshaking, UDP has not. However, UDP is more efficient than TCP when sending the same data to multiple receivers.

用户数据报协议(UDP) 是一个ip网络协议发送数据。 它是另一种更常用传输控制协议(TCP)的代替 。 两者之间的主要区别,与发送数据,而TCP握手,UDP没有。 然而,UDP在处理发送相同的数据到多个接收者,比TCP更有效。

Choosing between TCP and UDP is often an issue when streaming data over an Ethernet. If it is a point-to-point application, where for instance a sensor is sending data to a logging application, TCP would guarantee that all data is received at the logging side. If multiple clients would like the same guaranteed service, a more advanced TCP server-client code is needed. Then the data is transmitted once for each client, and you get more network traffic. If there are multiple clients that tolerate some data loss, due to lack of handshaking, you can implement the communication using UDP. If the clients only listen to the stream, then it is far easier to use UDP for distributing one stream to many clients than using TCP, implementation wise.

选择TCP和UDP流数据时往往是一个问题在一个以太网。 如果它是一个点对点应用程序,例如传感器是将数据发送到日志应用程序中,TCP会保证所有的数据都是收到日志。 如果多个客户想相同的保障服务,需要一个更高级的TCP于服务器-客户机代码。 然后为每个客户,一旦传输数据,得到更多的网络流量。 如果有多个客户端,容忍一些数据丢失,由于缺乏握手,你可以使用UDP实现通信。 如果客户只听流,那么更容易使用UDP分发一个流比使用TCP很多客户,实现智慧。

There are many thoughts about UDP vs. TCP, but this is a quick summary of mine. 有许多关于UDP与TCP的想法,但是这是一个快速的总结我的。

Using the code 使用的代码

I've already mentioned that this is a simple application, and the following code sections will confirm this. The code does not include any exception handling or other hints to why data is not received.我已经提到过,这是一个简单的应用程序,和下面的代码部分将证实了这一点。 代码不包括任何异常处理或其他暗示为什么没有收到数据。
Setting the properties through switches 通过开关设置的属性
The properties are set at startup through switches. As an example: SimpleUdpReceiver -lp 1234 sets the local port to 1234. The code behind is a regular input argument switch:属性设置在启动时通过开关。 作为一个例子: SimpleUdpReceiver lp 1234 将本地端口设置为1234。 背后的代码是一个常规的输入参数开关: re

for( int i = 0; i < args.Length; i++)
      {
      string cmd = args[i];  // The current command switch
      string value; // The current value related to the command
      int tempInt;  // Temporary integer holder>
      IPAddress tempAddress; // Temporary ip holder

      switch (cmd)
      {
      case "-lp": // Local port
      value = GetValue(args, ref i);
      if (int.TryParse(value, out tempInt))
      localPort = tempInt;
      break;
      case "-rp": // Remote port
      value = GetValue(args, ref i);
      if (int.TryParse(value, out tempInt))
      remoteSender.Port = tempInt;
      break;
      case "-rh": // Remote ip
      value = GetValue(args, ref i);
      if (IPAddress.TryParse(value, out tempAddress))
      remoteSender.Address = tempAddress;
      else if (int.TryParse(value, out tempInt) && tempInt == 0)
      remoteSender.Address = IPAddress.Any;
      break;
      case "-?":  // Help text
      default:
      PrintHelpText();
      flag = true;
      break;
      }
      }

If some of the switches are wrong, the switch-statement defaults, and the applications terminate displaying the help text. If some of the values are not parsed, the default value is used. 如果一些开关是错误的, 开关 声明中违约,终止显示帮助文本和应用程序。 如果不解析的一些值,使用默认值。

Start receiving UDP packages 开始接收UDP包

Using the UdpClient, my job of implementing UDP is greatly simplified. The client is created using the set local port. Then the client and information of the remote endpoint is stored in the class UdpState. UdpState is just a container for the two variables, and copied from the help sites from Microsoft.

使用 UdpClient ,实现UDP是大大简化我的工作。 创建客户端使用本地端口设置。 然后客户端和远程端点的信息存储在类 UdpState 。 UdpState 只是一个容器的两个变量,和复制的 帮助从微软网站 。

// Create UDP client
      UdpClient client = new UdpClient(localPort);
      UdpState state = new UdpState(client, remoteSender);
      // Start async receiving
      client.BeginReceive(new AsyncCallback(DataReceived), state);

// Wait for any key to terminate application Console.ReadKey(); client.Close();

Handling received UDP packages 处理收到的UDP包

UdpClient receives data asynchronized when using BeginReceive(). The method DataReceived runs in the thread pool to handle incoming UPD packages. UdpClient 接收数据异步交流时使用 BeginReceive() 。 该方法 DataReceived 运行的线程池来处理传入的乌利希期刊指南包。

private static void DataReceived(IAsyncResult ar)
     {
      UdpClient c = (UdpClient)((UdpState)ar.AsyncState).c;     
   IPEndPoint wantedIpEndPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
    IPEndPoint receivedIpEndPoint = new IPEndPoint(IPAddress.Any, 0);    
    Byte[] receiveBytes = c.EndReceive(ar, ref receivedIpEndPoint);
     // Check sender
      bool isRightHost = (wantedIpEndPoint.Address.Equals(receivedIpEndPoint.Address) 
      || wantedIpEndPoint.Address.Equals(IPAddress.Any);
      bool isRightPort = (wantedIpEndPoint.Port == receivedIpEndPoint.Port)
      || wantedIpEndPoint.Port == 0;
      if (isRightHost && isRightPort)
      {
      // Convert data to ASCII and print in console
      string receivedText = ASCIIEncoding.ASCII.GetString(receiveBytes);
      Console.Write(receivedText);
      }
     // Restart listening for udp data packages
      c.BeginReceive(new AsyncCallback(DataReceived), ar.AsyncState);
      }

Points of Interest 兴趣点

UDP as a protocol is like "fire and forget", so I used Wireshark to check how many UDP packages I received with wrong checksum. And on the LAN I am connected to, with some 100 clients, I got a ~4% bad checksum rate when sending a package every second or so. Normally, I use TCP to transmit sensor data, but in some cases, I have receivers which need UDP and accepts that some data may not be received.

UDP协议是像“发射后不管”,所以我使用 Wireshark 检查我收到了多少UDP包,这些包带有错误校验和。 我连接到局域网,有100客户,会有 4%错误校验率,环境是每秒发送包。 通常情况下,我使用TCP传输敏感数据,但是在某些情况下,我有接收器需要UDP和承认,一些数据可能不被接受。

Often corporate firewalls stop UDP packages receiving the endpoint. But it may also be that the local port is in other use, causing conflict when trying to receive packages. 通常公司防火墙阻止UDP包接收端点。 但这也可能是本地端口使用,导致收包时冲突。

友情链接
版权所有 Copyright(c)2004-2015 锐英源软件 公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768 地址:郑州市文化路47号院1号楼4层(47-1楼位于文化路和红专路十字路口东北角,郑州大学工学院招待所南边,工学院科技报告厅西边。)