0
나는 contiki cooja에 코드가 있습니다. 당신이 그림 1에서 볼 수있는 것처럼 나는 2 개의 노드와 2 개의 노드를 보낸다. 브로드 캐스트 발신자와 수신자는 다음과 같습니다.simple_udp_connection에서 응답 받기
#include "contiki.h"
#include "lib/random.h"
#include "sys/etimer.h"
#include "net/ip/uip.h"
#include "net/ipv6/uip-ds6.h"
#include "net/ip/uip-debug.h"
#include "simple-udp.h"
#include <stdio.h>
#include <string.h>
#define SEND_INTERVAL (20 * CLOCK_SECOND)
#define SEND_TIME (random_rand() % (SEND_INTERVAL))
static struct simple_udp_connection broadcast_connection;
/*---------------------------------------------------------------------------*/
PROCESS(broadcast_example_process, "UDP broadcast example process");
AUTOSTART_PROCESSES(&broadcast_example_process);
/*---------------------------------------------------------------------------*/
static void
receiver(struct simple_udp_connection *c,
const uip_ipaddr_t *sender_addr,
uint16_t sender_port,
const uip_ipaddr_t *receiver_addr,
uint16_t receiver_port,
const uint8_t *data,
uint16_t datalen)
{
printf("Data received from ");
uip_debug_ipaddr_print(sender_addr);
printf(" on port %d from port %d with length %d: %s \n",
receiver_port, sender_port, datalen, data);
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(broadcast_example_process, ev, data)
{
static struct etimer periodic_timer;
static struct etimer send_timer;
uip_ipaddr_t addr;
char message[20];
sprintf(message," hello ");
PROCESS_BEGIN();
simple_udp_register(&broadcast_connection, 1234,
NULL, 1900,
receiver);
etimer_set(&periodic_timer, SEND_INTERVAL);
while(1) {
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
etimer_reset(&periodic_timer);
etimer_set(&send_timer, SEND_TIME);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer));
printf("Sending broadcast\n");
uip_create_linklocal_allnodes_mcast(&addr);
simple_udp_sendto(&broadcast_connection, message , strlen(message) , &addr);
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
와 수신기 :
#include "contiki.h"
#include "lib/random.h"
#include "sys/ctimer.h"
#include "sys/etimer.h"
#include "net/ip/uip.h"
#include "net/ipv6/uip-ds6.h"
#include "simple-udp.h"
#include "net/ip/uip-debug.h"
#include <stdio.h>
#include <string.h>
#define UDP_PORT 1900
static struct simple_udp_connection broadcast_connection;
/*---------------------------------------------------------------------------*/
PROCESS(broadcast_example_process, "UDP broadcast example process");
AUTOSTART_PROCESSES(&broadcast_example_process);
/*---------------------------------------------------------------------------*/
static void
receiver(struct simple_udp_connection *c,
const uip_ipaddr_t *sender_addr,
uint16_t sender_port,
const uip_ipaddr_t *receiver_addr,
uint16_t receiver_port,
const uint8_t *data,
uint16_t datalen)
{
printf("Data received from ");
uip_debug_ipaddr_print(sender_addr);
printf(" on port %d from port %d with length %d: %s \n",
receiver_port, sender_port, datalen, data);
/* this line should work,right?*/
simple_udp_sendto(&broadcast_connection, "hello",5, sender_addr);
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(broadcast_example_process, ev, data)
{
PROCESS_BEGIN();
simple_udp_register(&broadcast_connection, 1900,
NULL, 1234,
receiver);
while(1) {
PROCESS_WAIT_EVENT();
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
이제 문제는 보낸 사람에게 회신한다. 나는 각 수신기 노드가 그 메시지를 보낸 발신자에게 응답하기를 원한다. 어떻게 해야할지 모르겠습니다. simple_udp_sendto()가 작동하지 않습니다.이 작업에이 기능을 사용할 수 있습니까? 어떻게 사용해야합니까?
감사합니다. 수신자 기능에서 볼 수 있듯이 sender_addr이 있습니다. 이 주소로의 simple_udp_send는 작동하지 않습니다. – rurilifree
이 코드에서 문제를 찾을 수 없습니다. – rurilifree
어떻게이 프로그램을 디버그 할 수 있습니까? sender_addr에 무엇이 있는지보고 싶습니다. 디버그를 위해 – rurilifree