有人可以使用UDP向我提供发送和接收广播消息的代码或链接吗?
我一直陷入困境,希望你能帮助我解决问题。 感谢
答案 0 :(得分:2)
这是一个C#示例:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
class MainClass {
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem(StartUDPListener);
UdpClient udpClient = new UdpClient();
udpClient.Send(new byte[]{0x00}, 1, new IPEndPoint(IPAddress.Broadcast, 4567));
Console.ReadLine();
}
private static void StartUDPListener(object state) {
UdpClient udpServer = new UdpClient(new IPEndPoint(IPAddress.Broadcast, 4567));
IPEndPoint remoteEndPoint = null;
udpServer.Receive(ref remoteEndPoint);
Console.WriteLine("UDP broadcast received from " + remoteEndPoint + ".");
}
}
答案 1 :(得分:2)
以下是广播发送者和接收者的示例代码。
它应该可以轻松移植到任何可以访问标准Berkly套接字API的语言。
#!/usr/bin/perl -w
# broadcast sender script
use strict;
use diagnostics;
use Socket;
my $sock;
my $receiverPort = 9722;
my $senderPort = 9721;
socket($sock, PF_INET, SOCK_DGRAM, getprotobyname('udp')) || die "socket: $!";
setsockopt($sock, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $!";
setsockopt($sock, SOL_SOCKET, SO_BROADCAST, pack("l", 1)) or die "sockopt: $!";
bind($sock, sockaddr_in($senderPort, inet_aton('192.168.2.103'))) || die "bind: $!";
while (1) {
my $datastring = `date`;
my $bytes = send($sock, $datastring, 0,
sockaddr_in($receiverPort, inet_aton('192.168.2.255')));
if (!defined($bytes)) {
print("$!\n");
} else {
print("sent $bytes bytes\n");
}
sleep(2);
}
#!/usr/bin/perl -w
# broadcast receiver script
use strict;
use diagnostics;
use Socket;
my $sock;
socket($sock, PF_INET, SOCK_DGRAM, getprotobyname('udp')) || die "socket: $!";
setsockopt($sock, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $!";
bind($sock, sockaddr_in(9722, inet_aton('192.168.2.255'))) || die "bind: $!";
# just loop forever listening for packets
while (1) {
my $datastring = '';
my $hispaddr = recv($sock, $datastring, 64, 0); # blocking recv
if (!defined($hispaddr)) {
print("recv failed: $!\n");
next;
}
print "$datastring";
}
答案 2 :(得分:1)
我不打算发布代码,只是几个观察结果:
发送UDP广播就像发送单播数据包一样 - 只有目标地址不同。此可以为INADDR_BROADCAST
(255.255.255.255),但这可能会导致具有多个网络接口的系统出现问题。最好发送到您要发送的接口的特定广播地址。唯一重要的警告是,在您的操作系统允许发送广播之前,您可能需要设置SO_BROADCAST
套接字选项。
接收UDP广播完全就像接收单播数据包一样。不需要特殊代码,但您应该将接收器绑定到INADDR_ANY
。
答案 3 :(得分:1)
我刚刚开始学习这个,但这是我的第一个工作示例,它可能对你有帮助。
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class UDPReceiver
{
static void Main(string[] args)
{
IPEndPoint endPoint;
using (UdpClient client = new UdpClient(9998))
{
bool connected = true;
while (connected)
{
byte[] dataBytes = client.Receive(ref endPoint);
string dataString = Encoding.UTF8.GetString(dataBytes);
if (dataString.ToLower() != "exit")
Console.WriteLine(dataString);
else
connected = false;
}
}
}
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class UDPSender
{
static void Main(string[] args)
{
using (UdpClient client = new UdpClient())
{
bool connected = true;
while (connected)
{
string dataString = Console.ReadLine();
byte[] dataBytes = Encoding.UTF8.GetBytes(dataString);
client.Send(dataBytes, dataBytes.Length, new IPEndPoint(IPAddress.Broadcast, 9998));
if (dataString.ToLower() == "exit")
connected = false;
}
}
}
}