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. if (remoteEP == null)
  116. throw new ArgumentNullException ("remoteEP cannot be null");
  117. // Length of the array for receiving data??
  118. byte [] recBuffer;
  119. int available = socket.Available;
  120. recBuffer = new byte [1024]; // FIXME: any suggestions?
  121. EndPoint endPoint = (EndPoint) remoteEP;
  122. int dataRead = socket.ReceiveFrom (recBuffer, ref endPoint);
  123. if (dataRead < recBuffer.Length)
  124. return CutArray (recBuffer, dataRead);
  125. return recBuffer;
  126. } finally {
  127. CheckDisposed ();
  128. }
  129. }
  130. public int Send (byte [] dgram, int bytes)
  131. {
  132. try {
  133. if (dgram == null)
  134. throw new ArgumentNullException ("dgram is null");
  135. byte [] realDgram;
  136. if (dgram.Length <= bytes)
  137. realDgram = dgram;
  138. else
  139. realDgram = CutArray (dgram, (bytes >= dgram.Length) ? bytes : dgram.Length);
  140. // the socket should be connected already, so I use Send instead of SendTo
  141. return socket.Send (realDgram);
  142. } finally {
  143. CheckDisposed ();
  144. }
  145. }
  146. public int Send (byte [] dgram, int bytes, IPEndPoint endPoint)
  147. {
  148. try {
  149. if (dgram == null)
  150. throw new ArgumentNullException ("dgram is null");
  151. byte [] realDgram;
  152. if (dgram.Length <= bytes)
  153. realDgram = dgram;
  154. else
  155. realDgram = CutArray (dgram, (bytes >= dgram.Length) ? bytes : dgram.Length);
  156. // the socket should not be connected
  157. return socket.SendTo (realDgram, endPoint);
  158. } finally {
  159. CheckDisposed ();
  160. }
  161. }
  162. public int Send (byte [] dgram, int bytes, string hostname, int port)
  163. {
  164. return Send (dgram, bytes,
  165. new IPEndPoint (Dns.Resolve (hostname).AddressList [0], port));
  166. }
  167. private byte [] CutArray (byte [] orig, int length)
  168. {
  169. byte [] newArray = new byte [length];
  170. Array.Copy (orig, 0, newArray, 0, length);
  171. return newArray;
  172. }
  173. #endregion
  174. #region Properties
  175. protected bool Active {
  176. get { return active; }
  177. set { active = value; }
  178. }
  179. protected Socket Client {
  180. get { return socket; }
  181. set { socket = value; }
  182. }
  183. #endregion
  184. /*
  185. // commented because in the ms.net implementation these are not overriden. -- LP
  186. #region Overrides
  187. public override bool Equals (object obj)
  188. {
  189. if (obj is UdpClient)
  190. return (((UdpClient) obj).socket == socket &&
  191. ((UdpClient) obj).localEP == localEP);
  192. return false;
  193. }
  194. public override int GetHashCode ()
  195. {
  196. return (socket.GetHashCode () + localEP.GetHashCode () + (active ? 1 : 0));
  197. }
  198. #endregion
  199. */
  200. #region Disposing
  201. void IDisposable.Dispose ()
  202. {
  203. Dispose (true);
  204. GC.SuppressFinalize (this);
  205. }
  206. protected virtual void Dispose (bool disposing)
  207. {
  208. if (disposed)
  209. return;
  210. disposed = true;
  211. if (disposing) {
  212. // release managed resources
  213. localEP = null;
  214. }
  215. // release unmanaged resources
  216. Socket s = socket;
  217. socket = null;
  218. if (s != null)
  219. s.Close ();
  220. }
  221. ~UdpClient ()
  222. {
  223. Dispose (false);
  224. }
  225. private void CheckDisposed ()
  226. {
  227. if (disposed)
  228. throw new ObjectDisposedException (GetType().FullName);
  229. }
  230. #endregion
  231. #endregion
  232. }
  233. }