UDP provides an end-to-end service different from that of TCP.

In fact, UDP performs only two functions:

(1) it adds another layer of addressing (ports) to that of IP

(2) it detects data corruption that may occur in transit and discards any corrupted messages

Diffenence from TCP Sockets:

1. UDP sockets do not have to be connected before being used.

2. TCP is like telephone, and UDP is like email.

UDP Client

The typical UDP client goes through three steps:

1. Construct an instance of UdpClient, optionally specifying the local address and port.
2. Communicate by sending and receiving datagrams (byte arrays) using the Send() and
  Receive() methods of UdpClient.
3. When finished, deallocate the socket using the Close() method of UdpClient.

UdpEchoClient.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Zeus.Thunder.Core;
  7. using System.Net; // For IPEndPoint
  8. using System.Net.Sockets; // For UdpClient, SocketException
  9.  
  10. namespace SharpTrainer.NetWork
  11. {
  12. class UdpEchoClient : ITestCase
  13. {
  14. public void Run()
  15. {
  16. Console.Write("Input Server IP: ");
  17. string server = Console.ReadLine();
  18.  
  19. Console.Write("Input Server Port: ");
  20. int servPort = Int32.Parse(Console.ReadLine());
  21.  
  22. Console.Write("Input Echo String: ");
  23. byte[] sendPacket = Encoding.ASCII.GetBytes(Console.ReadLine());
  24.  
  25. // Create a UdpClient instance
  26. UdpClient client = new UdpClient();
  27. try
  28. {
  29. // Send the echo string to the specified host and port
  30. client.Send(sendPacket, sendPacket.Length, server, servPort);
  31. Console.WriteLine("Sent {0} bytes to the server...", sendPacket.Length);
  32.  
  33. // This IPEndPoint instance will be populated with the remote sender’s
  34. // endpoint information after the Receive() call
  35. IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, );
  36.  
  37. // Attempt echo reply receive
  38. byte[] rcvPacket = client.Receive(ref remoteIPEndPoint);
  39.  
  40. Console.WriteLine("Received {0} bytes from {1}: {2}", rcvPacket.Length, remoteIPEndPoint,
  41. Encoding.ASCII.GetString(rcvPacket, , rcvPacket.Length));
  42. }
  43. catch (SocketException se)
  44. {
  45. Console.WriteLine(se.ErrorCode + ": " + se.Message);
  46. }
  47.  
  48. client.Close();
  49. }
  50. }
  51. }

UdpEchoServer.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Zeus.Thunder.Core;
  6. using System.Net; // For IPEndPoint
  7. using System.Net.Sockets; // For UdpClient, SocketException
  8.  
  9. namespace SharpTrainer.NetWork
  10. {
  11. class UdpEchoServer : ITestCase
  12. {
  13. public void Run()
  14. {
  15. Console.Write("Input Server Port: ");
  16. int servPort = Int32.Parse(Console.ReadLine());
  17.  
  18. UdpClient client = null;
  19. try {
  20. // Create an instance of UdpClient on the port to listen on
  21. client = new UdpClient(servPort);
  22. } catch (SocketException se) {
  23. Console.WriteLine(se.ErrorCode + ": " + se.Message);
  24. Environment.Exit(se.ErrorCode);
  25. }
  26.  
  27. // Create an IPEndPoint instance that will be passed as a reference
  28. // to the Receive() call and be populated with the remote client info
  29. IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, );
  30.  
  31. for (;;)
  32. {
  33. // Run forever, receiving and echoing datagrams
  34. try {
  35. // Receive a byte array with echo datagram packet contents
  36. byte[] byteBuffer = client.Receive(ref remoteIPEndPoint);
  37. Console.Write("Handling client at " + remoteIPEndPoint + " - ");
  38. // Send an echo packet back to the client
  39. client.Send(byteBuffer, byteBuffer.Length, remoteIPEndPoint);
  40. Console.WriteLine("echoed {0} bytes.", byteBuffer.Length);
  41. } catch (SocketException se) {
  42. Console.WriteLine(se.ErrorCode + ": " + se.Message);
  43. }
  44. }
  45. }
  46. }
  47. }

运行结果:

Server端:

Handling client at 127.0.0.1:65005 - echoed 18 bytes.

Client端:

Input Server IP: 127.0.0.1
Input Server Port: 9999
Input Echo String: Hello Master HaKu!
Sent 18 bytes to the server...
Received 18 bytes from 127.0.0.1:9999: Hello Master HaKu!

UDP Sockets in C#的更多相关文章

  1. Windows UDP sockets: recvfrom() fails with error 10054

    https://stackoverflow.com/questions/34242622/windows-udp-sockets-recvfrom-fails-with-error-10054 #in ...

  2. 【Network】高性能 UDP 应该怎么做?

    参考资料: EPOLL-UDP-GOLANG golang udp epoll - Google 搜索 go - golang: working with multiple client/server ...

  3. 开源免费的C/C++网络库(c/c++ sockets library)

    (1)ACE 庞大.复杂,适合大型项目.开源.免费,不依赖第三方库,支持跨平台. http://www.cs.wustl.edu/~schmidt/ACE.html (2)Asio Asio基于Boo ...

  4. 开源免费的C/C++网络库(c/c++ sockets library)补充

    (1)ACE 庞大.复杂,适合大型项目.开源.免费,不依赖第三方库,支持跨平台. http://www.cs.wustl.edu/~schmidt/ACE.html (2)Asio Asio基于Boo ...

  5. 【RL-TCPnet网络教程】第17章 RL-TCPnet之UDP通信

    第17章      RL-TCPnet之UDP通信 本章节为大家讲解RL-TCPnet的UDP通信实现,学习本章节前,务必要优先学习第16章UDP用户数据报协议基础知识.有了这些基础知识之后,再搞本章 ...

  6. ss is one another utility to investigate sockets(特适合大规模tcp链接)

    原创文章,转载请注明: 转载自系统技术非业余研究 本文链接地址: ss is one another utility to investigate sockets(特适合大规模tcp链接) 具体的可以 ...

  7. 开源免费的C/C++网络库(c/c++ sockets library)(转)

    原文转自 http://blog.csdn.net/weiwangchao_/article/details/8730199 (1)ACE 庞大.复杂,适合大型项目.开源.免费,不依赖第三方库,支持跨 ...

  8. TCP/UDP server

    Simple: Sample TCP/UDP server https://msdn.microsoft.com/en-us/library/aa231754(v=vs.60).aspx Simple ...

  9. fuser - identify processes using files or sockets

    FUSER(1) User Commands FUSER(1) NAME fuser - identify processes using files or sockets SYNOPSIS fuse ...

随机推荐

  1. 解释Crypto模块怎么就这么"皮"?No module named "Crypto"

    https://www.cnblogs.com/fawaikuangtu123/p/9761943.html python版本:python3.6,系统:win7 1.pip install cryp ...

  2. 在eclipse中安装TestNG

    https://www.cnblogs.com/baixiaozheng/p/4989856.html 1.可借助Eclipse的Marketplace来安装TestNG Eclipse插件 a.打开 ...

  3. Android为什么需要广播Broadcast

       在Android系统中,为什么需要广播机制呢?广播机制,本质上它就是一种组件间的通信方式,如果是两个组件位于不同的进程当中,那么可以用Binder机制来实现,如果两个组件是在同一个进程中,那么它 ...

  4. Linux下Apache2.2和PHP5的安装配置

    Linux下Apache2.2和PHP5的安装配置 环境介绍 我安装使用的Linux版本为CentOS6.5最精简版,Apache为2.2.29,PHP版本为5.4.28. 系统安装 首先安装Cent ...

  5. Codeforces Round #301 (Div. 2) A. Combination Lock 暴力

    A. Combination Lock Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/p ...

  6. VS2015启动拷贝过来的项目无法启动IIS Express

    最近将VS2015开发的项目考给同事,告知无法启动,大概分析了一下原因: 1.查看端口是否占用冲突 2.在解决方案上右键选择,清理解决方案->重建解决方案 3.以上两个方法还不生效的话,在Web ...

  7. 参加SAP VT项目有感

    凡事预则立,不预则废. 没有接到录取电话还是有些悲伤的,虽然知道最终被录取的可能性不大,但是之前还是抱着一丝期望的,毕竟是自己的处女面,就这么以失败的结果结束了. 从最开始的投递简历,到后来的电话面试 ...

  8. dtrace for mysql

    http://dtrace.org/blogs/brendan/2011/06/23/mysql-performance-schema-and-dtrace/

  9. DTCC:MySQl核心代码开发经验揭示

    http://tech.it168.com/a2012/0413/1337/000001337236.shtml

  10. 实施CMMI3的体会

    公司从去年年底开始实施CMMI3,记得当初上培训课的时候,听着老师介绍过程管理,项目管理,工程过程,支持过程这四大类过程域的时候,全部门上下听得稀里糊涂,从未想到这个鬼东西还这么复杂,这么麻烦. 公司 ...