UDP 通信服务端如何监听客户端连接
客户端不正常退出,服务端这边怎么知道客户端连接已经断开
请详细说明下
private void OnReceive(IAsyncResult ar)
{
try
{
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
EndPoint epSender = (EndPoint)ipeSender;
serverSocket.EndReceiveFrom (ar, ref epSender);
//Transform the array of bytes received from the user into an
//intelligent form of object Data
Data msgReceived = new Data(byteData);
//We will send this object in response the users request
Data msgToSend = new Data();
byte [] message;
//If the message is to login, logout, or simple text message
//then when send to others the type of the message remains the same
msgToSend.cmdCommand = msgReceived.cmdCommand;
msgToSend.strName = msgReceived.strName;
switch (msgReceived.cmdCommand)
{
case Command.Login:
//When a user logs in to the server then we add her to our
//list of clients
ClientInfo clientInfo = new ClientInfo();
clientInfo.endpoint = epSender;
clientInfo.strName = msgReceived.strName;
clientList.Add(clientInfo);
//Set the text of the message that we will broadcast to all users
msgToSend.strMessage = "<<<" + msgReceived.strName + " has joined the room>>>";
break;
case Command.Logout:
//When a user wants to log out of the server then we search for her
//in the list of clients and close the corresponding connection
int nIndex = 0;
foreach (ClientInfo client in clientList)
{
if (client.endpoint.ToString() == epSender.ToString())
{
clientList.RemoveAt(nIndex);
break;
}
++nIndex;
}
msgToSend.strMessage = "<<<" + msgReceived.strName + " has left the room>>>";
break;
case Command.Message:
//Set the text of the message that we will broadcast to all users
try
{
string[] msg = msgReceived.strMessage.Split('|');
msgToSend.strMessage = msgReceived.strName + " said to " + msg[0] + ":" + msg[1];
}
catch (Exception E)
{
MessageBox.Show(E.Message + "A");
}
break;
case Command.List:
//Send the names of all users in the chat room to the new user
msgToSend.cmdCommand = Command.List;
msgToSend.strName = null;
msgToSend.strMessage = null;
//Collect the names of the user in the chat room
foreach (ClientInfo client in clientList)
{
//To keep things simple we use asterisk as the marker to separate the user names
msgToSend.strMessage += client.strName + "*";
}
message = msgToSend.ToByte();
//Send the name of the users in the chat room
serverSocket.BeginSendTo (message, 0, message.Length, SocketFlags.None, epSender,
new AsyncCallback(OnSend), epSender);
break;
}
if (msgToSend.cmdCommand == Command.Message)
{
message = msgToSend.ToByte();
////////////////////反馈到客户端
string[] msg = msgReceived.strMessage.Split('|');
int count = clientList.Count;
for (int i = 0; i < count; i++)
{
if (msg[0].CompareTo("public") == 0)
{
if (((ClientInfo)clientList[i]).endpoint != epSender ||
msgToSend.cmdCommand != Command.Login)
{
//Send the message to all users
serverSocket.BeginSendTo(message, 0, message.Length, SocketFlags.None, ((ClientInfo)clientList[i]).endpoint,
new AsyncCallback(OnSend), ((ClientInfo)clientList[i]).endpoint);
}
}
else
{
if (msg[0].CompareTo(((ClientInfo)clientList[i]).strName) == 0 || msgReceived.strName.CompareTo(((ClientInfo)clientList[i]).strName) == 0)
{
//Send the message to all users
serverSocket.BeginSendTo(message, 0, message.Length, SocketFlags.None, ((ClientInfo)clientList[i]).endpoint,
new AsyncCallback(OnSend), ((ClientInfo)clientList[i]).endpoint);
}
}
}
}
else if (msgToSend.cmdCommand != Command.List) //List messages are not broadcasted
{
message = msgToSend.ToByte();
////////////////////反馈到客户端
int count = clientList.Count;
for (int i = 0; i < count; i++)
{
if (((ClientInfo)clientList[i]).endpoint != epSender ||
msgToSend.cmdCommand != Command.Login)
{
//Send the message to all users
serverSocket.BeginSendTo(message, 0, message.Length, SocketFlags.None, ((ClientInfo)clientList[i]).endpoint,
new AsyncCallback(OnSend), ((ClientInfo)clientList[i]).endpoint);
}
}
txtLog.Text += msgToSend.strMessage + "\r\n";
}
//If the user is logging out then we need not listen from her
if (msgReceived.cmdCommand != Command.Logout)
{
//Start listening to the message send by the user
serverSocket.BeginReceiveFrom (byteData, 0, byteData.Length, SocketFlags.None, ref epSender,
new AsyncCallback(OnReceive), epSender);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "SGSServerUDP", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}