UdpClient.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. //
  2. // System.Net.Sockets.UdpClient.cs
  3. //
  4. // Author:
  5. // Gonzalo Paniagua Javier <[email protected]>
  6. // Sridhar Kulkarni ([email protected])
  7. //
  8. // Copyright (C) Ximian, Inc. http://www.ximian.com
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Net;
  32. namespace System.Net.Sockets
  33. {
  34. public class UdpClient : IDisposable
  35. {
  36. private bool disposed = false;
  37. private bool active = false;
  38. private Socket socket;
  39. private AddressFamily family = AddressFamily.InterNetwork;
  40. #if NET_2_0
  41. private byte[] recvbuffer;
  42. #endif
  43. #region Constructors
  44. public UdpClient () : this(AddressFamily.InterNetwork)
  45. {
  46. }
  47. #if NET_1_1
  48. public UdpClient(AddressFamily family)
  49. {
  50. if(family != AddressFamily.InterNetwork && family != AddressFamily.InterNetworkV6)
  51. throw new ArgumentException("Family must be InterNetwork or InterNetworkV6", "family");
  52. this.family = family;
  53. InitSocket (null);
  54. }
  55. #endif
  56. public UdpClient (int port)
  57. {
  58. if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
  59. throw new ArgumentOutOfRangeException ("port");
  60. this.family = AddressFamily.InterNetwork;
  61. IPEndPoint localEP = new IPEndPoint (IPAddress.Any, port);
  62. InitSocket (localEP);
  63. }
  64. public UdpClient (IPEndPoint localEP)
  65. {
  66. if (localEP == null)
  67. throw new ArgumentNullException ("localEP");
  68. this.family = localEP.AddressFamily;
  69. InitSocket (localEP);
  70. }
  71. #if NET_1_1
  72. public UdpClient (int port, AddressFamily family)
  73. {
  74. if (family != AddressFamily.InterNetwork &&
  75. family != AddressFamily.InterNetworkV6) {
  76. throw new ArgumentException ("Family must be InterNetwork or InterNetworkV6", "family");
  77. }
  78. if (port < IPEndPoint.MinPort ||
  79. port > IPEndPoint.MaxPort) {
  80. throw new ArgumentOutOfRangeException ("port");
  81. }
  82. this.family = family;
  83. IPEndPoint localEP = new IPEndPoint (IPAddress.Any, port);
  84. InitSocket (localEP);
  85. }
  86. #endif
  87. public UdpClient (string hostname, int port)
  88. {
  89. if (hostname == null)
  90. throw new ArgumentNullException ("hostname");
  91. if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
  92. throw new ArgumentOutOfRangeException ("port");
  93. InitSocket (null);
  94. Connect (hostname, port);
  95. }
  96. private void InitSocket (EndPoint localEP)
  97. {
  98. if(socket != null) {
  99. socket.Close();
  100. socket = null;
  101. }
  102. socket = new Socket (family, SocketType.Dgram, ProtocolType.Udp);
  103. if (localEP != null)
  104. socket.Bind (localEP);
  105. }
  106. #endregion // Constructors
  107. #region Public methods
  108. #region Close
  109. public void Close ()
  110. {
  111. ((IDisposable) this).Dispose ();
  112. }
  113. #endregion
  114. #region Connect
  115. void DoConnect (IPEndPoint endPoint)
  116. {
  117. /* Catch EACCES and turn on SO_BROADCAST then,
  118. * as UDP sockets don't have it set by default
  119. */
  120. try {
  121. socket.Connect (endPoint);
  122. } catch (SocketException ex) {
  123. if (ex.ErrorCode == (int)SocketError.AccessDenied) {
  124. socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  125. socket.Connect (endPoint);
  126. } else {
  127. throw;
  128. }
  129. }
  130. }
  131. public void Connect (IPEndPoint endPoint)
  132. {
  133. CheckDisposed ();
  134. if (endPoint == null)
  135. throw new ArgumentNullException ("endPoint");
  136. DoConnect (endPoint);
  137. active = true;
  138. }
  139. public void Connect (IPAddress addr, int port)
  140. {
  141. if (addr == null)
  142. throw new ArgumentNullException ("addr");
  143. if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
  144. throw new ArgumentOutOfRangeException ("port");
  145. Connect (new IPEndPoint (addr, port));
  146. }
  147. public void Connect (string hostname, int port)
  148. {
  149. if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
  150. throw new ArgumentOutOfRangeException ("port");
  151. IPAddress[] addresses = Dns.Resolve (hostname).AddressList;
  152. for(int i=0; i<addresses.Length; i++) {
  153. try {
  154. this.family = addresses[i].AddressFamily;
  155. Connect (new IPEndPoint (addresses[i], port));
  156. break;
  157. } catch(Exception e) {
  158. if(i == addresses.Length - 1){
  159. if(socket != null) {
  160. socket.Close();
  161. socket = null;
  162. }
  163. /// This is the last entry, re-throw the exception
  164. throw e;
  165. }
  166. }
  167. }
  168. }
  169. #endregion
  170. #region Multicast methods
  171. public void DropMulticastGroup (IPAddress multicastAddr)
  172. {
  173. CheckDisposed ();
  174. if (multicastAddr == null)
  175. throw new ArgumentNullException ("multicastAddr");
  176. if(family == AddressFamily.InterNetwork)
  177. socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
  178. new MulticastOption (multicastAddr));
  179. #if NET_1_1
  180. else
  181. socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
  182. new IPv6MulticastOption (multicastAddr));
  183. #endif
  184. }
  185. #if NET_1_1
  186. public void DropMulticastGroup (IPAddress multicastAddr,
  187. int ifindex)
  188. {
  189. CheckDisposed ();
  190. /* LAMESPEC: exceptions haven't been specified
  191. * for this overload.
  192. */
  193. if (multicastAddr == null) {
  194. throw new ArgumentNullException ("multicastAddr");
  195. }
  196. /* Does this overload only apply to IPv6?
  197. * Only the IPv6MulticastOption has an
  198. * ifindex-using constructor. The MS docs
  199. * don't say.
  200. */
  201. if (family == AddressFamily.InterNetworkV6) {
  202. socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership, new IPv6MulticastOption (multicastAddr, ifindex));
  203. }
  204. }
  205. #endif
  206. public void JoinMulticastGroup (IPAddress multicastAddr)
  207. {
  208. CheckDisposed ();
  209. if(family == AddressFamily.InterNetwork)
  210. socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  211. new MulticastOption (multicastAddr));
  212. #if NET_1_1
  213. else
  214. socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  215. new IPv6MulticastOption (multicastAddr));
  216. #endif
  217. }
  218. #if NET_1_1
  219. public void JoinMulticastGroup (int ifindex,
  220. IPAddress multicastAddr)
  221. {
  222. CheckDisposed ();
  223. /* Does this overload only apply to IPv6?
  224. * Only the IPv6MulticastOption has an
  225. * ifindex-using constructor. The MS docs
  226. * don't say.
  227. */
  228. if (family == AddressFamily.InterNetworkV6) {
  229. socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption (multicastAddr, ifindex));
  230. }
  231. }
  232. #endif
  233. public void JoinMulticastGroup (IPAddress multicastAddr, int timeToLive)
  234. {
  235. CheckDisposed ();
  236. JoinMulticastGroup (multicastAddr);
  237. if (timeToLive < 0 || timeToLive > 255)
  238. throw new ArgumentOutOfRangeException ("timeToLive");
  239. if(family == AddressFamily.InterNetwork)
  240. socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive,
  241. timeToLive);
  242. #if NET_1_1
  243. else
  244. socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.MulticastTimeToLive,
  245. timeToLive);
  246. #endif
  247. }
  248. #if NET_2_0
  249. public void JoinMulticastGroup (IPAddress multicastAddr,
  250. IPAddress localAddress)
  251. {
  252. CheckDisposed ();
  253. if (family == AddressFamily.InterNetwork) {
  254. socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption (multicastAddr, localAddress));
  255. } else if (family == AddressFamily.InterNetworkV6) {
  256. socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new MulticastOption (multicastAddr, localAddress));
  257. }
  258. }
  259. #endif
  260. #endregion
  261. #region Data I/O
  262. public byte [] Receive (ref IPEndPoint remoteEP)
  263. {
  264. CheckDisposed ();
  265. // Bug 45633: the spec states that we should block until a datagram arrives:
  266. // remove the 512 hardcoded value.
  267. // Block until we get it.
  268. socket.Poll (-1, SelectMode.SelectRead);
  269. byte [] recBuffer;
  270. int available = socket.Available;
  271. recBuffer = new byte [available];
  272. EndPoint endPoint = new IPEndPoint (IPAddress.Any, 0);
  273. int dataRead = socket.ReceiveFrom (recBuffer, ref endPoint);
  274. if (dataRead < recBuffer.Length)
  275. recBuffer = CutArray (recBuffer, dataRead);
  276. remoteEP = (IPEndPoint) endPoint;
  277. return recBuffer;
  278. }
  279. int DoSend (byte[] dgram, int bytes, IPEndPoint endPoint)
  280. {
  281. /* Catch EACCES and turn on SO_BROADCAST then,
  282. * as UDP sockets don't have it set by default
  283. */
  284. try {
  285. if (endPoint == null) {
  286. return(socket.Send (dgram, 0, bytes,
  287. SocketFlags.None));
  288. } else {
  289. return(socket.SendTo (dgram, 0, bytes,
  290. SocketFlags.None,
  291. endPoint));
  292. }
  293. } catch (SocketException ex) {
  294. if (ex.ErrorCode == (int)SocketError.AccessDenied) {
  295. socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  296. if (endPoint == null) {
  297. return(socket.Send (dgram, 0, bytes, SocketFlags.None));
  298. } else {
  299. return(socket.SendTo (dgram, 0, bytes, SocketFlags.None, endPoint));
  300. }
  301. } else {
  302. throw;
  303. }
  304. }
  305. }
  306. public int Send (byte [] dgram, int bytes)
  307. {
  308. CheckDisposed ();
  309. if (dgram == null)
  310. throw new ArgumentNullException ("dgram");
  311. if (!active)
  312. throw new InvalidOperationException ("Operation not allowed on " +
  313. "non-connected sockets.");
  314. return(DoSend (dgram, bytes, null));
  315. }
  316. public int Send (byte [] dgram, int bytes, IPEndPoint endPoint)
  317. {
  318. CheckDisposed ();
  319. if (dgram == null)
  320. throw new ArgumentNullException ("dgram is null");
  321. if (active) {
  322. if (endPoint != null)
  323. throw new InvalidOperationException ("Cannot send packets to an " +
  324. "arbitrary host while connected.");
  325. return(DoSend (dgram, bytes, null));
  326. }
  327. return(DoSend (dgram, bytes, endPoint));
  328. }
  329. public int Send (byte [] dgram, int bytes, string hostname, int port)
  330. {
  331. return Send (dgram, bytes,
  332. new IPEndPoint (Dns.Resolve (hostname).AddressList [0], port));
  333. }
  334. private byte [] CutArray (byte [] orig, int length)
  335. {
  336. byte [] newArray = new byte [length];
  337. Buffer.BlockCopy (orig, 0, newArray, 0, length);
  338. return newArray;
  339. }
  340. #endregion
  341. #if NET_2_0
  342. IAsyncResult DoBeginSend (byte[] datagram, int bytes,
  343. IPEndPoint endPoint,
  344. AsyncCallback requestCallback,
  345. object state)
  346. {
  347. /* Catch EACCES and turn on SO_BROADCAST then,
  348. * as UDP sockets don't have it set by default
  349. */
  350. try {
  351. if (endPoint == null) {
  352. return(socket.BeginSend (datagram, 0, bytes, SocketFlags.None, requestCallback, state));
  353. } else {
  354. return(socket.BeginSendTo (datagram, 0, bytes, SocketFlags.None, endPoint, requestCallback, state));
  355. }
  356. } catch (SocketException ex) {
  357. if (ex.ErrorCode == (int)SocketError.AccessDenied) {
  358. socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  359. if (endPoint == null) {
  360. return(socket.BeginSend (datagram, 0, bytes, SocketFlags.None, requestCallback, state));
  361. } else {
  362. return(socket.BeginSendTo (datagram, 0, bytes, SocketFlags.None, endPoint, requestCallback, state));
  363. }
  364. } else {
  365. throw;
  366. }
  367. }
  368. }
  369. public IAsyncResult BeginSend (byte[] datagram, int bytes,
  370. AsyncCallback requestCallback,
  371. object state)
  372. {
  373. return(BeginSend (datagram, bytes, null,
  374. requestCallback, state));
  375. }
  376. public IAsyncResult BeginSend (byte[] datagram, int bytes,
  377. IPEndPoint endPoint,
  378. AsyncCallback requestCallback,
  379. object state)
  380. {
  381. CheckDisposed ();
  382. if (datagram == null) {
  383. throw new ArgumentNullException ("datagram");
  384. }
  385. return(DoBeginSend (datagram, bytes, endPoint,
  386. requestCallback, state));
  387. }
  388. public IAsyncResult BeginSend (byte[] datagram, int bytes,
  389. string hostname, int port,
  390. AsyncCallback requestCallback,
  391. object state)
  392. {
  393. return(BeginSend (datagram, bytes, new IPEndPoint (Dns.Resolve (hostname).AddressList[0], port), requestCallback, state));
  394. }
  395. public int EndSend (IAsyncResult asyncResult)
  396. {
  397. CheckDisposed ();
  398. if (asyncResult == null) {
  399. throw new ArgumentNullException ("asyncResult is a null reference");
  400. }
  401. return(socket.EndSend (asyncResult));
  402. }
  403. public IAsyncResult BeginReceive (AsyncCallback callback,
  404. object state)
  405. {
  406. CheckDisposed ();
  407. recvbuffer = new byte[8192];
  408. EndPoint ep;
  409. if (family == AddressFamily.InterNetwork) {
  410. ep = new IPEndPoint (IPAddress.Any, 0);
  411. } else {
  412. ep = new IPEndPoint (IPAddress.IPv6Any, 0);
  413. }
  414. return(socket.BeginReceiveFrom (recvbuffer, 0, 8192,
  415. SocketFlags.None,
  416. ref ep,
  417. callback, state));
  418. }
  419. public byte[] EndReceive (IAsyncResult asyncResult,
  420. ref IPEndPoint remoteEP)
  421. {
  422. CheckDisposed ();
  423. if (asyncResult == null) {
  424. throw new ArgumentNullException ("asyncResult is a null reference");
  425. }
  426. EndPoint ep;
  427. if (family == AddressFamily.InterNetwork) {
  428. ep = new IPEndPoint (IPAddress.Any, 0);
  429. } else {
  430. ep = new IPEndPoint (IPAddress.IPv6Any, 0);
  431. }
  432. int bytes = socket.EndReceiveFrom (asyncResult,
  433. ref ep);
  434. remoteEP = (IPEndPoint)ep;
  435. /* Need to copy into a new array here, because
  436. * otherwise the returned array length is not
  437. * 'bytes'
  438. */
  439. byte[] buf = new byte[bytes];
  440. Array.Copy (recvbuffer, buf, bytes);
  441. return(buf);
  442. }
  443. #endif
  444. #region Properties
  445. protected bool Active {
  446. get { return active; }
  447. set { active = value; }
  448. }
  449. #if NET_2_0
  450. public
  451. #else
  452. protected
  453. #endif
  454. Socket Client {
  455. get { return socket; }
  456. set { socket = value; }
  457. }
  458. #if NET_2_0
  459. public int Available
  460. {
  461. get {
  462. return(socket.Available);
  463. }
  464. }
  465. #if TARGET_JVM
  466. [MonoNotSupported ("Not supported as Socket.DontFragment is not supported")]
  467. #endif
  468. public bool DontFragment
  469. {
  470. get {
  471. return(socket.DontFragment);
  472. }
  473. set {
  474. socket.DontFragment = value;
  475. }
  476. }
  477. #if TARGET_JVM
  478. [MonoNotSupported ("Not supported as Socket.EnableBroadcast is not supported")]
  479. #endif
  480. public bool EnableBroadcast
  481. {
  482. get {
  483. return(socket.EnableBroadcast);
  484. }
  485. set {
  486. socket.EnableBroadcast = value;
  487. }
  488. }
  489. #if TARGET_JVM
  490. [MonoNotSupported ("Not supported as Socket.ExclusiveAddressUse is not supported")]
  491. #endif
  492. public bool ExclusiveAddressUse
  493. {
  494. get {
  495. return(socket.ExclusiveAddressUse);
  496. }
  497. set {
  498. socket.ExclusiveAddressUse = value;
  499. }
  500. }
  501. #if TARGET_JVM
  502. [MonoNotSupported ("Not supported as Socket.MulticastLoopback is not supported")]
  503. #endif
  504. public bool MulticastLoopback
  505. {
  506. get {
  507. return(socket.MulticastLoopback);
  508. }
  509. set {
  510. socket.MulticastLoopback = value;
  511. }
  512. }
  513. #if TARGET_JVM
  514. [MonoNotSupported ("Not supported as Socket.Ttl is not supported")]
  515. #endif
  516. public short Ttl
  517. {
  518. get {
  519. return(socket.Ttl);
  520. }
  521. set {
  522. socket.Ttl = value;
  523. }
  524. }
  525. #endif
  526. #endregion
  527. #region Disposing
  528. void IDisposable.Dispose ()
  529. {
  530. Dispose (true);
  531. GC.SuppressFinalize (this);
  532. }
  533. #if NET_2_0
  534. protected virtual
  535. #endif
  536. void Dispose (bool disposing)
  537. {
  538. if (disposed)
  539. return;
  540. disposed = true;
  541. if (disposing){
  542. if (socket != null)
  543. socket.Close ();
  544. socket = null;
  545. }
  546. }
  547. ~UdpClient ()
  548. {
  549. Dispose (false);
  550. }
  551. private void CheckDisposed ()
  552. {
  553. if (disposed)
  554. throw new ObjectDisposedException (GetType().FullName);
  555. }
  556. #endregion
  557. #endregion
  558. }
  559. }