使用 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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地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-