Show / Hide Table of Contents

Class SocketExtensions

Extension methods for Socket class.

Inheritance
Object
SocketExtensions
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: UnityFx.Async.Extensions
Assembly: UnityFx.Async-netstandard2.0.dll
Syntax
public static class SocketExtensions

Methods

| Improve this Doc View Source

AcceptAsync(Socket)

Begins an asynchronous operation to accept an incoming connection attempt.

Declaration
public static IAsyncOperation<Socket> AcceptAsync(this Socket socket)
Parameters
Type Name Description
Socket socket

The target socket.

Returns
Type Description
IAsyncOperation<Socket>

Returns IAsyncOperation<TResult> representing the asynchronous operation.

Exceptions
Type Condition
SocketException

An error occurred when attempting to access the socket.

ObjectDisposedException

Thrown if the socket has been closed.

| Improve this Doc View Source

AcceptAsync(Socket, Int32)

Begins an asynchronous operation to accept an incoming connection attempt and receives the first block of data sent by the client application.

Declaration
public static IAsyncOperation<Socket> AcceptAsync(this Socket socket, int receiveSize)
Parameters
Type Name Description
Socket socket

The target socket.

Int32 receiveSize

The maximum number of bytes to receive.

Returns
Type Description
IAsyncOperation<Socket>

Returns IAsyncOperation<TResult> representing the asynchronous operation.

Exceptions
Type Condition
SocketException

An error occurred when attempting to access the socket.

ObjectDisposedException

Thrown if the socket has been closed.

| Improve this Doc View Source

AcceptAsync(Socket, Socket, Int32)

Begins an asynchronous operation to accept an incoming connection attempt from a specified socket and receives the first block of data sent by the client application.

Declaration
public static IAsyncOperation<Socket> AcceptAsync(this Socket socket, Socket acceptSocket, int receiveSize)
Parameters
Type Name Description
Socket socket

The target socket.

Socket acceptSocket

The accepted Socket object. This value may be null.

Int32 receiveSize

The maximum number of bytes to receive.

Returns
Type Description
IAsyncOperation<Socket>

Returns IAsyncOperation<TResult> representing the asynchronous operation.

Exceptions
Type Condition
SocketException

An error occurred when attempting to access the socket.

ObjectDisposedException

Thrown if the socket has been closed.

| Improve this Doc View Source

ConnectAsync(Socket, EndPoint)

Begins an asynchronous request for a remote host connection.

Declaration
public static IAsyncOperation ConnectAsync(this Socket socket, EndPoint remoteEP)
Parameters
Type Name Description
Socket socket

The target socket.

EndPoint remoteEP

An EndPoint that represents the remote host.

Returns
Type Description
IAsyncOperation

Returns IAsyncOperation representing the asynchronous operation.

Exceptions
Type Condition
ArgumentNullException

Thrown if remoteEP is null.

InvalidOperationException

The socket is listening.

SocketException

An error occurred when attempting to access the socket.

ObjectDisposedException

Thrown if the socket has been closed.

| Improve this Doc View Source

ConnectAsync(Socket, IPAddress, Int32)

Begins an asynchronous request for a remote host connection. The host is specified by an IPAddress and a port number.

Declaration
public static IAsyncOperation ConnectAsync(this Socket socket, IPAddress address, int port)
Parameters
Type Name Description
Socket socket

The target socket.

IPAddress address

The IPAddress of the remote host.

Int32 port

The port number of the remote host.

Returns
Type Description
IAsyncOperation

Returns IAsyncOperation representing the asynchronous operation.

Exceptions
Type Condition
ArgumentNullException

Thrown if address is null.

ArgumentOutOfRangeException

Thrown if port number is invalid.

InvalidOperationException

The socket is listening.

SocketException

An error occurred when attempting to access the socket.

ObjectDisposedException

Thrown if the socket has been closed.

| Improve this Doc View Source

ConnectAsync(Socket, IPAddress[], Int32)

Begins an asynchronous request for a remote host connection. The host is specified by an IPAddress array and a port number.

Declaration
public static IAsyncOperation ConnectAsync(this Socket socket, IPAddress[] addresses, int port)
Parameters
Type Name Description
Socket socket

The target socket.

IPAddress[] addresses

At least one IPAddress, designating the remote host.

Int32 port

The port number of the remote host.

Returns
Type Description
IAsyncOperation

Returns IAsyncOperation representing the asynchronous operation.

Exceptions
Type Condition
ArgumentNullException

Thrown if addresses is null.

ArgumentOutOfRangeException

Thrown if port number is invalid.

InvalidOperationException

The socket is listening.

SocketException

An error occurred when attempting to access the socket.

ObjectDisposedException

Thrown if the socket has been closed.

| Improve this Doc View Source

ConnectAsync(Socket, String, Int32)

Begins an asynchronous request for a remote host connection. The host is specified by a host name and a port number.

Declaration
public static IAsyncOperation ConnectAsync(this Socket socket, string host, int port)
Parameters
Type Name Description
Socket socket

The target socket.

String host

The name of the remote host.

Int32 port

The port number of the remote host.

Returns
Type Description
IAsyncOperation

Returns IAsyncOperation representing the asynchronous operation.

Exceptions
Type Condition
ArgumentNullException

Thrown if host is null.

ArgumentOutOfRangeException

Thrown if port number is invalid.

InvalidOperationException

The socket is listening.

SocketException

An error occurred when attempting to access the socket.

ObjectDisposedException

Thrown if the socket has been closed.

| Improve this Doc View Source

DisconnectAsync(Socket, Boolean)

Begins an asynchronous request to disconnect from a remote endpoint.

Declaration
public static IAsyncOperation DisconnectAsync(this Socket socket, bool reuseSocket)
Parameters
Type Name Description
Socket socket

The target socket.

Boolean reuseSocket

true if this socket can be reused after the connection is closed; otherwise, false.

Returns
Type Description
IAsyncOperation

Returns IAsyncOperation representing the asynchronous operation.

Exceptions
Type Condition
SocketException

An error occurred when attempting to access the socket.

NotSupportedException

The operating system is Windows 2000 or earlier, and this method requires Windows XP.

ObjectDisposedException

Thrown if the socket has been closed.

| Improve this Doc View Source

ReceiveAsync(Socket, Byte[], Int32, Int32, SocketFlags)

Begins to asynchronously receive data from a connected Socket.

Declaration
public static IAsyncOperation<int> ReceiveAsync(this Socket socket, byte[] buffer, int offset, int size, SocketFlags socketFlags)
Parameters
Type Name Description
Socket socket

The target socket.

System.Byte[] buffer

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

Int32 offset

The zero-based position in the buffer at which 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
IAsyncOperation<Int32>

Returns IAsyncOperation<TResult> representing the asynchronous operation.

Exceptions
Type Condition
ArgumentNullException

Thrown if buffer is null.

ArgumentOutOfRangeException

Thrown if offset is less than 0 or offset is greater than the length of buffer or size is less than 0 or size is greater than the length of buffer minus the value of the offset parameter.

SocketException

An error occurred when attempting to access the socket.

ObjectDisposedException

Thrown if the socket has been closed.

| Improve this Doc View Source

ReceiveAsync(Socket, IList<ArraySegment<Byte>>, SocketFlags)

Begins to asynchronously receive data from a connected Socket.

Declaration
public static IAsyncOperation<int> ReceiveAsync(this Socket socket, IList<ArraySegment<byte>> buffers, SocketFlags socketFlags)
Parameters
Type Name Description
Socket socket

The target socket.

System.Collections.Generic.IList<System.ArraySegment<System.Byte>> buffers

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

SocketFlags socketFlags

A bitwise combination of the SocketFlags values.

Returns
Type Description
IAsyncOperation<Int32>

Returns IAsyncOperation<TResult> representing the asynchronous operation.

Exceptions
Type Condition
ArgumentNullException

Thrown if buffers is null.

SocketException

An error occurred when attempting to access the socket.

ObjectDisposedException

Thrown if the socket has been closed.

| Improve this Doc View Source

SendAsync(Socket, Byte[], Int32, Int32, SocketFlags)

Sends data asynchronously to a connected Socket.

Declaration
public static IAsyncOperation<int> SendAsync(this Socket socket, byte[] buffer, int offset, int size, SocketFlags socketFlags)
Parameters
Type Name Description
Socket socket

The target socket.

System.Byte[] buffer

An array of bytes that contains the data to send.

Int32 offset

The zero-based position in the buffer at which to begin sending data.

Int32 size

The number of bytes to send.

SocketFlags socketFlags

A bitwise combination of the SocketFlags values.

Returns
Type Description
IAsyncOperation<Int32>

Returns IAsyncOperation<TResult> representing the asynchronous operation.

Exceptions
Type Condition
ArgumentNullException

Thrown if buffer is null.

ArgumentOutOfRangeException

Thrown if offset is less than 0 or offset is less than the length of buffer or size is less than 0 or size is greater than the length of buffer minus the value of the offset parameter.

SocketException

An error occurred when attempting to access the socket.

ObjectDisposedException

Thrown if the socket has been closed.

| Improve this Doc View Source

SendAsync(Socket, IList<ArraySegment<Byte>>, SocketFlags)

Sends data asynchronously to a connected Socket.

Declaration
public static IAsyncOperation<int> SendAsync(this Socket socket, IList<ArraySegment<byte>> buffers, SocketFlags socketFlags)
Parameters
Type Name Description
Socket socket

The target socket.

System.Collections.Generic.IList<System.ArraySegment<System.Byte>> buffers

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

SocketFlags socketFlags

A bitwise combination of the SocketFlags values.

Returns
Type Description
IAsyncOperation<Int32>

Returns IAsyncOperation<TResult> representing the asynchronous operation.

Exceptions
Type Condition
ArgumentNullException

Thrown if buffers is null.

SocketException

An error occurred when attempting to access the socket.

ObjectDisposedException

Thrown if the socket has been closed.

| Improve this Doc View Source

SendFileAsync(Socket, String)

Sends the file fileName to a connected Socket object using the UseDefaultWorkerThread flag.

Declaration
public static IAsyncOperation SendFileAsync(this Socket socket, string fileName)
Parameters
Type Name Description
Socket socket

The target socket.

String fileName

A string that contains the path and name of the file to send. This parameter can be null.

Returns
Type Description
IAsyncOperation

Returns IAsyncOperation representing the asynchronous operation.

Exceptions
Type Condition
FileNotFoundException

The file fileName was not found.

SocketException

An error occurred when attempting to access the socket.

NotSupportedException

The operating system is not Windows NT or later or rhe socket is not connected to a remote host.

ObjectDisposedException

Thrown if the socket has been closed.

| Improve this Doc View Source

SendFileAsync(Socket, String, Byte[], Byte[], TransmitFileOptions)

Sends the file fileName to a connected Socket object.

Declaration
public static IAsyncOperation SendFileAsync(this Socket socket, string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags)
Parameters
Type Name Description
Socket socket

The target socket.

String fileName

A string that contains the path and name of the file to send. This parameter can be null.

System.Byte[] preBuffer

A byte array that contains data to be sent before the file is sent. This parameter can be null.

System.Byte[] postBuffer

A byte array that contains data to be sent after the file is sent. This parameter can be null.

TransmitFileOptions flags

A bitwise combination of TransmitFileOptions values.

Returns
Type Description
IAsyncOperation

Returns IAsyncOperation representing the asynchronous operation.

Exceptions
Type Condition
FileNotFoundException

The file fileName was not found.

SocketException

An error occurred when attempting to access the socket.

NotSupportedException

The operating system is not Windows NT or later or rhe socket is not connected to a remote host.

ObjectDisposedException

Thrown if the socket has been closed.

  • Improve this Doc
  • View Source
Back to top Copyright (c) 2017-2018 Alexander Bogarsukov