Show / Hide Table of Contents

    Class Socket

    Implements the Berkeley sockets interface.

    Inheritance
    Object
    Socket
    Implements
    IDisposable
    Inherited Members
    Object.ToString()
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.ReferenceEquals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Namespace: System.Net.Sockets
    Assembly: System.Net.dll
    Syntax
    public class Socket : IDisposable

    Constructors

    | Improve this Doc View Source

    Socket(AddressFamily, SocketType, ProtocolType)

    Initializes a new instance of the Socket class using the specified address family, socket type and protocol.

    Declaration
    public Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
    Parameters
    Type Name Description
    AddressFamily addressFamily

    One of the AddressFamily values.

    SocketType socketType

    One of the SocketType values.

    ProtocolType protocolType

    One of the ProtocolType values.

    Remarks

    The addressFamily parameter specifies the addressing scheme that the Socket class uses, the socketType parameter specifies the type of the Socket class, and the protocolType parameter specifies the protocol used by Socket. The three parameters are not independent. Some address families restrict which protocols can be used with them, and often the Socket type is implicit in the protocol. If the combination of address family, Socket type, and protocol type esults in an invalid Socket, this constructor throws a SocketException.

    Properties

    | Improve this Doc View Source

    Available

    Gets the amount of data that has been received from the network and is available to be read.

    Declaration
    public int Available { get; }
    Property Value
    Type Description
    Int32

    An integer error code that is associated with this exception.

    Remarks

    If you are using a non-blocking Socket, Available is a good way to determine whether data is queued for reading, before calling Receive(Byte[]). The available data is the total amount of data queued in the network buffer for reading. If no data is queued in the network buffer, Available returns 0.

    If the remote host shuts down or closes the connection, Available can throw a SocketException. If you receive a SocketException, use the ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

    | Improve this Doc View Source

    LocalEndPoint

    Gets the local endpoint.

    Declaration
    public EndPoint LocalEndPoint { get; }
    Property Value
    Type Description
    EndPoint

    The EndPoint that the Socket is using for communications.

    Remarks

    The LocalEndPoint property gets an EndPoint that contains the local IP address and port number to which your Socket is bound. You must cast this EndPoint to an IPEndPoint before retrieving any information. You can then call the Address method to retrieve the local IPAddress, and the Port method to retrieve the local port number.

    The LocalEndPoint property is usually set after you make a call to the Bind(EndPoint) method. If you allow the system to assign your socket's local IP address and port number, the LocalEndPoint property will be set after the first I/O operation. For connection-oriented protocols, the first I/O operation would be a call to the Connect or Accept() method. For connectionless protocols, the first I/O operation would be any of the send or receive calls.

    | Improve this Doc View Source

    ReceiveTimeout

    Gets or sets a value that specifies the amount of time after which a synchronous Receive(Byte[]) call will time out.

    Declaration
    public int ReceiveTimeout { get; set; }
    Property Value
    Type Description
    Int32

    The time-out value, in milliseconds. The default value is 0, which indicates an infinite time-out period. Specifying -1 also indicates an infinite time-out period.

    Remarks

    This option applies to synchronous Receive(Byte[]) calls only. If the time-out period is exceeded, the Receive(Byte[]) method will throw a SocketException.

    | Improve this Doc View Source

    RemoteEndPoint

    Gets the remote endpoint.

    Declaration
    public EndPoint RemoteEndPoint { get; }
    Property Value
    Type Description
    EndPoint

    The EndPoint with which the Socket is communicating.

    Remarks

    If you are using a connection-oriented protocol, the RemoteEndPoint property gets the EndPoint that contains the remote IP address and port number to which the Socket is connected. If you are using a connectionless protocol, RemoteEndPoint contains the default remote IP address and port number with which the Socket will communicate. You must cast this EndPoint to an IPEndPoint before retrieving any information. You can then call the Address method to retrieve the remote IPAddress, and the Port method to retrieve the remote port number.

    The RemoteEndPoint is set after a call to either Accept() or Connect(EndPoint). If you try to access this property earlier, RemoteEndPoint will throw a SocketException. If you receive a SocketException, use the ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

    | Improve this Doc View Source

    SendTimeout

    Gets or sets a value that specifies the amount of time after which a synchronous Send(Byte[]) call will time out.

    Declaration
    public int SendTimeout { get; set; }
    Property Value
    Type Description
    Int32

    The time-out value, in milliseconds. If you set the property with a value between 1 and 499, the value will be changed to 500. The default value is 0, which indicates an infinite time-out period. Specifying -1 also indicates an infinite time-out period.

    Remarks

    This option applies to synchronous Send(Byte[]) calls only. If the time-out period is exceeded, the Send(Byte[]) method will throw a SocketException.

    | Improve this Doc View Source

    SocketType

    Gets the type of the Socket.

    Declaration
    public SocketType SocketType { get; }
    Property Value
    Type Description
    SocketType

    One of the SocketType values.

    Remarks

    SocketType is read-only and is set when the Socket is created.

    Methods

    | Improve this Doc View Source

    Accept()

    Creates a new Socket for a newly created connection.

    Declaration
    public Socket Accept()
    Returns
    Type Description
    Socket

    A Socket for a newly created connection.

    Remarks

    Accept synchronously extracts the first pending connection request from the connection request queue of the listening socket, and then creates and returns a new Socket. You cannot use this returned Socket to accept any additional connections from the connection queue. However, you can call the RemoteEndPoint method of the returned Socket to identify the remote host's network address and port number.

    Before calling the Accept method, you must first call the Listen method to listen for and queue incoming connection requests.

    | Improve this Doc View Source

    Bind(EndPoint)

    Associates a Socket with a local endpoint.

    Declaration
    public void Bind(EndPoint localEP)
    Parameters
    Type Name Description
    EndPoint localEP

    The local EndPoint to associate with the Socket.

    Remarks

    Use the Bind method if you need to use a specific local endpoint. You must call Bind before you can call the Listen(Int32) method. You do not need to call Bind before using the Connect(EndPoint) method unless you need to use a specific local endpoint. You can use the Bind method on both connectionless and connection-oriented protocols.

    Before calling Bind, you must first create the local IPEndPoint from which you intend to communicate data. If you do not care which local address is assigned, you can create an IPEndPoint using Any as the address parameter, and the underlying service provider will assign the most appropriate network address. This might help simplify your application if you have multiple network interfaces. If you do not care which local port is used, you can create an IPEndPoint using 0 for the port number. In this case, the service provider will assign an available port number between 1024 and 5000.

    If you use the above approach, you can discover what local network address and port number has been assigned by calling the LocalEndPoint. If you are using a connection-oriented protocol, LocalEndPoint will not return the locally assigned network address until after you have made a call to the Connect(EndPoint) method. If you are using a connectionless protocol, you will not have access to this information until you have completed a send or receive.

    If you intend to receive multicast datagrams, you must call the Bind method with a multicast port number. You must call the Bind method if you intend to receive connectionless datagrams using the ReceiveFrom method.
    | Improve this Doc View Source

    Close()

    Closes the Socket connection and releases all associated resources.

    Declaration
    public void Close()
    Remarks

    The Close method closes the remote host connection and releases all managed and unmanaged resources associated with the Socket. Upon closing, the Connected property is set to false.

    | Improve this Doc View Source

    Connect(EndPoint)

    Establishes a connection to a remote host.

    Declaration
    public void Connect(EndPoint remoteEP)
    Parameters
    Type Name Description
    EndPoint remoteEP

    An EndPoint that represents the remote device.

    Remarks

    If you are using a connection-oriented protocol such as TCP, the Connect method synchronously establishes a network connection between LocalEndPoint and the specified remote endpoint. If you are using a connectionless protocol, Connect establishes a default remote host. After you call Connect, you can send data to the remote device with the Send(Byte[]) method, or receive data from the remote device with the Receive(Byte[]) method.

    If you are using a connectionless protocol such as UDP, you do not have to call Connect before sending and receiving data. You can use SendTo(Byte[], EndPoint) and ReceiveFrom(Byte[], Int32, Int32, SocketFlags, ref EndPoint) to synchronously communicate with a remote host. If you do call Connect, any datagrams that arrive from an address other than the specified default will be discarded. If you want to set your default remote host to a broadcast address, you must first call the SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean) method and set the socket option to Broadcast, or Connect will throw a SocketException. If you receive a SocketException, use the ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

    If you are using a connection-oriented protocol and did not call Bind before calling Connect, the underlying service provider will assign the local network address and port number. If you are using a connectionless protocol, the service provider will not assign a local network address and port number until you complete a send or receive operation. If you want to change the default remote host, call Connect again with the desired endpoint.

    | Improve this Doc View Source

    Dispose(Boolean)

    Releases the unmanaged resources used by the Socket, and optionally disposes of the managed resources.

    Declaration
    protected virtual void Dispose(bool disposing)
    Parameters
    Type Name Description
    Boolean disposing

    true to release both managed and unmanaged resources; false to releases only unmanaged resources.

    Remarks

    This method is called by the public Dispose() method and the Finalize() method. Dispose() invokes the protected Dispose(Boolean) method with the disposing parameter set to true. Finalize() invokes Dispose with disposing set to false.

    When the disposing parameter is true, this method releases all resources held by any managed objects that this Socket references. This method invokes the Dispose() method of each referenced object.

    | Improve this Doc View Source

    Finalize()

    Deconstructor

    Declaration
    protected void Finalize()
    | Improve this Doc View Source

    GetSocketOption(SocketOptionLevel, SocketOptionName)

    Returns the value of a Socket option.

    Declaration
    public object GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName)
    Parameters
    Type Name Description
    SocketOptionLevel optionLevel

    One of the SocketOptionLevel values.

    SocketOptionName optionName

    One of the SocketOptionName values.

    Returns
    Type Description
    Object

    /// An object that represents the value of the option.

    When the optionName parameter is set to Linger the return value is an Int32 with the value in seconds that the socket will linger after close. To check if linger is enabled for the socket the DontLinger option should be queried.

    When optionName is set to ExclusiveAddressUse, DontLinger, AcceptConnection, Broadcast or KeepAlive, the return value is Boolean.

    When optionName is any other value, the return value is an integer.

    Exceptions
    Type Condition
    NotSupportedException

    When using an SocketOptionName that can't be retrieved.

    | Improve this Doc View Source

    GetSocketOption(SocketOptionLevel, SocketOptionName, Byte[])

    Returns the specified Socket option setting, represented as a byte array.

    Declaration
    public void GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] val)
    Parameters
    Type Name Description
    SocketOptionLevel optionLevel

    One of the SocketOptionLevel values.

    SocketOptionName optionName

    One of the SocketOptionName values.

    Byte[] val

    An array of type Byte that is to receive the option setting.

    Remarks

    Socket options determine the behavior of the current Socket. Upon successful completion of this method, the array specified by the val parameter contains the value of the specified Socket option.

    When the length of the val array is smaller than the number of bytes required to store the value of the specified Socket option, GetSocketOption(SocketOptionLevel, SocketOptionName) will throw a SocketException. If you receive a SocketException, use the ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error. Use this overload for any sockets that are represented by Boolean values or integers.

    | Improve this Doc View Source

    Listen(Int32)

    Places a Socket in a listening state.

    Declaration
    public void Listen(int backlog)
    Parameters
    Type Name Description
    Int32 backlog

    The maximum length of the pending connections queue.

    Remarks

    Listen causes a connection-oriented Socket to listen for incoming connection attempts. The backlog parameter specifies the number of incoming connections that can be queued for acceptance. To determine the maximum number of connections you can specify, retrieve the MaxConnections value. Listen does not block.

    If you receive a SocketException, use the ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error. Use Accept() or BeginAccept to accept a connection from the queue.

    You must call the Bind(EndPoint) method before calling Listen, or Listen will throw a SocketException.

    | Improve this Doc View Source

    Poll(Int32, SelectMode)

    Determines the status of the Socket.

    Declaration
    public bool Poll(int microSeconds, SelectMode mode)
    Parameters
    Type Name Description
    Int32 microSeconds

    The time to wait for a response, in microseconds.

    SelectMode mode

    One of the SelectMode values.

    Returns
    Type Description
    Boolean

    The status of the Socket based on the polling mode value passed in the mode parameter.

    • SelectReadSelectWriteSelectError

      true if Listen has been called and a connection is pending;

      -or-

      true if data is available for reading;

      -or-

      true if the connection has been closed, reset, or terminated; otherwise, returns false.

      true , if processing a Connect, and the connection has succeeded;

      -or-

      true if data can be sent; otherwise, returns false.

      true if processing a Connect(EndPoint) that does not block, and the connection has failed;

      -or-

      true if OutOfBandInline is not set and out-of-band data is available; otherwise, returns false.

    Remarks

    The Poll method will check the state of the Socket. Specify SelectRead for the selectMode parameter to determine if the Socket is readable. Specify SelectWrite to determine if the Socket is writable. Use SelectError to detect an error condition. Poll will block execution until the specified time period, measured in microseconds, elapses. Set the microSeconds parameter to a negative integer if you would like to wait indefinitely for a response. If you want to check the status of multiple sockets, you might prefer to use the Select method.

    If you receive a SocketException, use the ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

    | Improve this Doc View Source

    Receive(Byte[])

    Receives data from a bound Socket into a receive buffer.

    Declaration
    public int Receive(byte[] buffer)
    Parameters
    Type Name Description
    Byte[] buffer

    An array of type Byte that is the storage location for the received data.

    Returns
    Type Description
    Int32

    The number of bytes received.

    Remarks

    The Receive(Byte[]) method reads data into the buffer parameter and returns the number of bytes successfully read. You can call Receive(Byte[]) from both connection-oriented and connectionless sockets.

    This overload only requires you to provide a receive buffer. The buffer offset defaults to 0, the size defaults to the length of the buffer parameter, and the SocketFlags value defaults to None.

    If no data is available for reading, the Receive(Byte[]) method will block until data is available, unless a time-out value was set by using ReceiveTimeout. If the time-out value was exceeded, the Receive(Byte[]) call will throw a SocketException. If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the Receive(Byte[]) method will complete immediately and throw a SocketException. You can use the Available property to determine if data is available for reading. When Available is non-zero, retry the receive operation.

    If you are using a connectionless Socket, Receive(Byte[]) will read the first queued datagram from the destination address you specify in the Connect method. If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

    | Improve this Doc View Source

    Receive(Byte[], Int32, Int32, SocketFlags)

    Receives the specified number of bytes from a bound Socket into the specified offset position of the receive buffer, using the specified SocketFlags.

    Declaration
    public int Receive(byte[] buffer, int offset, int size, SocketFlags socketFlags)
    Parameters
    Type Name Description
    Byte[] buffer

    An array of type Byte that is the storage location for the received data.

    Int32 offset

    The location in buffer to store the received data.

    Int32 size

    The number of bytes to receive.

    SocketFlags socketFlags

    A bitwise combination of the SocketFlags values.

    Returns
    Type Description
    Int32

    The number of bytes received.

    Remarks

    The Receive(Byte[]) method reads data into the buffer parameter and returns the number of bytes successfully read. You can call Receive(Byte[]) from both connection- oriented and connectionless sockets.

    | Improve this Doc View Source

    Receive(Byte[], Int32, SocketFlags)

    Receives the specified number of bytes of data from a bound Socket into a receive buffer, using the specified SocketFlags.

    Declaration
    public int Receive(byte[] buffer, int size, SocketFlags socketFlags)
    Parameters
    Type Name Description
    Byte[] buffer

    An array of type Byte that is the storage location for the received data.

    Int32 size

    The number of bytes to receive.

    SocketFlags socketFlags

    A bitwise combination of the SocketFlags values.

    Returns
    Type Description
    Int32

    The number of bytes received.

    Remarks

    The Receive(Byte[]) method reads data into the buffer parameter and returns the number of bytes successfully read. You can call Receive from both connection-oriented and connectionless sockets.

    This overload only requires you to provide a receive buffer, the number of bytes you want to receive, and the necessary SocketFlags.

    If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using ReceiveTimeout. If the time-out value was exceeded, the Receive(Byte[]) call will throw a SocketException. If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, The Receive(Byte[]) method will complete immediately and throw a SocketException. You can use the Available property to determine if data is available for reading. When Available is non-zero, retry your receive operation.

    | Improve this Doc View Source

    Receive(Byte[], SocketFlags)

    Receives data from a bound Socket into a receive buffer, using the specified SocketFlags.

    Declaration
    public int Receive(byte[] buffer, SocketFlags socketFlags)
    Parameters
    Type Name Description
    Byte[] buffer

    An array of type Byte that is the storage location for the received data.

    SocketFlags socketFlags

    A bitwise combination of the SocketFlags values.

    Returns
    Type Description
    Int32

    The number of bytes received.

    Remarks

    The Receive(Byte[]) method reads data into the buffer parameter and returns the number of bytes successfully read. You can call Receive(Byte[]) from both connection-oriented and connectionless sockets.

    This overload only requires you to provide a receive buffer and the necessary SocketFlags. The buffer offset defaults to 0, and the size defaults to the length of the byte parameter.

    | Improve this Doc View Source

    ReceiveFrom(Byte[], Int32, Int32, SocketFlags, ref EndPoint)

    Receives the specified number of bytes of data into the specified location of the data buffer, using the specified SocketFlags, and stores the endpoint.

    Declaration
    public int ReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP)
    Parameters
    Type Name Description
    Byte[] buffer

    An array of type Byte that is the storage location for received data.

    Int32 offset

    The position in the buffer parameter to store the received data.

    Int32 size

    The number of bytes to receive.

    SocketFlags socketFlags

    A bitwise combination of the SocketFlags values.

    EndPoint remoteEP

    An EndPoint, passed by reference, that represents the remote server.

    Returns
    Type Description
    Int32

    The number of bytes received.

    Remarks

    The ReceiveFrom(Byte[], Int32, Int32, SocketFlags, ref EndPoint) method reads data into the buffer parameter, returns the number of bytes successfully read, and captures the remote host endpoint from which the data was sent. This method is useful if you intend to receive connectionless datagrams from an unknown host or multiple hosts.

    With connectionless protocols, ReceiveFrom(Byte[], Int32, Int32, SocketFlags, ref EndPoint) will read the first enqueued datagram received into the local network buffer.If the datagram you receive is larger than the size of buffer, the ReceiveFrom(Byte[], Int32, Int32, SocketFlags, ref EndPoint) method will fill buffer with as much of the message as is possible, and throw a SocketException.If you are using an unreliable protocol, the excess data will be lost.If you are using a reliable protocol, the excess data will be retained by the service provider and you can retrieve it by calling the ReceiveFrom(Byte[], Int32, Int32, SocketFlags, ref EndPoint) method with a large enough buffer.

    If no data is available for reading, the ReceiveFrom(Byte[], Int32, Int32, SocketFlags, ref EndPoint) method will block until data is available.If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the ReceiveFrom(Byte[], Int32, Int32, SocketFlags, ref EndPoint) method will complete immediately and throw a SocketException. You can use the Available property to determine if data is available for reading. When Available is non-zero, retry the receive operation.

    Although ReceiveFrom(Byte[], Int32, Int32, SocketFlags, ref EndPoint) is intended for connectionless protocols, you can use a connection-oriented protocol as well.If you choose to do so, you must first either establish a remote host connection by calling the Connect method or accept an incoming remote host connection by calling the Accept() method. If you do not establish or accept a connection before calling the ReceiveFrom(Byte[], Int32, Int32, SocketFlags, ref EndPoint) method, you will get a SocketException.You can also establish a default remote host for a connectionless protocol prior to calling the ReceiveFrom(Byte[], Int32, Int32, SocketFlags, ref EndPoint) method.In either of these cases, the ReceiveFrom(Byte[], Int32, Int32, SocketFlags, ref EndPoint) method will ignore the remoteEP parameter and only receive data from the connected or default remote host.

    With connection-oriented sockets, ReceiveFrom(Byte[], Int32, Int32, SocketFlags, ref EndPoint) will read as much data as is available up to the amount of bytes specified by the size parameter. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been Received, the ReceiveFrom(Byte[], Int32, Int32, SocketFlags, ref EndPoint) method will complete immediately and return zero bytes.

    | Improve this Doc View Source

    ReceiveFrom(Byte[], Int32, SocketFlags, ref EndPoint)

    Receives the specified number of bytes into the data buffer, using the specified SocketFlags, and stores the endpoint.

    Declaration
    public int ReceiveFrom(byte[] buffer, int size, SocketFlags socketFlags, ref EndPoint remoteEP)
    Parameters
    Type Name Description
    Byte[] buffer

    An array of type Byte that is the storage location for received data.

    Int32 size

    The number of bytes to receive.

    SocketFlags socketFlags

    A bitwise combination of the SocketFlags values.

    EndPoint remoteEP

    An EndPoint, passed by reference, that represents the remote server.

    Returns
    Type Description
    Int32

    The number of bytes received.

    | Improve this Doc View Source

    ReceiveFrom(Byte[], ref EndPoint)

    Receives a datagram into the data buffer and stores the endpoint.

    Declaration
    public int ReceiveFrom(byte[] buffer, ref EndPoint remoteEP)
    Parameters
    Type Name Description
    Byte[] buffer

    An array of type Byte that is the storage location for received data.

    EndPoint remoteEP

    An EndPoint, passed by reference, that represents the remote server.

    Returns
    Type Description
    Int32

    The number of bytes received.

    Remarks

    The method reads data into the buffer parameter, returns the number of bytes successfully read, and captures the remote host endpoint from which the data was sent. This method is useful if you intend to receive connectionless datagrams from an unknown host or multiple hosts.

    This overload only requires you to provide a receive buffer, and an EndPoint that represents the remote host. The buffer offset defaults to 0. The size defaults to the length of the buffer parameter and the socketFlags value defaults to None.

    important

    Before calling , you must explicitly Bind(EndPoint) the Socket to a local endpoint using the Bind(EndPoint) method. If you do not, will throw a SocketException.

    important

    The AddressFamily of the EndPoint used in needs to match the AddressFamily of the EndPoint used in .

    | Improve this Doc View Source

    ReceiveFrom(Byte[], SocketFlags, ref EndPoint)

    Receives a datagram into the data buffer, using the specified SocketFlags, and stores the endpoint.

    Declaration
    public int ReceiveFrom(byte[] buffer, SocketFlags socketFlags, ref EndPoint remoteEP)
    Parameters
    Type Name Description
    Byte[] buffer

    An array of type Byte that is the storage location for received data.

    SocketFlags socketFlags

    A bitwise combination of the SocketFlags values.

    EndPoint remoteEP

    An EndPoint, passed by reference, that represents the remote server.

    Returns
    Type Description
    Int32

    The number of bytes received.

    | Improve this Doc View Source

    Send(Byte[])

    Sends data to a connected Socket.

    Declaration
    public int Send(byte[] buffer)
    Parameters
    Type Name Description
    Byte[] buffer

    An array of type Byte that contains the data to be sent

    Returns
    Type Description
    Int32

    The number of bytes sent to the Socket.

    Remarks

    Send(Byte[]) synchronously sends data to the remote host specified in the Connect(EndPoint) or Accept() method and returns the number of bytes successfully sent. Send(Byte[]) can be used for both connection-oriented and connectionless protocols.

    This overload requires a buffer that contains the data you want to send. The SocketFlags value defaults to 0, the buffer offset defaults to 0, and the number of bytes to send defaults to the size of the buffer.

    If you are using a connectionless protocol, you must call Connect(EndPoint) before calling this method, or will throw a SocketException. If you are using a connection-oriented protocol, you must either use Connect(EndPoint) to establish a remote host connection, or use Accept() to accept an incoming connection.

    If you are using a connectionless protocol and plan to send data to several different hosts, you should use the SendTo(Byte[], EndPoint) method. If you do not use the SendTo method, you will have to call Connect before each call to Send. You can use SendTo even after you have established a default remote host with Connect. You can also change the default remote host prior to calling Send by making another call to Connect.

    If you are using a connection-oriented protocol, will block until all of the bytes in the buffer are sent, unless a time-out was set by using SendTimeout. If the time-out value was exceeded, the call will throw a SocketException. In nonblocking mode, Send may complete successfully even if it sends less than the number of bytes in the buffer. It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends the bytes in the buffer. There is also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. A successful completion of the Send(Byte[]) method means that the underlying system has had room to buffer your data for a network send.

    important

    If you receive a SocketException, use the ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

    important

    The successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

    See Also
    Socket
    System.Net.Sockets
    | Improve this Doc View Source

    Send(Byte[], Int32, Int32, SocketFlags)

    Sends the specified number of bytes of data to a connected Socket, starting at the specified offset, and using the specified SocketFlags.

    Declaration
    public int Send(byte[] buffer, int offset, int size, SocketFlags socketFlags)
    Parameters
    Type Name Description
    Byte[] buffer

    An array of type Byte that contains the data to be sent

    Int32 offset
    Int32 size
    SocketFlags socketFlags
    Returns
    Type Description
    Int32

    The number of bytes sent to the Socket.

    Remarks

    synchronously sends data to the remote host specified in the Connect(EndPoint) or Accept() method and returns the number of bytes successfully sent. can be used for both connection-oriented and connectionless protocols.

    In this overload, if you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

    important

    The successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

    See Also
    Socket
    System.Net.Sockets
    | Improve this Doc View Source

    Send(Byte[], Int32, SocketFlags)

    Sends the specified number of bytes of data to a connected Socket, using the specified SocketFlags.

    Declaration
    public int Send(byte[] buffer, int size, SocketFlags socketFlags)
    Parameters
    Type Name Description
    Byte[] buffer

    An array of type Byte that contains the data to be sent

    Int32 size
    SocketFlags socketFlags
    Returns
    Type Description
    Int32

    The number of bytes sent to the Socket.

    Remarks

    synchronously sends data to the remote host specified in the Connect(EndPoint) or Accept() method and returns the number of bytes successfully sent. can be used for both connection-oriented and connectionless protocols.

    This overload requires a buffer that contains the data you want to send, the number of bytes you want to send, and a bitwise combination of any SocketFlags. If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

    important

    You must ensure that the size of your buffer does not exceed the maximum packet size of the underlying service provider. If it does, the datagram will not be sent and will throw a SocketException. If you receive a SocketException, use the ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

    important

    The successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

    See Also
    Socket
    System.Net.Sockets
    | Improve this Doc View Source

    Send(Byte[], SocketFlags)

    Sends data to a connected Socket using the specified SocketFlags.

    Declaration
    public int Send(byte[] buffer, SocketFlags socketFlags)
    Parameters
    Type Name Description
    Byte[] buffer

    An array of type Byte that contains the data to be sent

    SocketFlags socketFlags
    Returns
    Type Description
    Int32

    The number of bytes sent to the Socket.

    Remarks

    synchronously sends data to the remote host specified in the Connect(EndPoint) or Accept() method and returns the number of bytes successfully sent. can be used for both connection-oriented and connectionless protocols.

    his overload requires a buffer that contains the data you want to send and a bitwise combination of SocketFlags. The buffer offset defaults to 0, and the number of bytes to send defaults to the size of the buffer. If you specify the DontRoute flag as the socketflags parameter value, the data you are sending will not be routed.

    important

    You must ensure that the size of your buffer does not exceed the maximum packet size of the underlying service provider. If it does, the datagram will not be sent and will throw a SocketException. If you receive a SocketException, use the ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

    important

    The successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

    See Also
    Socket
    System.Net.Sockets
    | Improve this Doc View Source

    SendTo(Byte[], Int32, Int32, SocketFlags, EndPoint)

    Sends data to the specified endpoint.

    Declaration
    public int SendTo(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint remoteEP)
    Parameters
    Type Name Description
    Byte[] buffer

    An array of type Byte that contains the data to be sent.

    Int32 offset

    The EndPoint that represents the destination for the data.

    Int32 size

    The number of bytes to send.

    SocketFlags socketFlags

    A bitwise combination of the SocketFlags values.

    EndPoint remoteEP

    The EndPoint that represents the destination location for the data.

    Returns
    Type Description
    Int32

    The number of bytes sent.

    | Improve this Doc View Source

    SendTo(Byte[], Int32, SocketFlags, EndPoint)

    Sends the specified number of bytes of data to the specified endpoint using the specified SocketFlags.

    Declaration
    public int SendTo(byte[] buffer, int size, SocketFlags socketFlags, EndPoint remoteEP)
    Parameters
    Type Name Description
    Byte[] buffer

    An array of type Byte that contains the data to be sent.

    Int32 size

    The number of bytes to send.

    SocketFlags socketFlags

    A bitwise combination of the SocketFlags values.

    EndPoint remoteEP

    The EndPoint that represents the destination location for the data.

    Returns
    Type Description
    Int32

    The number of bytes sent.

    | Improve this Doc View Source

    SendTo(Byte[], EndPoint)

    Sends data to the specified endpoint.

    Declaration
    public int SendTo(byte[] buffer, EndPoint remoteEP)
    Parameters
    Type Name Description
    Byte[] buffer

    n array of type Byte that contains the data to be sent.

    EndPoint remoteEP

    The EndPoint that represents the destination location for the data.

    Returns
    Type Description
    Int32

    The number of bytes sent.

    Remarks

    In this overload, the buffer offset defaults to 0, the number of bytes to send defaults to the size of the buffer parameter, and the SocketFlags value defaults to 0.

    | Improve this Doc View Source

    SendTo(Byte[], SocketFlags, EndPoint)

    Sends data to a specific endpoint using the specified SocketFlags.

    Declaration
    public int SendTo(byte[] buffer, SocketFlags socketFlags, EndPoint remoteEP)
    Parameters
    Type Name Description
    Byte[] buffer

    An array of type Byte that contains the data to be sent.

    SocketFlags socketFlags

    A bitwise combination of the SocketFlags values.

    EndPoint remoteEP

    The EndPoint that represents the destination location for the data.

    Returns
    Type Description
    Int32

    The number of bytes sent.

    Remarks

    In this overload, the buffer offset defaults to 0, and the number of bytes to send defaults to the size of the buffer. If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

    | Improve this Doc View Source

    SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean)

    Sets the specified Socket option to the specified Boolean value.

    Declaration
    public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, bool optionValue)
    Parameters
    Type Name Description
    SocketOptionLevel optionLevel

    One of the SocketOptionLevel values.

    SocketOptionName optionName

    One of the SocketOptionName values.

    Boolean optionValue

    The value of the option, represented as a Boolean.

    | Improve this Doc View Source

    SetSocketOption(SocketOptionLevel, SocketOptionName, Byte[])

    Sets the specified Socket option to the specified value, represented as a byte array.

    Declaration
    public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue)
    Parameters
    Type Name Description
    SocketOptionLevel optionLevel

    One of the SocketOptionLevel values.

    SocketOptionName optionName

    One of the SocketOptionName values.

    Byte[] optionValue

    An array of type Byte that represents the value of the option.

    Remarks

    Socket options determine the behavior of the current Socket. Use this overload to set those Socket options that require a byte array as an option value.

    | Improve this Doc View Source

    SetSocketOption(SocketOptionLevel, SocketOptionName, Int32)

    Sets the specified Socket option to the specified integer value.

    Declaration
    public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue)
    Parameters
    Type Name Description
    SocketOptionLevel optionLevel

    One of the SocketOptionLevel values.

    SocketOptionName optionName

    One of the SocketOptionName values.

    Int32 optionValue

    A value of the option.

    Remarks

    Socket options determine the behavior of the current Socket. For an option with a Boolean data type, specify a nonzero value to enable the option, and a zero value to disable the option. For an option with an integer data type, specify the appropriate value. Socket options are grouped by level of protocol support.

    For Linger option the optionValue it's the number of seconds that the socket will linger before closing the connection. To disable socket linger call SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean) with DontLinger and setting it to true.

    Explicit Interface Implementations

    | Improve this Doc View Source

    IDisposable.Dispose()

    Declaration
    void IDisposable.Dispose()

    Implements

    IDisposable

    Extension Methods

    LogDispatcher.GetCurrentClassLogger(Object)
    • Improve this Doc
    • View Source
    Back to top Copyright © 2018 nanoFramework Contributors
    Generated by DocFX