使用 sock_udp 的 Riot 客户端导致分段错误

如何解决使用 sock_udp 的 Riot 客户端导致分段错误

我正在尝试使用 c 和套接字在 RIOT OS 中构建服务器 - 客户端进行通信。他们需要能够交换消息。客户端首先需要配置为能够从硬件读取值,然后将它们发送到服务器。我是新手,所以我需要一些帮助。我的服务器目前正在运行,没有任何问题,但客户端总是导致分段错误,我不知道。我找到了这条线。使用 make debug PORT=tap1。它是:

服务器代码

#include <stdio.h>
#include <inttypes.h>

#include "net/gnrc.h"
#include "net/gnrc/ipv6.h"
#include "net/gnrc/netif.h"
#include "net/gnrc/netif/hdr.h"
#include "net/gnrc/udp.h"
#include "net/gnrc/pktdump.h"
#include "timex.h"
#include "utlist.h"
#include "xtimer.h"

#include "net/sock/udp.h"


uint8_t buf[128];

//SERVER
int main(void)
{
    puts("RIOT network stack example application");

    /* get interfaces and print their addresses */
    gnrc_netif_t *netif = NULL;
    while ((netif = gnrc_netif_iter(netif))) {
        ipv6_addr_t ipv6_addrs[CONFIG_GNRC_NETIF_IPV6_ADDRS_NUMOF];
        int res = gnrc_netapi_get(netif->pid,NETOPT_IPV6_ADDR,ipv6_addrs,sizeof(ipv6_addrs));

        if (res < 0) {
            continue;
        }
        for (unsigned i = 0; i < (unsigned)(res / sizeof(ipv6_addr_t)); i++) {
            char ipv6_addr[IPV6_ADDR_MAX_STR_LEN];

            ipv6_addr_to_str(ipv6_addr,&ipv6_addrs[i],IPV6_ADDR_MAX_STR_LEN);
            printf("My address is %s\n",ipv6_addr);
        }
    }
    sock_udp_ep_t local = SOCK_IPV6_EP_ANY;
    sock_udp_t sock;

    local.port = 12345;

    if (sock_udp_create(&sock,&local,NULL,0) < 0) {
        puts("Error creating UDP sock");
        return 1;
    }

    while (1) {
        sock_udp_ep_t remote;
        ssize_t res;

        if ((res = sock_udp_recv(&sock,buf,sizeof(buf),SOCK_NO_TIMEOUT,&remote)) >= 0) {
            puts("Received a message");
            if (sock_udp_send(&sock,res,&remote) < 0) {
                puts("Error sending reply");
            }
        }
    }

    return 0;
}

客户端代码:


#include "net/netopt.h" 
#include "net/gnrc/nettype.h" 
#include "net/gnrc/pkt.h"

#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include <stdio.h>

#include "net/af.h"
#include "net/protnum.h"
#include "net/ipv6/addr.h"
#include "net/sock/udp.h"
#include "xtimer.h"
#include "net/protnum.h"
#include "net/ipv6/addr.h"
#include "net/sock/udp.h"

#include "saul_reg.h"

#include "shell.h"
#include "board.h"
#include "periph_conf.h"
#include "xtimer.h"  //for udp and tcp and processes
#include "thread.h" //for threads
#include "msg.h" //for threads
#include "net/gnrc/pktbuf.h" //for packets
//#include <pkt.h>
#include <pthread.h>

#include "periph/gpio.h" //file periph.h: Parameter definitions for mapping peripherals directly to SAUL.

#define Q_SZ 1000
//#define THREAD_STACKSIZE_MAIN 
//#include "net/ipv6/addr.h"  //for udp and tcp

#ifdef BOARD_ESP32_WROOM
#if MODULE_MRF24J40
#define MRF24J40_PARAM_CS       GPIO16       /* MRF24J40 CS signal    */
#define MRF24J40_PARAM_RESET    GPIO17       /* MRF24J40 RESET signal */
#define MRF24J40_PARAM_INT      GPIO34       /* MRF24J40 INT signal   */
#define MRF24J40_PARAM_SPI_CLK  SPI_CLK_1MHZ /* SPI clock frequency */
#endif
#if MODULE_ENC28J80
#define ENC28J80_PARAM_CS       GPIO32      /* ENC28J80 CS signal    */
#define ENC28J80_PARAM_RESET    GPIO33      /* ENC28J80 RESET signal */
#define ENC28J80_PARAM_INT      GPIO35      /* ENC28J80 INT signal   */
#endif
#endif

#ifndef ARDUINO_LED
#define ARDUINO_LED     (0)
#endif


#ifdef STDIO_UART_BAUDRATE
#define SERIAL_BAUDRATE STDIO_UART_BAUDRATE
#else
#define SERIAL_BAUDRATE 115200
#endif

//uint8_t buf[7];
static char stack[THREAD_STACKSIZE_MAIN];

// Assign the default LED pin
int ledPin = ARDUINO_LED;

// input buffer for receiving chars on the serial port
int buf[64];

// counter that counts the number of received chars
int count = 0;

static saul_reg_t* saul_dht_temp;
static saul_reg_t* saul_dht_humid;

static kernel_pid_t snd_pid;
gnrc_pktsnip_t *pkt;

sock_udp_ep_t local = SOCK_IPV6_EP_ANY;
sock_udp_t sock;

void *snd_handler(void *arg){ 
    pkt->data = &arg;
       //pkt->data = &phy.val[0]; //crushes
 
    sock_udp_ep_t remote = { .family = AF_INET6 };
    ssize_t res;


    remote.port = 12345;
    ipv6_addr_set_all_nodes_multicast((ipv6_addr_t *)&remote.addr.ipv6,IPV6_ADDR_MCAST_SCP_LINK_LOCAL);
    while(1){
        if (sock_udp_send(&sock,pkt->data,sizeof(pkt->data),&remote) < 0) {
            puts("Error sending message");
            sock_udp_close(&sock);
            break;
        }
         if ((res = sock_udp_recv(&sock,1 * US_PER_SEC,NULL)) < 0) {
            if (res == -ETIMEDOUT) {
                puts("Timed out");
            }
            else {
                puts("Error receiving message");
            }
         }
         else {
            printf("Received message: \"");
            for (int i = 0; i < res; i++) {
                printf("%c",buf[i]);
            }
            printf("\"\n");
         }
        xtimer_sleep(2); //sleep for 1  
     }
     return NULL;
}


int main(void){
    puts("Welcome to RIOT!\n");
    //puts("Type `help` for help,type `saul` to see all SAUL devices\n");
    //char line_buf[SHELL_DEFAULT_BUFSIZE];
    //shell_run(NULL,line_buf,SHELL_DEFAULT_BUFSIZE);
    
    saul_dht_humid = saul_reg_find_type(SAUL_SENSE_HUM);
    saul_dht_temp = saul_reg_find_type(SAUL_SENSE_TEMP);
    phydat_t phy;
    phydat_t phy2;
    


    local.port = 0xabcd;

    if (sock_udp_create(&sock,0) < 0) {
        puts("Error creating UDP sock");
        return 1;
    }
    
    while(1){
        //gpio_toggle(g2); 
        saul_reg_read(saul_dht_temp,&phy);  //read a value( a set of values ) from a device
        saul_reg_read(saul_dht_humid,&phy2);
        printf("temperature: %d %d %d Humid: %d %d %d \n",phy.val[0],phy.unit,phy.scale,phy2.val[0],phy2.unit,phy2.scale);
          
          snd_pid = thread_create(stack,/* stack array pointer */
                            sizeof(stack),/* stack size */
                            THREAD_PRIORITY_MAIN - 1,/* thread priority*/
                            THREAD_CREATE_STACKTEST,/* flag: thread configuration flag,usually*/
                            snd_handler,/* thread handler function */
                             (void *) &phy2.val[0],/* argument of thread_handler function*/
                            "snd_handler");   
        
       //pthread_exit(&snd_pid);
    
    }   
    puts("server ending!\n");
    
    return 0;
}

当然,在同一目录中也存在带有模块的 makefile。

我得到的错误是照片中的这个:

enter image description here

要运行服务器客户端,首先我需要创建一个点击界面和 make 命令。

我曾尝试增加堆栈的大小,但没有帮助。我继续收到同样的错误。知道为什么会导致分段错误会很有帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 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