Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

TcpListener.AcceptSocket Method

Accepts a pending connection request.

Namespace: System.Net.Sockets
Assembly: System (in system.dll)

public Socket AcceptSocket ()
public Socket AcceptSocket ()
public function AcceptSocket () : Socket
Not applicable.

Return Value

A Socket used to send and receive data.

Exception typeCondition

InvalidOperationException

The listener has not been started with a call to Start.

AcceptSocket is a blocking method that returns a Socket that you can use to send and receive data. If you want to avoid blocking, use the Pending method to determine if connection 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.

NoteNote:

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing.

In the following code example, the AcceptSocket method is used to return a Socket. This Socket is used to communicate with the newly connected client.

           
// 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);

// 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.
ubyte sendBytes[] = Encoding.get_ASCII().GetBytes(
                        responseString);
int i = socket.Send(sendBytes);
Console.WriteLine(("Message Sent /> : "
    + responseString));

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft