gpt4 book ai didi

c# - 关闭断开的异步套接字会使监听器停止监听吗?

转载 作者:行者123 更新时间:2023-12-03 12:06:15 32 4
gpt4 key购买 nike

在以下摘录自msdn的示例中:

public static void StartListening() {
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];

// Establish the local endpoint for the socket.
// The DNS name of the computer
// running the listener is "host.contoso.com".
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp );

// Bind the socket to the local endpoint and listen for incoming connections.
try {
listener.Bind(localEndPoint);
listener.Listen(100);

while (true) {
// Set the event to nonsignaled state.
allDone.Reset();

// Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener );

// Wait until a connection is made before continuing.
allDone.WaitOne();
}

} catch (Exception e) {
Console.WriteLine(e.ToString());
}

Console.WriteLine("\nPress ENTER to continue...");
Console.Read();

}

public static void AcceptCallback(IAsyncResult ar) {
// Signal the main thread to continue.
allDone.Set();

// Get the socket that handles the client request.
Socket listener = (Socket) ar.AsyncState;
Socket handler = listener.EndAccept(ar);

// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}

关闭在AcceptCallback方法中从AsyncState创建的套接字是否会关闭在StartListening方法中创建的监听器套接字?

真的,我的问题是,如果我剥离线程池上的套接字接收,对数据做些什么,然后关闭套接字,那么实际上是关闭整个服务器还是仅关闭与该客户端的连接?

最佳答案

每个接受的连接都会获得一个新的套接字。完成后,将其关闭。收听者将继续收听。

关于c# - 关闭断开的异步套接字会使监听器停止监听吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9774440/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com