2 // System.Net.Sockets.UdpClient.cs
\r
5 // Gonzalo Paniagua Javier <gonzalo@ximian.com>
\r
7 // Copyright (C) Ximian, Inc. http://www.ximian.com
\r
13 namespace System.Net.Sockets
\r
15 public class UdpClient : IDisposable
\r
17 private bool disposed = false;
\r
18 private bool active = false;
\r
19 private Socket socket;
\r
20 private IPEndPoint localEP;
\r
22 #region Constructors
\r
25 localEP = new IPEndPoint (IPAddress.Any, 0);
\r
29 public UdpClient (int port)
\r
31 // IPEndPoint throws ArgumentException when port is invalid
\r
32 localEP = new IPEndPoint (IPAddress.Any, port);
\r
36 public UdpClient (IPEndPoint localEP)
\r
38 if (localEP == null)
\r
39 throw new ArgumentNullException ("IPEndPoint cannot be null");
\r
41 this.localEP = localEP;
\r
45 public UdpClient (string hostname, int port)
\r
47 if (hostname == null)
\r
48 throw new ArgumentNullException ("hostname cannot be null");
\r
50 if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
\r
51 throw new ArgumentException ("Invalid port");
\r
53 localEP = new IPEndPoint (IPAddress.Any, 0);
\r
55 Connect (hostname, port);
\r
58 private void InitSocket ()
\r
61 socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
\r
62 socket.Bind (localEP);
\r
65 #endregion // Constructors
\r
66 #region Public methods
\r
68 public void Close ()
\r
70 ((IDisposable) this).Dispose ();
\r
74 public void Connect (IPEndPoint endPoint)
\r
77 socket.Connect (endPoint);
\r
84 public void Connect (IPAddress addr, int port)
\r
86 Connect (new IPEndPoint (addr, port));
\r
89 public void Connect (string hostname, int port)
\r
91 Connect (new IPEndPoint (Dns.Resolve (hostname).AddressList [0], port));
\r
94 #region Multicast methods
\r
95 public void DropMulticastGroup (IPAddress multicastAddr)
\r
98 socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
\r
99 new MulticastOption (multicastAddr));
\r
105 public void JoinMulticastGroup (IPAddress multicastAddr)
\r
108 socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
\r
109 new MulticastOption (multicastAddr));
\r
115 public void JoinMulticastGroup (IPAddress multicastAddr, int timeToLive)
\r
117 JoinMulticastGroup (multicastAddr);
\r
119 socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive,
\r
127 public byte [] Receive (ref IPEndPoint remoteEP)
\r
130 // Length of the array for receiving data??
\r
132 int available = socket.Available;
\r
133 if (available < 512)
\r
136 recBuffer = new byte [available];
\r
137 EndPoint endPoint = new IPEndPoint (IPAddress.Any, 0);
\r
138 int dataRead = socket.ReceiveFrom (recBuffer, ref endPoint);
\r
139 if (dataRead < recBuffer.Length)
\r
140 recBuffer = CutArray (recBuffer, dataRead);
\r
142 remoteEP = (IPEndPoint) endPoint;
\r
149 public int Send (byte [] dgram, int bytes)
\r
153 throw new ArgumentNullException ("dgram is null");
\r
156 if (dgram.Length <= bytes)
\r
159 realDgram = CutArray (dgram, (bytes >= dgram.Length) ? bytes : dgram.Length);
\r
161 // the socket should be connected already, so I use Send instead of SendTo
\r
162 return socket.Send (realDgram);
\r
168 public int Send (byte [] dgram, int bytes, IPEndPoint endPoint)
\r
172 throw new ArgumentNullException ("dgram is null");
\r
175 if (dgram.Length <= bytes)
\r
178 realDgram = CutArray (dgram, (bytes >= dgram.Length) ? bytes : dgram.Length);
\r
180 // the socket should not be connected
\r
181 return socket.SendTo (realDgram, endPoint);
\r
187 public int Send (byte [] dgram, int bytes, string hostname, int port)
\r
189 return Send (dgram, bytes,
\r
190 new IPEndPoint (Dns.Resolve (hostname).AddressList [0], port));
\r
193 private byte [] CutArray (byte [] orig, int length)
\r
195 byte [] newArray = new byte [length];
\r
196 Array.Copy (orig, 0, newArray, 0, length);
\r
202 protected bool Active {
\r
203 get { return active; }
\r
204 set { active = value; }
\r
207 protected Socket Client {
\r
208 get { return socket; }
\r
209 set { socket = value; }
\r
214 // commented because in the ms.net implementation these are not overriden. -- LP
\r
216 public override bool Equals (object obj)
\r
218 if (obj is UdpClient)
\r
219 return (((UdpClient) obj).socket == socket &&
\r
220 ((UdpClient) obj).localEP == localEP);
\r
225 public override int GetHashCode ()
\r
227 return (socket.GetHashCode () + localEP.GetHashCode () + (active ? 1 : 0));
\r
233 void IDisposable.Dispose ()
\r
236 GC.SuppressFinalize (this);
\r
239 void Dispose (bool disposing)
\r
245 // release managed resources
\r
248 // release unmanaged resources
\r
260 private void CheckDisposed ()
\r
263 throw new ObjectDisposedException (GetType().FullName);
\r