正确停止异步 TCP 应用程序
您好,我的服务器应用程序中有一个断开连接按钮单击事件,如下所示。在它 DC 之前,它将通过发送“/exit”命令来提醒其他客户端。之后它将关闭其连接。
private void stopButton_Click(object sender, EventArgs e)
{
byte[] exit_command = Encoding.ASCII.GetBytes("/exit");
g_server_conn.BeginSend(exit_command, 0, exit_command.Length, SocketFlags.None, new AsyncCallback(Send), g_server_conn);
g_server_conn.Shutdown(SocketShutdown.Both);
g_server_conn.Close();
}
问题是服务器一直在执行 Socket.BeginRecieve() 方法。我们如何告诉开始接收方法停止其操作,以便我可以正确关闭。
私有无效接受(IAsyncResult iar) {
Socket winsock = (Socket)iar.AsyncState;
g_server_conn = winsock.EndAccept(iar);
//Function that exchanges names of each other
NewClient(g_server_conn);
Socket server_conn = g_server_conn;
chat_msg = new byte[1024];
server_conn.BeginReceive(chat_msg, 0, chat_msg.Length, SocketFlags.None, new AsyncCallback(Recieve), server_conn);
}
private void Recieve(IAsyncResult iar)
{
Socket server_conn = (Socket)iar.AsyncState;
server_conn.EndReceive(iar);
//If clients shutdown connection,Server recieves /exit command
if (Encoding.ASCII.GetString(chat_msg, 0, chat_msg.Length) == "/exit")
{
g_server_conn.Shutdown(SocketShutdown.Both);
g_server_conn.Close();
return;
}
SetLabel(client_name, chatListBox);
SetLabel(Encoding.ASCII.GetString(chat_msg), chatListBox);
chat_msg = new byte[1024];
server_conn.BeginReceive(chat_msg, 0, chat_msg.Length, SocketFlags.None, new AsyncCallback(Recieve), server_conn);
}
Hi i have a disconnect button click event in my SERVER application as follows.Before it dc, it will alert other clients by sending a "/exit" command.After that it will shutdown its connection.
private void stopButton_Click(object sender, EventArgs e)
{
byte[] exit_command = Encoding.ASCII.GetBytes("/exit");
g_server_conn.BeginSend(exit_command, 0, exit_command.Length, SocketFlags.None, new AsyncCallback(Send), g_server_conn);
g_server_conn.Shutdown(SocketShutdown.Both);
g_server_conn.Close();
}
The problem is that the server is executing Socket.BeginRecieve() method all the time. How do we tell the begin recieve method to stop its operation so that i can close properly.
private void Accept(IAsyncResult iar)
{
Socket winsock = (Socket)iar.AsyncState;
g_server_conn = winsock.EndAccept(iar);
//Function that exchanges names of each other
NewClient(g_server_conn);
Socket server_conn = g_server_conn;
chat_msg = new byte[1024];
server_conn.BeginReceive(chat_msg, 0, chat_msg.Length, SocketFlags.None, new AsyncCallback(Recieve), server_conn);
}
private void Recieve(IAsyncResult iar)
{
Socket server_conn = (Socket)iar.AsyncState;
server_conn.EndReceive(iar);
//If clients shutdown connection,Server recieves /exit command
if (Encoding.ASCII.GetString(chat_msg, 0, chat_msg.Length) == "/exit")
{
g_server_conn.Shutdown(SocketShutdown.Both);
g_server_conn.Close();
return;
}
SetLabel(client_name, chatListBox);
SetLabel(Encoding.ASCII.GetString(chat_msg), chatListBox);
chat_msg = new byte[1024];
server_conn.BeginReceive(chat_msg, 0, chat_msg.Length, SocketFlags.None, new AsyncCallback(Recieve), server_conn);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用
BeginXXX
方法返回的IAsyncResult
。 IIRC,您可以处理WaitHandle
。You should use the
IAsyncResult
returned by theBeginXXX
methods. IIRC, you can dispose of theWaitHandle
.客户端是否真的需要知道服务器为什么关闭连接(您似乎发送了一条没有任何原因信息的断开消息)?看来您只是向客户端发送一条额外的消息来断开它们的连接。
您可以断开所有客户端的连接,它们将收到一条关闭套接字消息,这将在客户端显示连接正在关闭。
附带说明一下,多次转换接收到的字符串是没有意义的,只需将其从字节数组转换为字符串一次即可完成:)。
就我个人而言,我会这样做的顺序是:
1)设置一个全局布尔值,我们不再接受新客户端(IE如果此时有新客户端尝试连接,我不会接受它)
2)检查客户端列表并断开它们
3)关闭列表器。
Does the clients realy need to know why the server closed the connection(You seem to send a disconnect message with no reason information anyways,)? It seems your just sending a extra message to the clients to disconnect them.
You can just disconnect all the clients, and they will recive a close socket message, this will on the client side show the connection being closed.
On a side note, there is no point in converting the recived string so many times, convert it once from byte array to string and your done :).
Personaly the order i would do it is:
1)Set a global bool, that we are no longer accepting new clients ( IE if a new client tries to connect in this time, i would not accept it)
2)Go over the client list an and disconnect them
3)Shut downt he lister.