使用套接字发送回复广播

发布于 2024-08-09 19:51:12 字数 1529 浏览 7 评论 0原文

我试图发送广播,然后让服务器回复它:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几度春秋 2024-08-16 19:51:12

看起来您的 SendBroadcast() 套接字没有绑定到端口,因此他不会接收任何内容。事实上,您的 ReceiveBroadcast() 套接字正在将回复发送回他自己的端口,因此他将收到自己的回复。

ReceiveBroadcast: binds to port 16789
SendBroadcast:    sends to port 16789
ReceiveBroadcast: receives datagram on port 16789
ReceiveBroadcast: sends reply to 16789
ReceiveBroadcast: **would receive own datagram if SendTo follwed by Receive**

您需要 (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 your ReceiveBroadcast() socket is sending the reply back his own port so he will receive his own reply.

ReceiveBroadcast: binds to port 16789
SendBroadcast:    sends to port 16789
ReceiveBroadcast: receives datagram on port 16789
ReceiveBroadcast: sends reply to 16789
ReceiveBroadcast: **would receive own datagram if SendTo follwed by Receive**

You need to (a) have SendBroadcast() bind to a different port and change ReceiveBroadcast() send to that port (instead of his own endpoint ep), or (b) have both functions use the same socket object so they can both receive datagrams on port 16789.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文