从同一个套接字发送和接收数据的简单 UDP 示例 [英] Simple UDP example to send and receive data from same socket

查看:23
本文介绍了从同一个套接字发送和接收数据的简单 UDP 示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我很难从同一个套接字发送和接收数据.无论如何,这是我的客户端代码:

For some reason I am having a hard time sending and receiving data from the same socket. Anyways here is my client code:

var client = new UdpClient();
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000); // endpoint where server is listening (testing localy)
client.Connect(ep); 

// send data
client.Send(new byte[] { 1, 2, 3, 4, 5 }, 5);

// then receive data
var receivedData = client.Receive(ref ep);  // Exception: An existing connection was forcibly closed by the remote host

基本上我想创建一个协议,在该协议中我发送一个 udp 数据包,然后我期望得到响应.就像每个请求都有一个响应的 HTTP 协议一样. 如果服务器位于不同的计算机上,则此代码有效.但是,可能存在服务器和客户端在同一台计算机上的情况.

Basically I want to create a protocol where I send a udp packet and then I expect a response. Just like the HTTP protocol for every request there is a response. This code works if the server is on a different computer. There might be the case where the server and client are on the same computer though.

这里是服务器:

UdpClient udpServer = new UdpClient(UDP_LISTEN_PORT);

while (true)
{
    var groupEP = new IPEndPoint(IPAddress.Any, 11000); // listen on any port
    var data = udpServer.Receive(ref groupEP);
    udpServer.Send(new byte[] { 1 }, 1); // if data is received reply letting the client know that we got his data          
}

<小时>

编辑

我不能使用 tcp 的原因是因为有时客户端在 NAT(路由器)后面,而且 UDP 打洞比 TCP 更简单.


Edit

the reason why I cannot use tcp is because sometimes the client is behind a NAT (router) and it is simpler to do UDP hole punching than TCP.

感谢 markmnl 的回答,这是我的代码:

thanks to markmnl answer here is my code:

服务器:

UdpClient udpServer = new UdpClient(11000);

while (true)
{
    var remoteEP = new IPEndPoint(IPAddress.Any, 11000); 
    var data = udpServer.Receive(ref remoteEP); // listen on port 11000
    Console.Write("receive data from " + remoteEP.ToString());
    udpServer.Send(new byte[] { 1 }, 1, remoteEP); // reply back
}

客户端代码:

var client = new UdpClient();
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000); // endpoint where server is listening
client.Connect(ep);

// send data
client.Send(new byte[] { 1, 2, 3, 4, 5 }, 5);

// then receive data
var receivedData = client.Receive(ref ep);

Console.Write("receive data from " + ep.ToString());

Console.Read();

推荐答案

(我想你知道使用 UDP(用户数据报协议)并不能保证交付、检查重复和拥塞控制,只会回答你的问题).

(I presume you are aware that using UDP(User Datagram Protocol) does not guarantee delivery, checks for duplicates and congestion control and will just answer your question).

在你的服务器中这一行:

In your server this line:

var data = udpServer.Receive(ref groupEP);

groupEP 从你所拥有的地址重新分配到你收到东西的地址.

re-assigns groupEP from what you had to a the address you receive something on.

这一行:

udpServer.Send(new byte[] { 1 }, 1); 

由于您没有指定将数据发送给谁,因此无法正常工作.(它适用于您的客户端,因为您调用了 connect,这意味着 send 将始终发送到您连接的端点,当然我们不希望在服务器上这样做,因为我们可能有很多客户端).我会:

Will not work since you have not specified who to send the data to. (It works on your client because you called connect which means send will always be sent to the end point you connected to, of course we don't want that on the server as we could have many clients). I would:

UdpClient udpServer = new UdpClient(UDP_LISTEN_PORT);

while (true)
{
    var remoteEP = new IPEndPoint(IPAddress.Any, 11000);
    var data = udpServer.Receive(ref remoteEP);
    udpServer.Send(new byte[] { 1 }, 1, remoteEP); // if data is received reply letting the client know that we got his data          
}

此外,如果您在同一台机器上拥有服务器和客户端,则应该将它们放在不同的端口上.

Also if you have server and client on the same machine you should have them on different ports.

这篇关于从同一个套接字发送和接收数据的简单 UDP 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆