通过 DatagramSocket 进行广播的问题

发布于 2024-11-14 20:11:43 字数 1770 浏览 4 评论 0原文

我在将数据报包从一部 Android 手机广播到另一部 Android 手机时遇到问题。

我已经将我的应用程序设置为能够使用 2 个不同的数据报套接字。这一切都运行良好,我可以很好地在套接字之间切换。

我使用 2 部手机来测试该应用程序,以及一个能够与我的应用程序通信的基于 PC 的应用程序。当我尝试从 2 部手机中的 1 部广播数据报数据包时,PC 应用程序反应良好,但另一部手机根本没有响应。当我用另一部手机尝试时,也会发生同样的情况。

但这里有一个问题:每当我尝试从基于 PC 的应用程序进行广播时,两部手机都会做出响应。 (???)

两个设备和 PC 应用程序都设置为使用相同的广播地址进行发送。但手机似乎不接受对方的广播。我已经确认他们在广播时确实得到了自己的广播响应,这显然是正确的。

我用来初始化和更新广播套接字的方法如下所述。

private void initBroadcastSocket(Inet4Address address, int port){
    try {
        mBroadcastSocket = new DatagramSocket(port, address);
        mBroadcastSocket.setBroadcast(true);
        mBroadcastSocket.setSoTimeout(SOCKET_TIME_OUT);
    } catch (IOException ioe) {
        Log.e(TAG, "Exception occurred while initializing BroadcastSocket: " + ioe.toString());
    }
    if(mBroadcastSocket != null){
        Log.d(TAG, "BroadcastSocket initially set to " + mBroadcastSocket.getLocalAddress() +
                    ":" + mBroadcastSocket.getLocalPort());
    }
}

public synchronized void updateBroadcastSocket(Inet4Address address, int port){
    // Temporarily suspend the listening Thread.
    ...
    // If the socket is open, close it.
    if(mBroadcastSocket != null){
        mBroadcastSocket.close();
        mBroadcastSocket = null;
    }
    // Create new socket with the passed values.
    try {
        mBroadcastSocket = new DatagramSocket(port, address);
        mBroadcastSocket.setBroadcast(true);
        mBroadcastSocket.setSoTimeout(SOCKET_TIME_OUT);
    } catch (SocketException se) {
        Log.e(TAG, "Exception occured while updating BroadcastSocket: " + se.toString());
    }
    // Log new address and port.
    ...
    // Continue the listening Thread.
    ...
}

如果有人发现我的代码有缺陷,请详细说明。

I have an issue with broadcasting Datagram packets from one Android phone to the next.

I have set up my application to be able to use 2 different Datagram Sockets. This all works well, and I am able to switch between the Sockets just fine.

I use 2 phones to test the application, as well as a PC based application that is able to communicate with my application. When I try to broadcast Datagram packets from 1 of the 2 phones, the PC application reacts just fine, but the other phone doesn't respond at all. The same happens when I try it with the other phone.

But here's the catch: whenever I try to broadcast from the PC based application, both phones respond. (???)

Both devices and the PC application are set to use the same broadcast address for sending. Yet it doesn't seem that the phone's accept the broadcast of the other. I have confirmed that they do get a broadcast response from themselves when they are broadcasting, which is correct obviously.

The methods I use to initialise and update the broadcast socket is stated below.

private void initBroadcastSocket(Inet4Address address, int port){
    try {
        mBroadcastSocket = new DatagramSocket(port, address);
        mBroadcastSocket.setBroadcast(true);
        mBroadcastSocket.setSoTimeout(SOCKET_TIME_OUT);
    } catch (IOException ioe) {
        Log.e(TAG, "Exception occurred while initializing BroadcastSocket: " + ioe.toString());
    }
    if(mBroadcastSocket != null){
        Log.d(TAG, "BroadcastSocket initially set to " + mBroadcastSocket.getLocalAddress() +
                    ":" + mBroadcastSocket.getLocalPort());
    }
}

public synchronized void updateBroadcastSocket(Inet4Address address, int port){
    // Temporarily suspend the listening Thread.
    ...
    // If the socket is open, close it.
    if(mBroadcastSocket != null){
        mBroadcastSocket.close();
        mBroadcastSocket = null;
    }
    // Create new socket with the passed values.
    try {
        mBroadcastSocket = new DatagramSocket(port, address);
        mBroadcastSocket.setBroadcast(true);
        mBroadcastSocket.setSoTimeout(SOCKET_TIME_OUT);
    } catch (SocketException se) {
        Log.e(TAG, "Exception occured while updating BroadcastSocket: " + se.toString());
    }
    // Log new address and port.
    ...
    // Continue the listening Thread.
    ...
}

If anyone finds a flaw in my code, please elaborate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

So要识趣 2024-11-21 20:11:43

问题是两个 Android 设备具有相同的 IP 地址(不知何故)。我现在正在尝试解决这个问题。

Issue is both Android devices have the same IP-Address (somehow). I am trying to fix this now.

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