UdpHelper.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace ToolsClassLibrary
  10. {
  11. /// <summary>
  12. /// UDP传输类
  13. /// </summary>
  14. public class UdpHelper
  15. {
  16. #region 容器对象
  17. /// <summary>
  18. /// 容器对象
  19. /// </summary>
  20. public class StateObject
  21. {
  22. //服务器端
  23. public Socket udpServer = null;
  24. //接受数据缓冲区
  25. public byte[] buffer = new byte[1024];
  26. //远程终端
  27. public EndPoint remoteEP;
  28. }
  29. public StateObject state;
  30. public void Close()
  31. {
  32. state.udpServer.Close();
  33. state.udpServer.Dispose();
  34. }
  35. #endregion
  36. #region 服务器广播发送端
  37. public void ServerBind(string ipAddress, int port)
  38. {
  39. try
  40. {
  41. state = new StateObject();
  42. state.udpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  43. state.udpServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  44. if (ipAddress == "0.0.0.0")
  45. {
  46. state.remoteEP = new IPEndPoint(IPAddress.Broadcast, port);
  47. }
  48. else
  49. {
  50. state.remoteEP = new IPEndPoint(IPAddress.Parse(ipAddress), port);
  51. }
  52. }
  53. catch (Exception e)
  54. {
  55. Console.WriteLine(e.Message.ToString());
  56. }
  57. }
  58. public void SendTo(byte[] dataString)
  59. {
  60. try
  61. {
  62. state.udpServer.SendTo(dataString, state.remoteEP);
  63. }
  64. catch (Exception e)
  65. {
  66. Console.WriteLine(e.Message.ToString());
  67. }
  68. }
  69. #endregion
  70. #region 客户接收端
  71. public void ClientBind()
  72. {
  73. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  74. string ip = "";
  75. for (int i = 0; i != IpEntry.AddressList.Length; i++)
  76. {
  77. if (!IpEntry.AddressList[i].IsIPv6LinkLocal
  78. && !IpEntry.AddressList[i].IsIPv6Multicast
  79. && !IpEntry.AddressList[i].IsIPv6SiteLocal
  80. && !IpEntry.AddressList[i].IsIPv6Teredo)
  81. {
  82. ip = IpEntry.AddressList[i].ToString();
  83. }
  84. }
  85. Console.WriteLine("客户端Udp模式");
  86. state = new StateObject();
  87. state.udpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  88. state.udpServer.Bind(new IPEndPoint(IPAddress.Parse(ip), 8787));
  89. state.remoteEP = new IPEndPoint(IPAddress.Any, 0);
  90. }
  91. public byte[] Receive()
  92. {
  93. state.buffer = new byte[1024*1024];
  94. try
  95. {
  96. int length = state.udpServer.ReceiveFrom(state.buffer, ref state.remoteEP);
  97. }
  98. catch (Exception ex)
  99. {
  100. Console.WriteLine(string.Format("出现异常:{0}", ex.Message));
  101. }
  102. return state.buffer;
  103. }
  104. #endregion
  105. }
  106. }