| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- //
- // System.Net.Sockets.UdpClient.cs
- //
- // Author:
- // Gonzalo Paniagua Javier <[email protected]>
- //
- // Copyright (C) Ximian, Inc. http://www.ximian.com
- //
- using System;
- using System.Net;
- namespace System.Net.Sockets
- {
- public class UdpClient : IDisposable
- {
- private bool disposed = false;
- private bool active = false;
- private Socket socket;
- private IPEndPoint localEP;
-
- #region Constructors
- public UdpClient ()
- {
- localEP = new IPEndPoint (IPAddress.Any, 0);
- InitSocket ();
- }
- public UdpClient (int port)
- {
- // IPEndPoint throws ArgumentException when port is invalid
- localEP = new IPEndPoint (IPAddress.Any, port);
- InitSocket ();
- }
- public UdpClient (IPEndPoint localEP)
- {
- if (localEP == null)
- throw new ArgumentNullException ("IPEndPoint cannot be null");
- this.localEP = localEP;
- InitSocket ();
- }
- public UdpClient (string hostname, int port)
- {
- if (hostname == null)
- throw new ArgumentNullException ("hostname cannot be null");
- if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
- throw new ArgumentException ("Invalid port");
- localEP = new IPEndPoint (IPAddress.Any, 0);
- InitSocket ();
- Connect (hostname, port);
- }
- private void InitSocket ()
- {
- active = false;
- socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- socket.Bind (localEP);
- }
- #endregion // Constructors
- #region Public methods
- #region Close
- public void Close ()
- {
- ((IDisposable) this).Dispose ();
- }
- #endregion
- #region Connect
- public void Connect (IPEndPoint endPoint)
- {
- try {
- socket.Connect (endPoint);
- active = true;
- } finally {
- CheckDisposed ();
- }
- }
- public void Connect (IPAddress addr, int port)
- {
- Connect (new IPEndPoint (addr, port));
- }
- public void Connect (string hostname, int port)
- {
- Connect (new IPEndPoint (Dns.Resolve (hostname).AddressList [0], port));
- }
- #endregion
- #region Multicast methods
- public void DropMulticastGroup (IPAddress multicastAddr)
- {
- try {
- socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
- new MulticastOption (multicastAddr));
- } finally {
- CheckDisposed ();
- }
- }
- public void JoinMulticastGroup (IPAddress multicastAddr)
- {
- try {
- socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
- new MulticastOption (multicastAddr));
- } finally {
- CheckDisposed ();
- }
- }
- public void JoinMulticastGroup (IPAddress multicastAddr, int timeToLive)
- {
- JoinMulticastGroup (multicastAddr);
- try {
- socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive,
- timeToLive);
- } finally {
- CheckDisposed ();
- }
- }
- #endregion
- #region Data I/O
- public byte [] Receive (ref IPEndPoint remoteEP)
- {
- try {
- if (remoteEP == null)
- throw new ArgumentNullException ("remoteEP cannot be null");
- // Length of the array for receiving data??
- byte [] recBuffer;
- int available = socket.Available;
- recBuffer = new byte [1024]; // FIXME: any suggestions?
- EndPoint endPoint = (EndPoint) remoteEP;
- int dataRead = socket.ReceiveFrom (recBuffer, ref endPoint);
- if (dataRead < recBuffer.Length)
- return CutArray (recBuffer, dataRead);
- return recBuffer;
- } finally {
- CheckDisposed ();
- }
- }
- public int Send (byte [] dgram, int bytes)
- {
- try {
- if (dgram == null)
- throw new ArgumentNullException ("dgram is null");
- byte [] realDgram;
- if (dgram.Length <= bytes)
- realDgram = dgram;
- else
- realDgram = CutArray (dgram, (bytes >= dgram.Length) ? bytes : dgram.Length);
- // the socket should be connected already, so I use Send instead of SendTo
- return socket.Send (realDgram);
- } finally {
- CheckDisposed ();
- }
- }
- public int Send (byte [] dgram, int bytes, IPEndPoint endPoint)
- {
- try {
- if (dgram == null)
- throw new ArgumentNullException ("dgram is null");
- byte [] realDgram;
- if (dgram.Length <= bytes)
- realDgram = dgram;
- else
- realDgram = CutArray (dgram, (bytes >= dgram.Length) ? bytes : dgram.Length);
- // the socket should not be connected
- return socket.SendTo (realDgram, endPoint);
- } finally {
- CheckDisposed ();
- }
- }
- public int Send (byte [] dgram, int bytes, string hostname, int port)
- {
- return Send (dgram, bytes,
- new IPEndPoint (Dns.Resolve (hostname).AddressList [0], port));
- }
- private byte [] CutArray (byte [] orig, int length)
- {
- byte [] newArray = new byte [length];
- Array.Copy (orig, 0, newArray, 0, length);
- return newArray;
- }
- #endregion
- #region Properties
- protected bool Active {
- get { return active; }
- set { active = value; }
- }
- protected Socket Client {
- get { return socket; }
- set { socket = value; }
- }
- #endregion
- /*
- // commented because in the ms.net implementation these are not overriden. -- LP
- #region Overrides
- public override bool Equals (object obj)
- {
- if (obj is UdpClient)
- return (((UdpClient) obj).socket == socket &&
- ((UdpClient) obj).localEP == localEP);
- return false;
- }
- public override int GetHashCode ()
- {
- return (socket.GetHashCode () + localEP.GetHashCode () + (active ? 1 : 0));
- }
- #endregion
- */
- #region Disposing
- void IDisposable.Dispose ()
- {
- Dispose (true);
- GC.SuppressFinalize (this);
- }
- protected virtual void Dispose (bool disposing)
- {
- if (disposed)
- return;
- disposed = true;
- if (disposing) {
- // release managed resources
- localEP = null;
- }
- // release unmanaged resources
- Socket s = socket;
- socket = null;
- if (s != null)
- s.Close ();
- }
-
- ~UdpClient ()
- {
- Dispose (false);
- }
-
- private void CheckDisposed ()
- {
- if (disposed)
- throw new ObjectDisposedException (GetType().FullName);
- }
- #endregion
- #endregion
- }
- }
|