UdpClient.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // System.Net.Sockets.UdpClient.cs
  3. //
  4. // Author:
  5. // Gonzalo Paniagua Javier <[email protected]>
  6. //
  7. // Copyright (C) Ximian, Inc. http://www.ximian.com
  8. //
  9. using System;
  10. using System.Net;
  11. namespace System.Net.Sockets
  12. {
  13. public class UdpClient : IDisposable
  14. {
  15. private bool disposed = false;
  16. private bool active = false;
  17. private Socket socket;
  18. private IPEndPoint localEP;
  19. #region Constructors
  20. public UdpClient ()
  21. {
  22. localEP = new IPEndPoint (IPAddress.Any, 0);
  23. InitSocket ();
  24. }
  25. public UdpClient (int port)
  26. {
  27. // IPEndPoint throws ArgumentException when port is invalid
  28. localEP = new IPEndPoint (IPAddress.Any, port);
  29. InitSocket ();
  30. }
  31. public UdpClient (IPEndPoint localEP)
  32. {
  33. if (localEP == null)
  34. throw new ArgumentNullException ("IPEndPoint cannot be null");
  35. this.localEP = localEP;
  36. InitSocket ();
  37. }
  38. public UdpClient (string hostname, int port)
  39. {
  40. if (hostname == null)
  41. throw new ArgumentNullException ("hostname cannot be null");
  42. if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
  43. throw new ArgumentException ("Invalid port");
  44. localEP = new IPEndPoint (IPAddress.Any, 0);
  45. InitSocket ();
  46. Connect (hostname, port);
  47. }
  48. private void InitSocket ()
  49. {
  50. active = false;
  51. socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  52. socket.Bind (localEP);
  53. }
  54. #endregion // Constructors
  55. #region Public methods
  56. #region Close
  57. public void Close ()
  58. {
  59. ((IDisposable) this).Dispose ();
  60. }
  61. #endregion
  62. #region Connect
  63. public void Connect (IPEndPoint endPoint)
  64. {
  65. try {
  66. socket.Connect (endPoint);
  67. active = true;
  68. } finally {
  69. CheckDisposed ();
  70. }
  71. }
  72. public void Connect (IPAddress addr, int port)
  73. {
  74. Connect (new IPEndPoint (addr, port));
  75. }
  76. public void Connect (string hostname, int port)
  77. {
  78. Connect (new IPEndPoint (Dns.Resolve (hostname).AddressList [0], port));
  79. }
  80. #endregion
  81. #region Multicast methods
  82. public void DropMulticastGroup (IPAddress multicastAddr)
  83. {
  84. try {
  85. socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
  86. new MulticastOption (multicastAddr));
  87. } finally {
  88. CheckDisposed ();
  89. }
  90. }
  91. public void JoinMulticastGroup (IPAddress multicastAddr)
  92. {
  93. try {
  94. socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  95. new MulticastOption (multicastAddr));
  96. } finally {
  97. CheckDisposed ();
  98. }
  99. }
  100. public void JoinMulticastGroup (IPAddress multicastAddr, int timeToLive)
  101. {
  102. JoinMulticastGroup (multicastAddr);
  103. try {
  104. socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive,
  105. timeToLive);
  106. } finally {
  107. CheckDisposed ();
  108. }
  109. }
  110. #endregion
  111. #region Data I/O
  112. public byte [] Receive (ref IPEndPoint remoteEP)
  113. {
  114. try {
  115. // Length of the array for receiving data??
  116. byte [] recBuffer;
  117. int available = socket.Available;
  118. if (available < 512)
  119. available = 512;
  120. recBuffer = new byte [available];
  121. EndPoint endPoint = new IPEndPoint (IPAddress.Any, 0);
  122. int dataRead = socket.ReceiveFrom (recBuffer, ref endPoint);
  123. if (dataRead < recBuffer.Length)
  124. recBuffer = CutArray (recBuffer, dataRead);
  125. remoteEP = (IPEndPoint) endPoint;
  126. return recBuffer;
  127. } finally {
  128. CheckDisposed ();
  129. }
  130. }
  131. public int Send (byte [] dgram, int bytes)
  132. {
  133. try {
  134. if (dgram == null)
  135. throw new ArgumentNullException ("dgram is null");
  136. byte [] realDgram;
  137. if (dgram.Length <= bytes)
  138. realDgram = dgram;
  139. else
  140. realDgram = CutArray (dgram, (bytes >= dgram.Length) ? bytes : dgram.Length);
  141. // the socket should be connected already, so I use Send instead of SendTo
  142. return socket.Send (realDgram);
  143. } finally {
  144. CheckDisposed ();
  145. }
  146. }
  147. public int Send (byte [] dgram, int bytes, IPEndPoint endPoint)
  148. {
  149. try {
  150. if (dgram == null)
  151. throw new ArgumentNullException ("dgram is null");
  152. byte [] realDgram;
  153. if (dgram.Length <= bytes)
  154. realDgram = dgram;
  155. else
  156. realDgram = CutArray (dgram, (bytes >= dgram.Length) ? bytes : dgram.Length);
  157. // the socket should not be connected
  158. return socket.SendTo (realDgram, endPoint);
  159. } finally {
  160. CheckDisposed ();
  161. }
  162. }
  163. public int Send (byte [] dgram, int bytes, string hostname, int port)
  164. {
  165. return Send (dgram, bytes,
  166. new IPEndPoint (Dns.Resolve (hostname).AddressList [0], port));
  167. }
  168. private byte [] CutArray (byte [] orig, int length)
  169. {
  170. byte [] newArray = new byte [length];
  171. Array.Copy (orig, 0, newArray, 0, length);
  172. return newArray;
  173. }
  174. #endregion
  175. #region Properties
  176. protected bool Active {
  177. get { return active; }
  178. set { active = value; }
  179. }
  180. protected Socket Client {
  181. get { return socket; }
  182. set { socket = value; }
  183. }
  184. #endregion
  185. /*
  186. // commented because in the ms.net implementation these are not overriden. -- LP
  187. #region Overrides
  188. public override bool Equals (object obj)
  189. {
  190. if (obj is UdpClient)
  191. return (((UdpClient) obj).socket == socket &&
  192. ((UdpClient) obj).localEP == localEP);
  193. return false;
  194. }
  195. public override int GetHashCode ()
  196. {
  197. return (socket.GetHashCode () + localEP.GetHashCode () + (active ? 1 : 0));
  198. }
  199. #endregion
  200. */
  201. #region Disposing
  202. void IDisposable.Dispose ()
  203. {
  204. Dispose (true);
  205. GC.SuppressFinalize (this);
  206. }
  207. void Dispose (bool disposing)
  208. {
  209. if (disposed)
  210. return;
  211. disposed = true;
  212. if (disposing) {
  213. // release managed resources
  214. localEP = null;
  215. }
  216. // release unmanaged resources
  217. Socket s = socket;
  218. socket = null;
  219. if (s != null)
  220. s.Close ();
  221. }
  222. ~UdpClient ()
  223. {
  224. Dispose (false);
  225. }
  226. private void CheckDisposed ()
  227. {
  228. if (disposed)
  229. throw new ObjectDisposedException (GetType().FullName);
  230. }
  231. #endregion
  232. #endregion
  233. }
  234. }