简单UDP例如发送和接收来自同一个插座数据 [英] Simple UDP example to send and receive data from same socket

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

问题描述

由于某些原因,我有一个很难发送和从同一个插座接收数据。反正这是我的客户端代码:

  VAR的客户=新UdpClient(); 
IPEndPoint EP =新IPEndPoint(IPAddress.Parse(127.0.0.1),11000); //端点,其中服务器监听(localY坐标检测)
client.Connect(EP);

//发送数据
client.Send(新字节[] {1,2,3,4,5},5);

//然后接收数据
VAR receivedData = client.Receive(编号EP); //例外:一个现有的连接被强行远程主机



基本上我想创造一个我发送一个UDP数据包,然后我期待有一个响应的协议。就像每一个请求有响应HTTP协议。:此代码,如果该服务器是不同的计算机上工作。 ,有可能是在服务器和客户端是在同一台计算机上,虽然如此



下面是服务器:

  UdpClient udpServer =新的UdpClient(UDP_LISTEN_PORT); 

,而(真)
{
变种groupEP =新IPEndPoint(IPAddress.Any,11000); //监听任何端口
VAR数据= udpServer.Receive(REF groupEP);
udpServer.Send(新字节[] {1},1); //如果数据被接收的答复让客户知道我们得到了他的数据
}






修改



为什么我不能使用TCP的原因是因为有时客户端是NAT(路由器)后面,更。简单的事情UDP冲压打孔比TCP






解决方案:



由于markmnl答案这里是我的代码:



服务器:

  UdpClient udpServer =新的UdpClient(11000); 

,而(真)
{
变种remoteEP =新IPEndPoint(IPAddress.Any,11000);
VAR数据= udpServer.Receive(REF remoteEP); //侦听端口11000
Console.Write(+ remoteEP.ToString从接收数据());
udpServer.Send(新字节[] {1},1,remoteEP); //回信
}



客户端代码:

  VAR的客户=新UdpClient(); 
IPEndPoint EP =新IPEndPoint(IPAddress.Parse(127.0.0.1),11000); //端点,其中服务器监听
client.Connect(EP);

//发送数据
client.Send(新字节[] {1,2,3,4,5},5);

//然后接收数据
VAR receivedData = client.Receive(编号EP);

Console.Write(+ ep.ToString从接收数据());

Console.Read();


解决方案

(我相信大家都知道,使用UDP(用户。数据报协议)并不能保证交货,重复项和拥塞控制检查,只是回答你的问题)



在您的服务器这一行:

  VAR数据= udpServer.Receive(REF groupEP); 



再受让人 groupEP 从你有什么到该地址收到的东西。



这行:

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



不会起作用,因为你没有指定谁的数据发送到。 (它的工作原理在客户机上,因为你所谓的连接,这意味着发送总是发送到您连接到,我们当然不希望在服务器上的终点,因为我们可以有许多客户端)。我想:

  UdpClient udpServer =新的UdpClient(UDP_LISTEN_PORT); 

,而(真)
{
变种remoteEP =新IPEndPoint(IPAddress.Any,11000);
VAR数据= udpServer.Receive(REF remoteEP);
udpServer.Send(新字节[] {1},1,remoteEP); //如果数据被接收的答复让客户知道我们得到了他的数据
}



此外,如果有服务器和客户端在同一台机器上,你应该让他们在不同的端口。


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

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.

Here is the server:

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          
}


Edit

the reason why I cannot use tcp is because sometimes the client is behind a NAT (router) and it is more simple to do udp punch holing than tcp.


Solution:

thanks to markmnl answer here is my code:

Server:

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
}

Client code:

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();

解决方案

(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);

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

This line:

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

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天全站免登陆