以下代码在 15000 端口发送数据包:
The following code sends a packet on port 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();但是,如果我不能在另一台计算机上接收它,那就没用了.我所需要的只是向 LAN 上的另一台计算机发送命令,并让它接收它并执行一些操作.
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.
如果不使用 Pcap 库,我有什么办法可以做到这一点?我的程序与之通信的计算机是 Windows XP 32 位,发送计算机是 Windows 7 64 位,如果有区别的话.我查看了各种 net send 命令,但无法弄清楚.
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 send commands, but I can't figure them out.
我还可以访问计算机(XP 计算机)的本地 IP,通过在其上物理键入ipconfig".
I also have access to the computer (the XP one)'s local IP, by being able to physically type 'ipconfig' on it.
这是我正在使用的接收功能,从某处复制:
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)); }我正在调用 ReceiveBroadcast(15000) 但根本没有输出.
I'm calling ReceiveBroadcast(15000) but there's no output at all.
推荐答案这里是simple版本的Server和Client发送/接收UDP数据包
Here is the simple version of Server and Client to send/receive UDP packets
服务器
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));客户
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);更多推荐
发送和接收 UDP 数据包
发布评论