|
我要关闭clientSocket时,关不了,下面是我的部分代码
frmServer.cs- void OnReceive(IAsyncResult ar)
- {
- Socket client = (Socket)ar.AsyncState;
- if (client.Connected)
- {
- try
- {
- int length = client.EndReceive(ar);
- client.BeginReceive(revByte, 0, buffersize, SocketFlags.None, new AsyncCallback(OnReceive), client);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- else
- {
- ThreadMethodTxtServer(client.RemoteEndPoint.ToString() + " has disconnected!");
- }
- }
复制代码 frmClient.cs- private void btnDisConnect_Click(object sender, EventArgs e)
- {
- if (clientSocket.Connected)
- {
- clientSocket.Shutdown(SocketShutdown.Both);
- clientSocket.Close();
- }
- else
- {
- MessageBox.Show(clientSocket.LocalEndPoint.ToString() + " has not connected!");
- }
- }
复制代码 调试了很多次都找不到原因,当关闭clientSocket时,serverSocket会执行OnReceive部分的代码,并且一直执行,而且client.Connected为true。 |
|