Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TcpListener.AcceptSocket Method

Accepts a pending connection request.

[Visual Basic]
Public Function AcceptSocket() As Socket
[C#]
public Socket AcceptSocket();
[C++]
public: Socket* AcceptSocket();
[JScript]
public function AcceptSocket() : Socket;

Return Value

A Socket used to send and receive data.

Exceptions

Exception Type Condition
InvalidOperationException The listener has not been started with a call to Start.

Remarks

AcceptSocket is a blocking method that returns a Socket you can use to send and receive data. If you want to avoid blocking, use the Pending method to determine if connections requests are available in the incoming connection queue.

The Socket returned is initialized with the IP address and port number of the remote host. You can use any of the Send and Receive methods available in the Socket class to communicate with the remote host. When you are finished using the Socket, be sure to call its Close method. If your application is relatively simple, consider using the AcceptTcpClient method rather than the AcceptSocket method. TcpClient provides you with simple methods for sending and receiving data over a network in blocking synchronous mode.

Example

[Visual Basic, C#, C++] In the following example, AcceptSocket is used to return a Socket. This Socket is used to communicate with the newly connected client.

[Visual Basic] 
' Accepts the pending client connection and returns a socket for communciation.
Dim socket As Socket = tcpListener.AcceptSocket()
Console.WriteLine("Connection accepted.")

Dim responseString As String = "You have successfully connected to me"

'Forms and sends a response string to the connected client.
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
Dim i As Integer = socket.Send(sendBytes)
Console.WriteLine(("Message Sent /> : " + responseString))

[C#] 

   // Accepts the pending client connection and returns a socket for communciation.
    Socket socket = tcpListener.AcceptSocket();
      Console.WriteLine("Connection accepted.");

    string responseString = "You have successfully connected to me";

    //Forms and sends a response string to the connected client.
    Byte[] sendBytes = Encoding.ASCII.GetBytes(responseString);
    int i = socket.Send(sendBytes);
    Console.WriteLine("Message Sent /> : " + responseString);

[C++] 

// Accepts the pending client connection and returns a socket for communciation.
Socket* socket = tcpListener->AcceptSocket();
Console::WriteLine(S"Connection accepted.");

String* responseString = S"You have successfully connected to me";

//Forms and sends a response string to the connected client.
Byte sendBytes[] = Encoding::ASCII->GetBytes(responseString);
int i = socket->Send(sendBytes);
Console::WriteLine(S"Message Sent /> : {0}", responseString);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

TcpListener Class | TcpListener Members | System.Net.Sockets Namespace | AcceptTcpClient | Socket | Pending

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft