System.Net.Sockets.Socket.Accept Method

Creates a new System.Net.Sockets.Socket for a newly created connection.

Syntax

public Socket Accept ()

Returns

A System.Net.Sockets.Socket for a newly created connection.

Exceptions

TypeReason
ArgumentExceptionAn error occurred while creating the new System.Net.Sockets.Socket.
InvalidOperationExceptionAn asynchronous call is pending and a blocking method has been called.
System.Net.Sockets.SocketException

An error occurred while accessing the listening socket or while creating the new socket.

-or-

The Socket.Blocking property is set to false.

Note: For additional information on causes of the SocketException, see the System.Net.Sockets.SocketException class.

ObjectDisposedExceptionThe current instance has been disposed.

Remarks

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

In blocking mode, Socket.Accept blocks until an incoming connection attempt is queued. Once a connection is accepted, the original System.Net.Sockets.Socket continues queuing incoming connection requests until you close it.

If you call this method using a non-blocking System.Net.Sockets.Socket, and no connection requests are queued, Socket.Accept throws a System.Net.Sockets.SocketException. If you receive a System.Net.Sockets.SocketException, use the SocketException.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.

Note:

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

Note:

This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0