[英]How to do async udp networking right?
就像這里的許多其他人一樣,我正在嘗試創建一個網絡庫。 需求基本上是這樣的:
現在在閱讀了一些關於如何做到這一點(最鼓舞人心的是Gaffer on Games )之后,我設置了我的開發環境,思考如何做到這一點並提出了這個基本的工作流程:
當數據進來時,我執行以下操作:
我對這種方法做了一些測試,效果很好。 我每 10 毫秒向它發送一條消息,其中包含 200 字節的數據,持續 10000 個周期,並且 CPU 或內存負載幾乎沒有增加。 只有 NIC 負載在增加。 然而我想出了一些問題| 問題:
該庫有望有一天對其他人有用,並且它的存儲庫是公開可用的。 就這個問題而言,當前的提交可以在這里找到
哇,這真是一個巨大的主題。 如果您在學習之前沒有了解網絡套接字。 我可以給你要點,但這絕對不夠。
客戶:
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();
}
服務器:
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();
}
請在控制台應用程序上嘗試一下,看看它是如何工作的。
基本上,服務器打開端口(本例中為 9050)等待客戶端連接,然后客戶端連接到服務器,然后開始通信。
你提到你必須使用 UDP 套接字,我想你知道 udp 但如果不是你最好檢查一下 TCP 和 UDP 之間的區別,特別是關於驗證數據到達所需目的地的方式(臨時概念等等上)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.