C#UDP服务器 - Keep-Alive数据包 [英] C# UDP Server - Keep-Alive Packets
问题描述
我目前正在使用UDP协议在客户端/服务器架构的C#中创建一个小应用程序。这是一个代码,最终将用于将在Windows窗体应用程序中制作的2D游戏。
I'm currently making a small application in C# with a client/server architecture, that uses the UDP protocol. This is a code that'll be eventually used in a 2D game which will be made in Windows Forms Application.
现在,由于UDP是无连接的,我做到了这一点。用户连接,他通过专门的消息将他的名字发送到服务器,当服务器收到加入消息时,他将客户端添加到字符串列表。
Now, since UDP is connectionless, I made it so when a user connects, he sends his name to the server in a specialized message, and when the server receives the join message, he adds the client to a string list.
现在问题是,我不知道如何断开客户端。
Now, the problem is, I don't know how to make a disconnection of the client.
当然,如果客户端点击Disconnect按钮,我可以这样做,它向服务器发送一条消息以断开他的连接。
Of course, I can make it so if the client clicks a Disconnect button, it sends a message to the server to disconnect him.
但如果他只是点击X退出应用程序,或者他的电量耗尽会怎么样?
But what if he just exits the application by clicking 'X', or his power runs out?
我想过制作Keep-Alive数据包,但我想不出有效的方法。
I thought about making Keep-Alive packets, but I couldn't think of an effective way to do so.
这里是服务器代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace UDP_Server
{
class Program
{
Socket _serverSocket;
List<string> clients;
static void Main(string[] args)
{
Program go = new Program();
go.Server();
}
void Server()
{
byte[] msg = new byte[1024];
_serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_serverSocket.Bind(new IPEndPoint(IPAddress.Any, 12345));
while (true)
{
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
int recv = _serverSocket.ReceiveFrom(msg, ref Remote);
string data = Encoding.ASCII.GetString(msg);
if (data.Contains("Name!"))
{
clients.Add(data.Split('!')[1]);
}
else Console.WriteLine("Message received from {0}:", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(msg));
//_serverSocket.SendTo(msg, Remote);
}
}
}
}
有不需要客户端代码,因为它唯一要做的就是使用SendTo方法将数据报发送到服务器,以及获取数据的接收方法。
There's no need for the client code, since the only thing its doing is using the SendTo method to send a datagram to the server, and a receive method to get data.
所以我怎样才能建立一个保持活力的架构?另外,如果我为矿工以上的人工作,我应该使用多线程吗?
So how can I make a keep-alive architecture? Also, if I make it for more than ore person, should I use multithreading?
推荐答案
我无法回答你的具体问题,但是WCF可以选择吗?
I can't answer your specific question, but would WCF be an option?
我不确定你的应用程序具体是什么,但是WCF在连接ful和连接less场景中消除了很多麻烦。
I'm not sure what your application is for specifically, but WCF takes away a lot of the headaches in connections"ful" and connection"less" scenarios.
WCF客户端可以连接和断开连接,并且服务器能够处理多个用户,即使有或没有多线程,如你所说。希望这会有所帮助。
WCF clients can connect and disconnect and the server is capable of handling multiple users, even with or without "multithreading", as you say. Hope this helps.
这篇关于C#UDP服务器 - Keep-Alive数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!