Table of Contents

Class Socket

Namespace
System.Net.Sockets
Assembly
System.Net.dll

Implements the Berkeley sockets interface.

public class Socket : IDisposable
Inheritance
Socket
Implements
Inherited Members
Extension Methods

Constructors

Socket(AddressFamily, SocketType, ProtocolType)

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

public Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)

Parameters

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 results in an invalid Socket, this constructor throws a SocketException.

Properties

Available

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

public int Available { get; }

Property Value

int

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.

LocalEndPoint

Gets the local endpoint.

public EndPoint LocalEndPoint { get; }

Property Value

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.

ReceiveTimeout

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

public int ReceiveTimeout { get; set; }

Property Value

int

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.

RemoteEndPoint

Gets the remote endpoint.

public EndPoint RemoteEndPoint { get; }

Property Value

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.

SendTimeout

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

public int SendTimeout { get; set; }

Property Value

int

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.

SocketType

Gets the type of the Socket.

public SocketType SocketType { get; }

Property Value

SocketType

One of the SocketType values.

Remarks

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

Methods

Accept()

Creates a new Socket for a newly created connection.

public Socket Accept()

Returns

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.

Bind(EndPoint)

Associates a Socket with a local endpoint.

public void Bind(EndPoint localEP)

Parameters

localEP EndPoint

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(int) 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.

Close()

Closes the Socket connection and releases all associated resources.

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.

Connect(EndPoint)

Establishes a connection to a remote host.

public void Connect(EndPoint remoteEP)

Parameters

remoteEP EndPoint

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[], int, int, 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, bool) 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.

Dispose(bool)

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

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

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.

Finalize()

protected override void Finalize()

GetSocketOption(SocketOptionLevel, SocketOptionName)

Returns the value of a Socket option.

public object GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName)

Parameters

optionLevel SocketOptionLevel

One of the SocketOptionLevel values.

optionName SocketOptionName

One of the SocketOptionName values.

Returns

object

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

When the optionName parameter is set to Linger the return value is an int 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 bool.

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

Exceptions

NotSupportedException

When using an SocketOptionName that can't be retrieved.

GetSocketOption(SocketOptionLevel, SocketOptionName, byte[])

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

public void GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] val)

Parameters

optionLevel SocketOptionLevel

One of the SocketOptionLevel values.

optionName SocketOptionName

One of the SocketOptionName values.

val byte[]

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.

Listen(int)

Places a Socket in a listening state.

public void Listen(int backlog)

Parameters

backlog int

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.

Poll(int, SelectMode)

Determines the status of the Socket.

public bool Poll(int microSeconds, SelectMode mode)

Parameters

microSeconds int

The time to wait for a response, in microseconds.

mode SelectMode

One of the SelectMode values.

Returns

bool

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

  • SelectRead SelectWrite SelectError

    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.

      <p>true , if processing a Connect, and the connection has succeeded;</p>
      <p>-or-</p>
      <p>true if data can be sent; otherwise, returns false.</p>
    
      <p>true if processing a <xref href="System.Net.Sockets.Socket.Connect(System.Net.EndPoint)" data-throw-if-not-resolved="false"></xref> that does not block, and the connection has failed;</p>
      <p>-or-</p>
      <p>true if <xref href="System.Net.Sockets.SocketOptionName.OutOfBandInline" data-throw-if-not-resolved="false"></xref> is not set and out-of-band data is available; otherwise, returns false.</p>
    </li></ul>
    

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.

Receive(byte[])

Receives data from a bound Socket into a receive buffer.

public int Receive(byte[] buffer)

Parameters

buffer byte[]

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

Returns

int

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.

Receive(byte[], int, int, SocketFlags)

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

public int Receive(byte[] buffer, int offset, int size, SocketFlags socketFlags)

Parameters

buffer byte[]

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

offset int

The location in buffer to store the received data.

size int

The number of bytes to receive.

socketFlags SocketFlags

A bitwise combination of the SocketFlags values.

Returns

int

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.

Receive(byte[], int, SocketFlags)

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

public int Receive(byte[] buffer, int size, SocketFlags socketFlags)

Parameters

buffer byte[]

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

size int

The number of bytes to receive.

socketFlags SocketFlags

A bitwise combination of the SocketFlags values.

Returns

int

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.

Receive(byte[], SocketFlags)

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

public int Receive(byte[] buffer, SocketFlags socketFlags)

Parameters

buffer byte[]

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

socketFlags SocketFlags

A bitwise combination of the SocketFlags values.

Returns

int

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.

ReceiveFrom(byte[], int, int, 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.

public int ReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP)

Parameters

buffer byte[]

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

offset int

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

size int

The number of bytes to receive.

socketFlags SocketFlags

A bitwise combination of the SocketFlags values.

remoteEP EndPoint

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

Returns

int

The number of bytes received.

Remarks

The ReceiveFrom(byte[], int, int, 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[], int, int, 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[], int, int, 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[], int, int, SocketFlags, ref EndPoint) method with a large enough buffer.

If no data is available for reading, the ReceiveFrom(byte[], int, int, 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[], int, int, 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[], int, int, 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[], int, int, 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[], int, int, SocketFlags, ref EndPoint) method.In either of these cases, the ReceiveFrom(byte[], int, int, 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[], int, int, 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[], int, int, SocketFlags, ref EndPoint) method will complete immediately and return zero bytes.

ReceiveFrom(byte[], int, SocketFlags, ref EndPoint)

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

public int ReceiveFrom(byte[] buffer, int size, SocketFlags socketFlags, ref EndPoint remoteEP)

Parameters

buffer byte[]

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

size int

The number of bytes to receive.

socketFlags SocketFlags

A bitwise combination of the SocketFlags values.

remoteEP EndPoint

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

Returns

int

The number of bytes received.

ReceiveFrom(byte[], ref EndPoint)

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

public int ReceiveFrom(byte[] buffer, ref EndPoint remoteEP)

Parameters

buffer byte[]

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

remoteEP EndPoint

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

Returns

int

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 .

ReceiveFrom(byte[], SocketFlags, ref EndPoint)

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

public int ReceiveFrom(byte[] buffer, SocketFlags socketFlags, ref EndPoint remoteEP)

Parameters

buffer byte[]

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

socketFlags SocketFlags

A bitwise combination of the SocketFlags values.

remoteEP EndPoint

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

Returns

int

The number of bytes received.

Send(byte[])

Sends data to a connected Socket.

public int Send(byte[] buffer)

Parameters

buffer byte[]

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

Returns

int

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

Send(byte[], int, int, SocketFlags)

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

public int Send(byte[] buffer, int offset, int size, SocketFlags socketFlags)

Parameters

buffer byte[]

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

offset int
size int
socketFlags SocketFlags

Returns

int

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

Send(byte[], int, SocketFlags)

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

public int Send(byte[] buffer, int size, SocketFlags socketFlags)

Parameters

buffer byte[]

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

size int
socketFlags SocketFlags

Returns

int

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

Send(byte[], SocketFlags)

Sends data to a connected Socket using the specified SocketFlags.

public int Send(byte[] buffer, SocketFlags socketFlags)

Parameters

buffer byte[]

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

socketFlags SocketFlags

Returns

int

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

SendTo(byte[], int, int, SocketFlags, EndPoint)

Sends data to the specified endpoint.

public int SendTo(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint remoteEP)

Parameters

buffer byte[]

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

offset int

The EndPoint that represents the destination for the data.

size int

The number of bytes to send.

socketFlags SocketFlags

A bitwise combination of the SocketFlags values.

remoteEP EndPoint

The EndPoint that represents the destination location for the data.

Returns

int

The number of bytes sent.

SendTo(byte[], int, SocketFlags, EndPoint)

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

public int SendTo(byte[] buffer, int size, SocketFlags socketFlags, EndPoint remoteEP)

Parameters

buffer byte[]

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

size int

The number of bytes to send.

socketFlags SocketFlags

A bitwise combination of the SocketFlags values.

remoteEP EndPoint

The EndPoint that represents the destination location for the data.

Returns

int

The number of bytes sent.

SendTo(byte[], EndPoint)

Sends data to the specified endpoint.

public int SendTo(byte[] buffer, EndPoint remoteEP)

Parameters

buffer byte[]

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

remoteEP EndPoint

The EndPoint that represents the destination location for the data.

Returns

int

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.

SendTo(byte[], SocketFlags, EndPoint)

Sends data to a specific endpoint using the specified SocketFlags.

public int SendTo(byte[] buffer, SocketFlags socketFlags, EndPoint remoteEP)

Parameters

buffer byte[]

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

socketFlags SocketFlags

A bitwise combination of the SocketFlags values.

remoteEP EndPoint

The EndPoint that represents the destination location for the data.

Returns

int

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.

SetSocketOption(SocketOptionLevel, SocketOptionName, bool)

Sets the specified Socket option to the specified Boolean value.

public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, bool optionValue)

Parameters

optionLevel SocketOptionLevel

One of the SocketOptionLevel values.

optionName SocketOptionName

One of the SocketOptionName values.

optionValue bool

The value of the option, represented as a Boolean.

SetSocketOption(SocketOptionLevel, SocketOptionName, byte[])

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

public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue)

Parameters

optionLevel SocketOptionLevel

One of the SocketOptionLevel values.

optionName SocketOptionName

One of the SocketOptionName values.

optionValue byte[]

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.

SetSocketOption(SocketOptionLevel, SocketOptionName, int)

Sets the specified Socket option to the specified integer value.

public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue)

Parameters

optionLevel SocketOptionLevel

One of the SocketOptionLevel values.

optionName SocketOptionName

One of the SocketOptionName values.

optionValue int

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, bool) with DontLinger and setting it to true.