[ Team LiB ] Previous Section Next Section

SocketCF 1.0, ECMA 1.0, disposable

System.Net.Sockets (system.dll)class

This class implements a standard Berkeley socket. Each socket is constructed with the address family to use, the socket type (datagram or stream), and the protocol that the socket will use. Every socket must be bound to a local endpoint before you can use it. The Bind( ) method takes a System.Net.IPEndPoint object that contains the local IP address and port number to bind the socket to. Bind( ) must be called before any connection can be made through the socket. To establish a connection to a remote address, use Connect( ).

To listen for connections from remote clients, use Listen( ) to set the socket in listening mode where it waits for incoming connections. The integer argument to Listen( ) specifies how many remote connection requests can be queued at one time, waiting for a socket connection. A call to Accept( ) returns a new socket that connects to the first pending connection request in the listening queue. This new socket exists only for this connection and is destroyed once the connection is closed.

You can interrogate the socket to determine if it supports IPv4 (standard TCP/IP quad-style addresses) by examining the SupportsIPV4 property; similarly, you can determine if the socket supports IPv6 by examining SupportsIPv6.

Data is written to a socket using Send( ). Data from a specified buffer is sent through the socket to its remote endpoint. Data is read from a socket using Receive( ). Receive( ) gets data from the socket connection and stores it in a specified receive buffer.

You can set several socket options to control the behavior of a socket with SetSocketOption( ). This method requires a SocketOptionLevel value, which determines the type of socket option to set. For example, SocketOptionLevel.IP is used for options related to an IP socket. The SocketOptionName value gives the specific option, which must be applicable to the SocketOptionLevel. The last argument to SetSocketOption( ) provides the value of the option. SetSocketOption( ) enables features such as SocketOptionName.KeepAlive, in which a connection is maintained even when no data transfer is occurring, or SocketOptionName.MaxConnections, which sets the maximum permitted size of a listen queue.

When a session is finished, the connection can be gracefully closed with Shutdown( ). When send or receive options are called with a SocketShutdown value, they are no longer allowed on the socket. A call to Close( ) terminates the socket connection.

public class Socket : IDisposable {
// Public Constructors
   public Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType);
// Public Static Properties
   public static bool SupportsIPv4{get; }
   public static bool SupportsIPv6{get; }
// Public Instance Properties
   public AddressFamily AddressFamily{get; }
   public int Available{get; }
   public bool Blocking{set; get; }
   public bool Connected{get; }
   public IntPtr Handle{get; }
   public EndPoint LocalEndPoint{get; }
   public ProtocolType ProtocolType{get; }
   public EndPoint RemoteEndPoint{get; }
   public SocketType SocketType{get; }
// Public Static Methods
   public static void Select(System.Collections.IList checkRead, System.Collections.IList checkWrite,
       System.Collections.IList checkError, int microSeconds);
// Public Instance Methods
   public Socket Accept( );
   public IAsyncResult BeginAccept(AsyncCallback callback, object state);
   public IAsyncResult BeginConnect(System.Net.EndPoint remoteEP, AsyncCallback callback, object state);
   public IAsyncResult BeginReceive(byte[ ] buffer, int offset, int size, SocketFlags socketFlags,
       AsyncCallback callback, object state);
   public IAsyncResult BeginReceiveFrom(byte[ ] buffer, int offset, int size, SocketFlags socketFlags,
       ref System.Net.EndPoint remoteEP, AsyncCallback callback, object state);
   public IAsyncResult BeginSend(byte[ ] buffer, int offset, int size, SocketFlags socketFlags,
       AsyncCallback callback, object state);
   public IAsyncResult BeginSendTo(byte[ ] buffer, int offset, int size, SocketFlags socketFlags,
       System.Net.EndPoint remoteEP, AsyncCallback callback, object state);
   public void Bind(System.Net.EndPoint localEP);
   public void Close( );
   public void Connect(System.Net.EndPoint remoteEP);
   public Socket EndAccept(IAsyncResult asyncResult);
   public void EndConnect(IAsyncResult asyncResult);
   public int EndReceive(IAsyncResult asyncResult);
   public int EndReceiveFrom(IAsyncResult asyncResult, ref System.Net.EndPoint endPoint);
   public int EndSend(IAsyncResult asyncResult);
   public int EndSendTo(IAsyncResult asyncResult);
   public override int GetHashCode( );    
// overrides object
   public byte[ ] GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, 
        int optionLength);
   public object GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName);
   public void GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, 
        byte[ ] optionValue);
   public int IOControl(int ioControlCode, byte[ ] optionInValue, byte[ ] optionOutValue);
   public void Listen(int backlog);
   public bool Poll(int microSeconds, SelectMode mode);
   public int Receive(byte[ ] buffer);
   public int Receive(byte[ ] buffer, int offset, int size, SocketFlags socketFlags);
   public int Receive(byte[ ] buffer, int size, SocketFlags socketFlags);
   public int Receive(byte[ ] buffer, SocketFlags socketFlags);
   public int ReceiveFrom(byte[ ] buffer, ref System.Net.EndPoint remoteEP);
   public int ReceiveFrom(byte[ ] buffer, int offset, int size, SocketFlags socketFlags, 
        ref System.Net.EndPoint remoteEP);
   public int ReceiveFrom(byte[ ] buffer, int size, SocketFlags socketFlags, ref System.Net.EndPoint remoteEP);
   public int ReceiveFrom(byte[ ] buffer, SocketFlags socketFlags, ref System.Net.EndPoint remoteEP);
   public int Send(byte[ ] buffer);
   public int Send(byte[ ] buffer, int offset, int size, SocketFlags socketFlags);
   public int Send(byte[ ] buffer, int size, SocketFlags socketFlags);
   public int Send(byte[ ] buffer, SocketFlags socketFlags);
   public int SendTo(byte[ ] buffer, System.Net.EndPoint remoteEP);
   public int SendTo(byte[ ] buffer, int offset, int size, SocketFlags socketFlags, 
        System.Net.EndPoint remoteEP);
   public int SendTo(byte[ ] buffer, int size, SocketFlags socketFlags, System.Net.EndPoint remoteEP);
   public int SendTo(byte[ ] buffer, SocketFlags socketFlags, System.Net.EndPoint remoteEP);
   public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, 
        byte[ ] optionValue);
   public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, 
        int optionValue);
   public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, 
        object optionValue);
   public void Shutdown(SocketShutdown how);
// Protected Instance Methods
   protected virtual void Dispose(bool disposing);
   protected override void Finalize( );   
// overrides object
}

Returned By

NetworkStream.Socket, TcpClient.Client, TcpListener.{AcceptSocket( ), Server}, UdpClient.Client

Passed To

NetworkStream.NetworkStream( ), TcpClient.Client, UdpClient.Client

    [ Team LiB ] Previous Section Next Section