0

我正在尝试使用 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, 0, 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, buf, 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, buf, sizeof(buf), 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, &local, NULL, 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。

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

在此处输入图像描述

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

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

4

0 回答 0