Android DatagramSocket 无法通过 WiFi-Direct 接收数据包

如何解决Android DatagramSocket 无法通过 WiFi-Direct 接收数据包

我目前正在构建一个应用程序,目的是通过 WiFi 直接将视频从 Linux 设备流式传输到 Android 设备。我目前使用 raspberry pi 作为 Linux 设备,它也将是 WiFi-Direct 组所有者 (GO),因此它将被视为用于网络目的的服务器。在这种情况下,Android 设备将被视为客户端。

WiFi 直连的工作原理是客户端只知道组所有者的 IP 地址,我需要向 GO 提供 Android 的 IP,以便它可以开始通过已建立的 WiFi 直连发送 UDP 数据包。在这种情况下,Android 也将充当控制器,并且还需要知道 Pi 的 IP 地址才能向其发送命令。

在建立此连接后,Android 设备会向 pi 发送一个数据报数据包作为 ping。这样 pi 就可以记录 Android 的 IP。这就是我的问题所在。Pi 成功接收到这个数据包,并尝试将响应发送回 Android,让它知道它已经记录了 IP 地址。但是,Android 永远不会收到这个返回数据包!我仍然是一个网络菜鸟,所以在涉及这些类型的事情时我真的没有太多的调试技巧。

此代码在建立WiFi直连后在树莓派上执行;它的工作是从 Android 接收第一个 ping 并在 10 秒内发送 10 个数据包作为响应:

编辑:这会在端口 8988 上创建一个套接字。Pi 的静态 IP 地址为 192.168.4.1/24,并作为 DHCP 服务器运行。

#include"p2p_go_socket.h"

#include<cstddef>
#include<iostream>
#include<string>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/un.h>
#include<unistd.h>
#include<netdb.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<thread>
#include<errno.h>
#include<chrono>

#define BACKLOG 2 //Allowed connection count.

#define PORT "8988" //The port we will be listening on.

#define MAX_RECV 1024

/**
 * Listens for a UDP (Datagram) message from our connected p2p client.
 * ***Blocks*** until receiving a message,upon recieving this message,a response is sent to the client.
 * Once the client receives this response,they are safe to assume we have stored their IP address for future
 * UDP based communication.
 */
bool udp_connection_handshake() {
    //Address info structure,used to open our initial socket.
    struct addrinfo *addr_info;

    struct sockaddr_in client_addr;

    //Fill the udp server hints of our
    int sock_fd = -1;
    if (fill_udp_addr_info(&addr_info)) {
        std::cout << "Opening socket..." << std::endl;
        sock_fd = open_socket(addr_info);
    }

    if (sock_fd < 0) {
        std::cout << "Error opening server socket: " << strerror(errno) << std::endl;
        return false;
    }

    ///Bind socket so we can send/receive information.
    if (bind(sock_fd,addr_info->ai_addr,addr_info->ai_addrlen) < 0) {
        close(sock_fd);
        std::cout << "Error binding server socket: " << strerror(errno) << std::endl;
        return false;
    }
    std::cout << "Binded successfully" << std::endl;

    ///Allocate memory for the peer info.
    memset(&client_addr,sizeof client_addr);
    size_t len = sizeof client_addr;

    ///Buffer for receiving messages.
    char buffer[MAX_RECV];
    std::cout << "Listening for messages sent to us ... " << std::endl;

    //Try to receive a message from our client.
    int bytes_received = recvfrom(sock_fd,(char *) buffer,MAX_RECV,MSG_WAITALL,(struct sockaddr*) &client_addr,&len);
    if (bytes_received < 0) {
        close(sock_fd);
        return false;
    }
    buffer[bytes_received] = '\0';
    char client_ip[INET_ADDRSTRLEN];
    int client_port = ntohs((&client_addr)->sin_port);
    //debug code ----
    inet_ntop(AF_INET,&((&client_addr)->sin_addr),client_ip,INET_ADDRSTRLEN);
    std::cout << "Client Message: " << buffer << std::endl;
    std::cout << "Client information: " << std::endl;
    std::cout << "Client Addr = " << client_ip << std::endl;
    std::cout << "Client Port = " << client_port << std::endl;
    std::cout << "Sleeping for 500ms..." << std::endl;

    //We have received response :: 
    //Send out 10 confirmation messages over 10 seconds (ensure that client has open socket). 
    for (int i = 0; i < 10; i++) {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        int sent = sendto(sock_fd,CONFIRM_CONNECTION_MSG.c_str(),CONFIRM_CONNECTION_MSG.length(),MSG_CONFIRM,(const struct sockaddr*) &client_addr,len);
        std::cout << "Sent " << sent << " bytes " << std::endl;
    }
    std::cout << "Sent confirmation messages." << std::endl;
    //Close the socket. We are clear to start sending new data to the client's IP.
    close(sock_fd);
    return true;
}

/**
 * Opens a socket given the parameterized addrinfo hints.
 * Return value is identical to the return value of socket(),the socket file descriptor,or -1 on error.
 * Simply a wrapper for socket()
 */
int open_socket(struct addrinfo* addr_info) {
    return socket(addr_info->ai_family,addr_info->ai_socktype,addr_info->ai_protocol);
}

/**
 * Fills a addrinfo structure with the values indicating that
 * a socket opened with the values in the para structure will be a UDP server.
 * Returns true based on the address information being properly filled via "getaddrinfo()"
 */
bool fill_udp_addr_info(struct addrinfo** addr_info) {
    //Populate hint structure.
    struct addrinfo hints;
    memset(&hints,sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_flags = AI_PASSIVE;
    //Fill address info structure with
    return getaddrinfo(nullptr,PORT,&hints,addr_info) == 0;
}

Android 端遵循相同的概念,但是每次调用 receive() 时都会超时。出于调试目的,RECEIVE_TIMEOUT_MS 设置为 20000(20 秒)。每次调用此代码时,Pi 都会收到第一条消息,但响应永远不会到达 Android 设备。因此,它只会尝试一次这种假握手; Pi 会已经发出它的确认数据包。注意int MAX_HANDSHAKE_ATTEMPTS = 1。代码当前包含在后台运行的线程中:

编辑: Java 代码创建了一个绑定到端口 8988 的 DatagramSocket,当前允许系统选择一个有效的 IP。这个IP往往是192.168.4.163

public static final int PORT = 8988;

private static final int RECEIVE_TIMEOUT_MS = 20000; //20 seconds...
    
private static final int MAX_HANDSHAKE_ATTEMPTS = 1;
    
private static final int MAX_BUFFER_SIZE = 512;
    
private final WifiP2pInfo mGroupInfo;
    
private final WifiDirectService mWifiDirectService;

private final MutableLiveData<Integer> mReceivedResponseFlag = new MutableLiveData<>(FLAG_WAITING);

/* 
**
** unrelated code here 
**
*/

private Thread getExchangeWorker() {
    Runnable r = () -> {
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket(PORT);
            int connectAttempts = 0;
            byte[] message = INIT_MESSAGE.getBytes();
            byte[] buffer = new byte[MAX_BUFFER_SIZE];
            //Wait for RECEIVE_TIMEOUT_MS ms for a response to be sent back.
            socket.setSoTimeout(RECEIVE_TIMEOUT_MS);
            while (connectAttempts < MAX_HANDSHAKE_ATTEMPTS) {
                DatagramPacket sendPacket = new DatagramPacket(message,message.length,mGroupInfo.groupOwnerAddress,PORT);
                socket.send(sendPacket);
                DatagramPacket receivePacket = new DatagramPacket(buffer,buffer.length);
                try {
                    socket.receive(receivePacket);
                    if (receivePacket.getLength() > 0) { //We have received a response,note it!
                        mReceivedResponseFlag.postValue(FLAG_RECEIVED);
                        String receivedResponse = Arrays.toString(buffer);
                        Log.i(TAG,"Received message from GO: " + receivedResponse);
                        break; //We have received a packet. Break!
                    }
                } catch (SocketTimeoutException e) {
                    Log.e(TAG,"Receive timed out on connect attempt = " + (connectAttempts + 1));
                }
                connectAttempts++;
            }
        } catch (IOException e) {
            mReceivedResponseFlag.postValue(FLAG_ERROR);
            Log.e(TAG,"IO error inside of the client sender thread.",e);
        }
        if (socket != null)
            socket.close();
    };
    return new Thread(r);
}

我对这里的问题感到困惑。在发送信息的 10 秒内,Android 应该至少收到一个由 Pi 发送的数据报数据包,​​但它们似乎从未到达 Android!任何帮助将不胜感激。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res