UdpClient.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. #if NET_2_0
  52. throw new ArgumentException ("Family must be InterNetwork or InterNetworkV6", "family");
  53. #else
  54. throw new ArgumentException ("family");
  55. #endif
  56. this.family = family;
  57. InitSocket (null);
  58. }
  59. #endif
  60. public UdpClient (int port)
  61. {
  62. if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
  63. throw new ArgumentOutOfRangeException ("port");
  64. this.family = AddressFamily.InterNetwork;
  65. IPEndPoint localEP = new IPEndPoint (IPAddress.Any, port);
  66. InitSocket (localEP);
  67. }
  68. public UdpClient (IPEndPoint localEP)
  69. {
  70. if (localEP == null)
  71. throw new ArgumentNullException ("localEP");
  72. this.family = localEP.AddressFamily;
  73. InitSocket (localEP);
  74. }
  75. #if NET_1_1
  76. public UdpClient (int port, AddressFamily family)
  77. {
  78. if (family != AddressFamily.InterNetwork && family != AddressFamily.InterNetworkV6)
  79. #if NET_2_0
  80. throw new ArgumentException ("Family must be InterNetwork or InterNetworkV6", "family");
  81. #else
  82. throw new ArgumentException ("family");
  83. #endif
  84. if (port < IPEndPoint.MinPort ||
  85. port > IPEndPoint.MaxPort) {
  86. throw new ArgumentOutOfRangeException ("port");
  87. }
  88. this.family = family;
  89. IPEndPoint localEP;
  90. if (family == AddressFamily.InterNetwork)
  91. localEP = new IPEndPoint (IPAddress.Any, port);
  92. else
  93. localEP = new IPEndPoint (IPAddress.IPv6Any, port);
  94. InitSocket (localEP);
  95. }
  96. #endif
  97. public UdpClient (string hostname, int port)
  98. {
  99. if (hostname == null)
  100. throw new ArgumentNullException ("hostname");
  101. if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
  102. throw new ArgumentOutOfRangeException ("port");
  103. InitSocket (null);
  104. Connect (hostname, port);
  105. }
  106. private void InitSocket (EndPoint localEP)
  107. {
  108. if(socket != null) {
  109. socket.Close();
  110. socket = null;
  111. }
  112. socket = new Socket (family, SocketType.Dgram, ProtocolType.Udp);
  113. if (localEP != null)
  114. socket.Bind (localEP);
  115. }
  116. #endregion // Constructors
  117. #region Public methods
  118. #region Close
  119. public void Close ()
  120. {
  121. ((IDisposable) this).Dispose ();
  122. }
  123. #endregion
  124. #region Connect
  125. void DoConnect (IPEndPoint endPoint)
  126. {
  127. /* Catch EACCES and turn on SO_BROADCAST then,
  128. * as UDP sockets don't have it set by default
  129. */
  130. try {
  131. socket.Connect (endPoint);
  132. } catch (SocketException ex) {
  133. if (ex.ErrorCode == (int)SocketError.AccessDenied) {
  134. socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  135. socket.Connect (endPoint);
  136. } else {
  137. throw;
  138. }
  139. }
  140. }
  141. public void Connect (IPEndPoint endPoint)
  142. {
  143. CheckDisposed ();
  144. if (endPoint == null)
  145. throw new ArgumentNullException ("endPoint");
  146. DoConnect (endPoint);
  147. active = true;
  148. }
  149. public void Connect (IPAddress addr, int port)
  150. {
  151. if (addr == null)
  152. throw new ArgumentNullException ("addr");
  153. if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
  154. throw new ArgumentOutOfRangeException ("port");
  155. Connect (new IPEndPoint (addr, port));
  156. }
  157. public void Connect (string hostname, int port)
  158. {
  159. if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
  160. throw new ArgumentOutOfRangeException ("port");
  161. IPAddress[] addresses = Dns.GetHostAddresses (hostname);
  162. for(int i=0; i<addresses.Length; i++) {
  163. try {
  164. this.family = addresses[i].AddressFamily;
  165. Connect (new IPEndPoint (addresses[i], port));
  166. break;
  167. } catch(Exception e) {
  168. if(i == addresses.Length - 1){
  169. if(socket != null) {
  170. socket.Close();
  171. socket = null;
  172. }
  173. /// This is the last entry, re-throw the exception
  174. throw e;
  175. }
  176. }
  177. }
  178. }
  179. #endregion
  180. #region Multicast methods
  181. public void DropMulticastGroup (IPAddress multicastAddr)
  182. {
  183. CheckDisposed ();
  184. if (multicastAddr == null)
  185. throw new ArgumentNullException ("multicastAddr");
  186. if(family == AddressFamily.InterNetwork)
  187. socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
  188. new MulticastOption (multicastAddr));
  189. #if NET_1_1
  190. else
  191. socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
  192. new IPv6MulticastOption (multicastAddr));
  193. #endif
  194. }
  195. #if NET_1_1
  196. public void DropMulticastGroup (IPAddress multicastAddr,
  197. int ifindex)
  198. {
  199. CheckDisposed ();
  200. /* LAMESPEC: exceptions haven't been specified
  201. * for this overload.
  202. */
  203. if (multicastAddr == null) {
  204. throw new ArgumentNullException ("multicastAddr");
  205. }
  206. /* Does this overload only apply to IPv6?
  207. * Only the IPv6MulticastOption has an
  208. * ifindex-using constructor. The MS docs
  209. * don't say.
  210. */
  211. if (family == AddressFamily.InterNetworkV6) {
  212. socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership, new IPv6MulticastOption (multicastAddr, ifindex));
  213. }
  214. }
  215. #endif
  216. public void JoinMulticastGroup (IPAddress multicastAddr)
  217. {
  218. CheckDisposed ();
  219. if (multicastAddr == null)
  220. #if NET_2_0
  221. throw new ArgumentNullException ("multicastAddr");
  222. #else
  223. throw new NullReferenceException ();
  224. #endif
  225. if(family == AddressFamily.InterNetwork)
  226. socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
  227. new MulticastOption (multicastAddr));
  228. #if NET_1_1
  229. else
  230. socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
  231. new IPv6MulticastOption (multicastAddr));
  232. #endif
  233. }
  234. #if NET_1_1
  235. public void JoinMulticastGroup (int ifindex,
  236. IPAddress multicastAddr)
  237. {
  238. CheckDisposed ();
  239. if (multicastAddr == null)
  240. throw new ArgumentNullException ("multicastAddr");
  241. if (family == AddressFamily.InterNetworkV6)
  242. socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption (multicastAddr, ifindex));
  243. else
  244. throw new SocketException ((int) SocketError.OperationNotSupported);
  245. }
  246. #endif
  247. public void JoinMulticastGroup (IPAddress multicastAddr, int timeToLive)
  248. {
  249. CheckDisposed ();
  250. if (multicastAddr == null)
  251. throw new ArgumentNullException ("multicastAddr");
  252. if (timeToLive < 0 || timeToLive > 255)
  253. throw new ArgumentOutOfRangeException ("timeToLive");
  254. JoinMulticastGroup (multicastAddr);
  255. if(family == AddressFamily.InterNetwork)
  256. socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive,
  257. timeToLive);
  258. #if NET_1_1
  259. else
  260. socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.MulticastTimeToLive,
  261. timeToLive);
  262. #endif
  263. }
  264. #if NET_2_0
  265. public void JoinMulticastGroup (IPAddress multicastAddr,
  266. IPAddress localAddress)
  267. {
  268. CheckDisposed ();
  269. if (family == AddressFamily.InterNetwork)
  270. socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption (multicastAddr, localAddress));
  271. else
  272. throw new SocketException ((int) SocketError.OperationNotSupported);
  273. }
  274. #endif
  275. #endregion
  276. #region Data I/O
  277. public byte [] Receive (ref IPEndPoint remoteEP)
  278. {
  279. CheckDisposed ();
  280. byte [] recBuffer = new byte [65536]; // Max. size
  281. EndPoint endPoint = new IPEndPoint (IPAddress.Any, 0);
  282. int dataRead = socket.ReceiveFrom (recBuffer, ref endPoint);
  283. if (dataRead < recBuffer.Length)
  284. recBuffer = CutArray (recBuffer, dataRead);
  285. remoteEP = (IPEndPoint) endPoint;
  286. return recBuffer;
  287. }
  288. int DoSend (byte[] dgram, int bytes, IPEndPoint endPoint)
  289. {
  290. /* Catch EACCES and turn on SO_BROADCAST then,
  291. * as UDP sockets don't have it set by default
  292. */
  293. try {
  294. if (endPoint == null) {
  295. return(socket.Send (dgram, 0, bytes,
  296. SocketFlags.None));
  297. } else {
  298. return(socket.SendTo (dgram, 0, bytes,
  299. SocketFlags.None,
  300. endPoint));
  301. }
  302. } catch (SocketException ex) {
  303. if (ex.ErrorCode == (int)SocketError.AccessDenied) {
  304. socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  305. if (endPoint == null) {
  306. return(socket.Send (dgram, 0, bytes, SocketFlags.None));
  307. } else {
  308. return(socket.SendTo (dgram, 0, bytes, SocketFlags.None, endPoint));
  309. }
  310. } else {
  311. throw;
  312. }
  313. }
  314. }
  315. public int Send (byte [] dgram, int bytes)
  316. {
  317. CheckDisposed ();
  318. if (dgram == null)
  319. throw new ArgumentNullException ("dgram");
  320. if (!active)
  321. throw new InvalidOperationException ("Operation not allowed on " +
  322. "non-connected sockets.");
  323. return(DoSend (dgram, bytes, null));
  324. }
  325. public int Send (byte [] dgram, int bytes, IPEndPoint endPoint)
  326. {
  327. CheckDisposed ();
  328. if (dgram == null)
  329. throw new ArgumentNullException ("dgram is null");
  330. if (active) {
  331. if (endPoint != null)
  332. throw new InvalidOperationException ("Cannot send packets to an " +
  333. "arbitrary host while connected.");
  334. return(DoSend (dgram, bytes, null));
  335. }
  336. return(DoSend (dgram, bytes, endPoint));
  337. }
  338. public int Send (byte [] dgram, int bytes, string hostname, int port)
  339. {
  340. return Send (dgram, bytes,
  341. new IPEndPoint (Dns.GetHostAddresses (hostname) [0], port));
  342. }
  343. private byte [] CutArray (byte [] orig, int length)
  344. {
  345. byte [] newArray = new byte [length];
  346. Buffer.BlockCopy (orig, 0, newArray, 0, length);
  347. return newArray;
  348. }
  349. #endregion
  350. #if NET_2_0
  351. IAsyncResult DoBeginSend (byte[] datagram, int bytes,
  352. IPEndPoint endPoint,
  353. AsyncCallback requestCallback,
  354. object state)
  355. {
  356. /* Catch EACCES and turn on SO_BROADCAST then,
  357. * as UDP sockets don't have it set by default
  358. */
  359. try {
  360. if (endPoint == null) {
  361. return(socket.BeginSend (datagram, 0, bytes, SocketFlags.None, requestCallback, state));
  362. } else {
  363. return(socket.BeginSendTo (datagram, 0, bytes, SocketFlags.None, endPoint, requestCallback, state));
  364. }
  365. } catch (SocketException ex) {
  366. if (ex.ErrorCode == (int)SocketError.AccessDenied) {
  367. socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  368. if (endPoint == null) {
  369. return(socket.BeginSend (datagram, 0, bytes, SocketFlags.None, requestCallback, state));
  370. } else {
  371. return(socket.BeginSendTo (datagram, 0, bytes, SocketFlags.None, endPoint, requestCallback, state));
  372. }
  373. } else {
  374. throw;
  375. }
  376. }
  377. }
  378. public IAsyncResult BeginSend (byte[] datagram, int bytes,
  379. AsyncCallback requestCallback,
  380. object state)
  381. {
  382. return(BeginSend (datagram, bytes, null,
  383. requestCallback, state));
  384. }
  385. public IAsyncResult BeginSend (byte[] datagram, int bytes,
  386. IPEndPoint endPoint,
  387. AsyncCallback requestCallback,
  388. object state)
  389. {
  390. CheckDisposed ();
  391. if (datagram == null) {
  392. throw new ArgumentNullException ("datagram");
  393. }
  394. return(DoBeginSend (datagram, bytes, endPoint,
  395. requestCallback, state));
  396. }
  397. public IAsyncResult BeginSend (byte[] datagram, int bytes,
  398. string hostname, int port,
  399. AsyncCallback requestCallback,
  400. object state)
  401. {
  402. return(BeginSend (datagram, bytes, new IPEndPoint (Dns.GetHostAddresses (hostname) [0], port), requestCallback, state));
  403. }
  404. public int EndSend (IAsyncResult asyncResult)
  405. {
  406. CheckDisposed ();
  407. if (asyncResult == null) {
  408. throw new ArgumentNullException ("asyncResult is a null reference");
  409. }
  410. return(socket.EndSend (asyncResult));
  411. }
  412. public IAsyncResult BeginReceive (AsyncCallback callback,
  413. object state)
  414. {
  415. CheckDisposed ();
  416. recvbuffer = new byte[8192];
  417. EndPoint ep;
  418. if (family == AddressFamily.InterNetwork) {
  419. ep = new IPEndPoint (IPAddress.Any, 0);
  420. } else {
  421. ep = new IPEndPoint (IPAddress.IPv6Any, 0);
  422. }
  423. return(socket.BeginReceiveFrom (recvbuffer, 0, 8192,
  424. SocketFlags.None,
  425. ref ep,
  426. callback, state));
  427. }
  428. public byte[] EndReceive (IAsyncResult asyncResult,
  429. ref IPEndPoint remoteEP)
  430. {
  431. CheckDisposed ();
  432. if (asyncResult == null) {
  433. throw new ArgumentNullException ("asyncResult is a null reference");
  434. }
  435. EndPoint ep;
  436. if (family == AddressFamily.InterNetwork) {
  437. ep = new IPEndPoint (IPAddress.Any, 0);
  438. } else {
  439. ep = new IPEndPoint (IPAddress.IPv6Any, 0);
  440. }
  441. int bytes = socket.EndReceiveFrom (asyncResult,
  442. ref ep);
  443. remoteEP = (IPEndPoint)ep;
  444. /* Need to copy into a new array here, because
  445. * otherwise the returned array length is not
  446. * 'bytes'
  447. */
  448. byte[] buf = new byte[bytes];
  449. Array.Copy (recvbuffer, buf, bytes);
  450. return(buf);
  451. }
  452. #endif
  453. #region Properties
  454. protected bool Active {
  455. get { return active; }
  456. set { active = value; }
  457. }
  458. #if NET_2_0
  459. public
  460. #else
  461. protected
  462. #endif
  463. Socket Client {
  464. get { return socket; }
  465. set { socket = value; }
  466. }
  467. #if NET_2_0
  468. public int Available
  469. {
  470. get {
  471. return(socket.Available);
  472. }
  473. }
  474. #if TARGET_JVM
  475. [MonoNotSupported ("Not supported as Socket.DontFragment is not supported")]
  476. #endif
  477. public bool DontFragment
  478. {
  479. get {
  480. return(socket.DontFragment);
  481. }
  482. set {
  483. socket.DontFragment = value;
  484. }
  485. }
  486. #if TARGET_JVM
  487. [MonoNotSupported ("Not supported as Socket.EnableBroadcast is not supported")]
  488. #endif
  489. public bool EnableBroadcast
  490. {
  491. get {
  492. return(socket.EnableBroadcast);
  493. }
  494. set {
  495. socket.EnableBroadcast = value;
  496. }
  497. }
  498. #if TARGET_JVM
  499. [MonoNotSupported ("Not supported as Socket.ExclusiveAddressUse is not supported")]
  500. #endif
  501. public bool ExclusiveAddressUse
  502. {
  503. get {
  504. return(socket.ExclusiveAddressUse);
  505. }
  506. set {
  507. socket.ExclusiveAddressUse = value;
  508. }
  509. }
  510. #if TARGET_JVM
  511. [MonoNotSupported ("Not supported as Socket.MulticastLoopback is not supported")]
  512. #endif
  513. public bool MulticastLoopback
  514. {
  515. get {
  516. return(socket.MulticastLoopback);
  517. }
  518. set {
  519. socket.MulticastLoopback = value;
  520. }
  521. }
  522. #if TARGET_JVM
  523. [MonoNotSupported ("Not supported as Socket.Ttl is not supported")]
  524. #endif
  525. public short Ttl
  526. {
  527. get {
  528. return(socket.Ttl);
  529. }
  530. set {
  531. socket.Ttl = value;
  532. }
  533. }
  534. #endif
  535. #endregion
  536. #region Disposing
  537. void IDisposable.Dispose ()
  538. {
  539. Dispose (true);
  540. GC.SuppressFinalize (this);
  541. }
  542. #if NET_2_0
  543. protected virtual
  544. #endif
  545. void Dispose (bool disposing)
  546. {
  547. if (disposed)
  548. return;
  549. disposed = true;
  550. if (disposing){
  551. if (socket != null)
  552. socket.Close ();
  553. socket = null;
  554. }
  555. }
  556. ~UdpClient ()
  557. {
  558. Dispose (false);
  559. }
  560. private void CheckDisposed ()
  561. {
  562. if (disposed)
  563. throw new ObjectDisposedException (GetType().FullName);
  564. }
  565. #endregion
  566. #endregion
  567. }
  568. }