TcpListener.AcceptSocket Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Accepts a pending connection request.
public:
System::Net::Sockets::Socket ^ AcceptSocket();
public System.Net.Sockets.Socket AcceptSocket ();
member this.AcceptSocket : unit -> System.Net.Sockets.Socket
Public Function AcceptSocket () As Socket
Returns
A Socket used to send and receive data.
Exceptions
The listener has not been started with a call to Start().
Examples
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.
array<Byte>^sendBytes = Encoding::ASCII->GetBytes( responseString );
int i = socket->Send( sendBytes );
Console::WriteLine( "Message Sent /> : {0}", responseString );
// Accepts the pending client connection and returns a socket for communication.
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.
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))
Remarks
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.
Note
This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.