UdpClient.cs 17 KB

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