[英]How to do async udp networking right?
As many others here on SO I'm trying to create a networking library.就像这里的许多其他人一样,我正在尝试创建一个网络库。 The requirements basically look like this:需求基本上是这样的:
Now after some reading about how to do this (most inspiring was Gaffer on Games ) I set up my development environment, thought about how to do it and came up with this basic workflow:现在在阅读了一些关于如何做到这一点(最鼓舞人心的是Gaffer on Games )之后,我设置了我的开发环境,思考如何做到这一点并提出了这个基本的工作流程:
When data comes in, I do the following:当数据进来时,我执行以下操作:
I did some testing on this approach and it is working quite good.我对这种方法做了一些测试,效果很好。 I fired a message at it every 10 milliseconds with 200 bytes of data for 10000 cycles and there is pretty much no increase in CPU or memory load.我每 10 毫秒向它发送一条消息,其中包含 200 字节的数据,持续 10000 个周期,并且 CPU 或内存负载几乎没有增加。 Only NIC load is increasing.只有 NIC 负载在增加。 However I came up with some problems |然而我想出了一些问题| questions:问题:
The library will hopefully be of use to someone else one day and it's repository is publicly available.该库有望有一天对其他人有用,并且它的存储库是公开可用的。 As of this question, the current commit can be found here就这个问题而言,当前的提交可以在这里找到
Wow, it is really huge subject.哇,这真是一个巨大的主题。 if you didn't learned about network sockets before you'd better learn.如果您在学习之前没有了解网络套接字。 I can give you the gist but it is definitely not enough.我可以给你要点,但这绝对不够。
The Client:客户:
public void Get()
{
string data;
string input;
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
socket.Connect(ipep);
}
catch (SocketException e)
{
Console.WriteLine("Unable to connect to server");
Console.WriteLine(e.ToString());
return;
}
NetworkStream ns = new NetworkStream(socket);
StreamWriter sw = new StreamWriter(ns);
StreamReader sr = new StreamReader(ns);
data = sr.ReadLine();
Console.WriteLine(data);
while (true)
{
input = Console.ReadLine();
if (input == "exite")
break;
sw.WriteLine(input);
sw.Flush();
data = sr.ReadLine();
Console.WriteLine(data);
}
Console.WriteLine("Disconnected from server...");
socket.Close();
ns.Close();
sr.Close();
sr.Close();
}
The Server:服务器:
public void Get()
{
string data;
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ipep);
socket.Listen(10);
Console.WriteLine("Waiting for a client...");
Socket client = socket.Accept();
IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Connected with: {0}, at Port: {1}", newclient.Address, newclient.Port);
NetworkStream ns = new NetworkStream(client);
StreamReader sr = new StreamReader(ns);
StreamWriter sw = new StreamWriter(ns);
string welcome = "Welcome to my test server";
sw.Write(welcome);
sw.Flush();
while (true)
{
try
{
data = sr.ReadLine();
}
catch (IOException)
{
break;
}
Console.WriteLine(data);
sw.WriteLine(data);
sw.Flush();
}
Console.WriteLine("Disconnected from {0}", newclient.Address);
sw.Close();
ns.Close();
sr.Close();
}
Please try it out on Console application, see how it works.请在控制台应用程序上尝试一下,看看它是如何工作的。
Basically, the server opens the port (9050 in this example) waiting for the client connection then the client connects to the server and then starts the communication.基本上,服务器打开端口(本例中为 9050)等待客户端连接,然后客户端连接到服务器,然后开始通信。
You mentioned you have to use UDP sockets, I presume you know about udp but if not you'd better check about the distinction between TCP and UDP especially about the way to verify that the data get to the desired destination (ad hoc concept and so on).你提到你必须使用 UDP 套接字,我想你知道 udp 但如果不是你最好检查一下 TCP 和 UDP 之间的区别,特别是关于验证数据到达所需目的地的方式(临时概念等等上)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.