请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
先前版本
TcpListener 类
 AcceptSocket 方法
全部折叠/全部展开 全部折叠
此页面仅适用于
Microsoft Visual Studio 2005/.NET Framework 2.0

同时提供下列产品的其他版本:
.NET Framework 类库
TcpListener.AcceptSocket 方法

接受挂起的连接请求。

命名空间:System.Net.Sockets
程序集:System(在 system.dll 中)

Visual Basic(声明)
Public Function AcceptSocket As Socket
Visual Basic(用法)
Dim instance As TcpListener
Dim returnValue As Socket

returnValue = instance.AcceptSocket
C#
public Socket AcceptSocket ()
C++
public:
Socket^ AcceptSocket ()
J#
public Socket AcceptSocket ()
JScript
public function AcceptSocket () : Socket

返回值

用于发送和接收数据的 Socket
异常类型条件

InvalidOperationException

尚未通过调用 Start 来启动该侦听器。

AcceptSocket 是一个阻止方法,该方法返回可用于发送和接收数据的 Socket。如果希望避免阻止,请使用 Pending 方法来确定传入连接队列中的连接请求是否可用。

返回的 Socket 是使用远程主机的 IP 地址和端口号初始化的。您可以使用 Socket 类中任何可用的 SendReceive 方法与远程主机进行通信。使用 Socket 后,请确保调用其 Close 方法。如果应用程序相对简单,请考虑使用 AcceptTcpClient 方法代替 AcceptSocket 方法。TcpClient 提供了一些通过网络在阻止同步模式下发送和接收数据的简单方法。

Note注意

当在应用程序中启用网络跟踪功能后,此成员将输出跟踪信息。有关更多信息,请参见 网络跟踪

在下面的代码示例中,AcceptSocket 方法用于返回 Socket。此 Socket 用于和新连接上的客户端通信。

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( "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 );
J#
// 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 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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利 | 商标 | 隐私权声明
Page view tracker