C# 发送和接收UDP数据包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12864999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:44:24  来源:igfitidea点击:

Sending and Receiving UDP packets

c#udpmessagebroadcastpacket

提问by PGR

The following code sends a packet on port 15000:

以下代码在端口 15000 上发送数据包:

int port = 15000;
UdpClient udp = new UdpClient();
//udp.EnableBroadcast = true;  //This was suggested in a now deleted answer
IPEndPoint groupEP = new IPEndPoint(IPAddress.Broadcast, port);
string str4 = "I want to receive this!";
byte[] sendBytes4 = Encoding.ASCII.GetBytes(str4);
udp.Send(sendBytes4, sendBytes4.Length, groupEP);
udp.Close();

However, it's kind of useless if I can't then receive it on another computer. All I need is to send a command to another computer on the LAN, and for it to receive it and do something.

但是,如果我不能在另一台计算机上接收它,那它是没有用的。我所需要的只是向 LAN 上的另一台计算机发送命令,然后让它接收命令并执行某些操作。

Without using a Pcap library, is there any way I can accomplish this? The computer my program is communicating with is Windows XP 32-bit, and the sending computer is Windows 7 64-bit, if it makes a difference. I've looked into various net sendcommands, but I can't figure them out.

不使用 Pcap 库,有什么办法可以做到这一点吗?我的程序与之通信的计算机是 Windows XP 32 位,发送计算机是 Windows 7 64 位(如果有区别的话)。我查看了各种net send命令,但我无法弄清楚。

I also have access to the computer (the XP one)'s local IP, by being able to physically type 'ipconfig' on it.

我还可以访问计算机(XP 计算机)的本地 IP,通过在其上实际键入“ipconfig”。

EDIT:Here's the Receive function I'm using, copied from somewhere:

编辑:这是我正在使用的接收函数,从某处复制:

public void ReceiveBroadcast(int port)
{
    Debug.WriteLine("Trying to receive...");
    UdpClient client = null;
    try
    {
        client = new UdpClient(port);
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }

    IPEndPoint server = new IPEndPoint(IPAddress.Broadcast, port);

    byte[] packet = client.Receive(ref server);
    Debug.WriteLine(Encoding.ASCII.GetString(packet));
}

I'm calling ReceiveBroadcast(15000)but there's no output at all.

我正在打电话,ReceiveBroadcast(15000)但根本没有输出。

采纳答案by Turbot

Here is the simpleversion of Server and Client to send/receive UDP packets

这是simple发送/接收UDP数据包的服务器和客户端的版本

Server

服务器

IPEndPoint ServerEndPoint= new IPEndPoint(IPAddress.Any,9050);
Socket WinSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
WinSocket.Bind(ServerEndPoint);

Console.Write("Waiting for client");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0)
EndPoint Remote = (EndPoint)(sender);
int recv = WinSocket.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message received from {0}:", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

Client

客户

IPEndPoint RemoteEndPoint= new IPEndPoint(
IPAddress.Parse("ServerHostName"), 9050);
Socket server = new Socket(AddressFamily.InterNetwork,
                           SocketType.Dgram, ProtocolType.Udp);
string welcome = "Hello, are you there?";
data = Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, RemoteEndPoint);

回答by real_yggdrasil

There is actually a very good UDP example of a server and listener on MSDN: Simple UDP example

MSDN上实际上有一个非常好的UDP示例服务器和侦听器:Simple UDP example