我有侦听客户端的服务器应用程序。让我们的客户端失去互联网连接并失去与服务器的连接。
服务器是否会自动检查客户端何时断开连接?如果不是,我该如何实施这样的事情?
Main.cs http://pastebin.com/fHYpErz7
ServerSocket.cs: http: //pastebin.com/erw4tzdp
客户端.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
namespace jM2
{
class Client
{
private int clientConnectionID;
private Socket clientSocket;
private string clientIP;
private byte[] clientBuffer = new byte[1024];
public Client(int connectionID, Socket connectionSocket)
{
clientConnectionID = connectionID;
clientSocket = connectionSocket;
clientIP = connectionSocket.RemoteEndPoint.ToString();
clientSocket.BeginReceive(clientBuffer, 0, clientBuffer.Length, SocketFlags.None, new AsyncCallback(dataArrival), null);
}
public void Disconnect()
{
clientSocket.Close();
}
private void dataArrival(IAsyncResult iar)
{
int bytesReceived = clientSocket.EndReceive(iar);
clientSocket.BeginReceive(clientBuffer, 0, clientBuffer.Length, SocketFlags.None, new AsyncCallback(dataArrival), null);
}
}
}