使用套接字发送回复广播
我试图发送广播,然后让服务器回复它:
public static void SendBroadcast()
{
byte[] buffer = new byte[1024];
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
socket.Connect(new IPEndPoint(IPAddress.Broadcast, 16789));
socket.Send(System.Text.UTF8Encoding.UTF8.GetBytes("Anyone out there?"));
var ep = socket.LocalEndPoint;
socket.Close();
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(ep);
socket.Receive(buffer);
var data = UTF8Encoding.UTF8.GetString(buffer);
Console.WriteLine("Got reply: " + data);
socket.Close();
}
public static void ReceiveBroadcast()
{
byte[] buffer = new byte[1024];
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
var iep = new IPEndPoint(IPAddress.Any, 16789);
socket.Bind(iep);
var ep = iep as EndPoint;
socket.ReceiveFrom(buffer, ref ep);
var data = Encoding.UTF8.GetString(buffer);
Console.WriteLine("Received broadcast: " + data + " from: " + ep.ToString());
buffer = UTF8Encoding.UTF8.GetBytes("Yeah me!");
socket.SendTo(buffer, ep);
socket.Close();
}
广播到达正常,但回复没有。没有抛出异常。谁能帮助我吗?我是否必须打开一个新连接才能回复或其他什么?
编辑:稍微改变了我的代码,现在它可以工作了!感谢您的回复!
I'm trying to send a broadcast and then let the server reply to it:
public static void SendBroadcast()
{
byte[] buffer = new byte[1024];
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
socket.Connect(new IPEndPoint(IPAddress.Broadcast, 16789));
socket.Send(System.Text.UTF8Encoding.UTF8.GetBytes("Anyone out there?"));
var ep = socket.LocalEndPoint;
socket.Close();
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(ep);
socket.Receive(buffer);
var data = UTF8Encoding.UTF8.GetString(buffer);
Console.WriteLine("Got reply: " + data);
socket.Close();
}
public static void ReceiveBroadcast()
{
byte[] buffer = new byte[1024];
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
var iep = new IPEndPoint(IPAddress.Any, 16789);
socket.Bind(iep);
var ep = iep as EndPoint;
socket.ReceiveFrom(buffer, ref ep);
var data = Encoding.UTF8.GetString(buffer);
Console.WriteLine("Received broadcast: " + data + " from: " + ep.ToString());
buffer = UTF8Encoding.UTF8.GetBytes("Yeah me!");
socket.SendTo(buffer, ep);
socket.Close();
}
The broadcast arrives fine, but the reply doesn't. No exception is thrown. Can anyone help me? Do I have to open a new connection for the reply or something?
EDIT: Changed my code a bit, and now it works! Thanks for your reply!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您的
SendBroadcast()
套接字没有绑定到端口,因此他不会接收任何内容。事实上,您的 ReceiveBroadcast() 套接字正在将回复发送回他自己的端口,因此他将收到自己的回复。您需要 (a) 将
SendBroadcast()
绑定到不同端口,并将ReceiveBroadcast()
发送到该端口(而不是他自己的端口)端点ep
),或 (b) 让两个函数使用相同的套接字对象,以便它们都可以在端口 16789 上接收数据报。It doesn't look like your
SendBroadcast()
socket is bound to a port so he's not going to receive anything. In fact yourReceiveBroadcast()
socket is sending the reply back his own port so he will receive his own reply.You need to (a) have
SendBroadcast()
bind to a different port and changeReceiveBroadcast()
send to that port (instead of his own endpointep
), or (b) have both functions use the same socket object so they can both receive datagrams on port 16789.