我做错了什么?我的套接字不会接收发送给它的数据

发布于 2024-10-18 07:13:05 字数 1674 浏览 1 评论 0原文

我有一个单独的 C# 程序,它将数据发送到我在其中指定的任何 IP 地址。数据以我可以再次解码的方式进行编码 (UTF-8),并通过 UDPClient 发送。我已经设置了这个程序来将数据发送到我的机器上的端口,并将其转发到 Android Emulator。在我的应用程序中,我在此端口上设置了 DatagramSocket,但我的接收调用不会向我返回任何数据...

当我调试 C# 程序时,我确信数据已发送send 语句,我看到它实际上发送了数据...我怀疑它是我的套接字设置中的某些内容,但我不知道是什么...

private void updateUDPSocket(String IpAddress, String IpPort){
    Inet4Address ownIpAddress = null;
    int ownIpPort = 0;

    /*
     *  Try to recover the correct Inet4Address & Port.
     *  Catch all possible exceptions as well.
     */
    try{
        ownIpAddress = (Inet4Address) Inet4Address.getByName(IpAddress);
        ownIpPort = Integer.parseInt(IpPort);
    } catch (UnknownHostException uhe){
        uhe.printStackTrace();
    } catch (NumberFormatException nfe){
        nfe.printStackTrace();
    }

    // Check to see if the current socket is already initialised.
    if(mUDPSocket != null){
        // If so, close & nullify it.
        mUDPSocket.close();
        mUDPSocket = null;
    }

    // Create a new socket & catch the possible exception.
    try{
        mUDPSocket = new DatagramSocket(ownIpPort, ownIpAddress);
        mUDPSocket.setSoTimeout(TIME_OUT_IN_MILLIS);
    } catch (SocketException se){
        se.printStackTrace();
    }

    // Log current set address & port.
    Log.d(TAG, "UDPSocket set to address:" +
        mUDPSocket.getLocalAddress() + ":" + mUDPSocket.getLocalPort());
}

我在这里传递的 IP 地址是 0.0.0.0< /code>(如果是模拟器)或从 WifiManager 恢复的 IP 地址(如果是移动设备)。 设置套接字不会失败,但也许我使用了错误的地址?

我从接收方法得到的唯一输出是接收超时的错误消息......

I have a separate C# program which sends data to any IP-Address I specify in it. The data is encoded in a way that I can decode it again (UTF-8) and sent through an UDPClient. I've set up this program to send the data to a port on my machine which I forward to the Android Emulator. In my application I have a DatagramSocket set on this port, but my receive call doesn't return any data to me...

I know for sure that data is sent, when I debug the C# program on the send statement, I see it actually sends data away... I suspect it is something in my Socket settings, but I don't know what...

private void updateUDPSocket(String IpAddress, String IpPort){
    Inet4Address ownIpAddress = null;
    int ownIpPort = 0;

    /*
     *  Try to recover the correct Inet4Address & Port.
     *  Catch all possible exceptions as well.
     */
    try{
        ownIpAddress = (Inet4Address) Inet4Address.getByName(IpAddress);
        ownIpPort = Integer.parseInt(IpPort);
    } catch (UnknownHostException uhe){
        uhe.printStackTrace();
    } catch (NumberFormatException nfe){
        nfe.printStackTrace();
    }

    // Check to see if the current socket is already initialised.
    if(mUDPSocket != null){
        // If so, close & nullify it.
        mUDPSocket.close();
        mUDPSocket = null;
    }

    // Create a new socket & catch the possible exception.
    try{
        mUDPSocket = new DatagramSocket(ownIpPort, ownIpAddress);
        mUDPSocket.setSoTimeout(TIME_OUT_IN_MILLIS);
    } catch (SocketException se){
        se.printStackTrace();
    }

    // Log current set address & port.
    Log.d(TAG, "UDPSocket set to address:" +
        mUDPSocket.getLocalAddress() + ":" + mUDPSocket.getLocalPort());
}

The IP-Address I pass here is either 0.0.0.0 (in case of the emulator) or the IP-Address recovered from the WifiManager (in case of a mobile device).
It doesn't fail in setting the Socket, but per haps I am using the wrong address?

The only output I get from the receive method is the error message of the receive getting timed out...

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

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

发布评论

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

评论(1

等风来 2024-10-25 07:13:05

我现在知道没有什么问题了,我现在已经在真实设备上测试了这个,它会在Socket上接收数据......

I know now that there is nothing wrong, I've tested this on a real device now, and it will receive data on the Socket...

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