UDP 通信服务端如何监听客户端连接

g19891117 2010-10-26 12:43:20
客户端不正常退出,服务端这边怎么知道客户端连接已经断开
请详细说明下
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);
}
}
...全文
562 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
g19891117 2010-10-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 beargo 的回复:]
UDP不是直联的。。应该做个定时器。定时向UDP服务器发送在线通知包。。
服务器同样道理。。开个定时器。检查超出时间没有收到在线通知包的客户视为已断线。
[/Quote]
请问服务端这边该怎么写,有试过写个定时器 但是一直不成功。
足球中国 2010-10-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 beargo 的回复:]
UDP不是直联的。。应该做个定时器。定时向UDP服务器发送在线通知包。。
服务器同样道理。。开个定时器。检查超出时间没有收到在线通知包的客户视为已断线。
[/Quote]OK
beargo 2010-10-26
  • 打赏
  • 举报
回复
UDP不是直联的。。应该做个定时器。定时向UDP服务器发送在线通知包。。
服务器同样道理。。开个定时器。检查超出时间没有收到在线通知包的客户视为已断线。
g19891117 2010-10-26
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 beargo 的回复:]
就用你的BeginSendTo就行了。。。随便发送个什么内容都行。。。可以不处理。但服务器收到消息就把它的最近接收时间更新到当前接收到的时间。。下次检查时就可以通过时间的长短来判断它是否离线。。。。
[/Quote]

服务器接收不要另外写吗?能加我下QQ指导我下吗? 441657969
beargo 2010-10-26
  • 打赏
  • 举报
回复
就用你的BeginSendTo就行了。。。随便发送个什么内容都行。。。可以不处理。但服务器收到消息就把它的最近接收时间更新到当前接收到的时间。。下次检查时就可以通过时间的长短来判断它是否离线。。。。
g19891117 2010-10-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 beargo 的回复:]
心跳包就是定时器发送的数据包。。定时启动发送。就像心跳一样!!所以叫心跳包。呵呵。。是这么理解吧!!
[/Quote]
具体怎么发送 谁能告诉我下
serverSocket下的那个方法
beargo 2010-10-26
  • 打赏
  • 举报
回复
心跳包就是定时器发送的数据包。。定时启动发送。就像心跳一样!!所以叫心跳包。呵呵。。是这么理解吧!!
g19891117 2010-10-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wangyue4 的回复:]
udp服务端与客户端应该是有心跳包的。每个客户端都应该定时向服务端发包以示存在。
但累计到一定时间服务端没收到客户端的信息就试为其已经下线。将此客户端从在线列表里剔除。
[/Quote]
什么心跳包
wangyue4 2010-10-26
  • 打赏
  • 举报
回复
udp服务端与客户端应该是有心跳包的。每个客户端都应该定时向服务端发包以示存在。
但累计到一定时间服务端没收到客户端的信息就试为其已经下线。将此客户端从在线列表里剔除。

110,572

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧