查看: 3569|回复: 2

[EVAL-WSN]6LowPan——UDP Client客户端

[复制链接]
  • TA的每日心情
    奋斗
    2023-7-8 16:17
  • 签到天数: 971 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2016-1-31 10:04:08 | 显示全部楼层 |阅读模式
    分享到:

    上期说了[EVAL-WSN]6LowPan——UDP Serverhttps://www.cirmall.com/bbs/foru ... 1&fromuid=23447

    这期说 UDP  Client

    UDP 客户端程序
    struct uip_udp_conn *
    udp_new(const uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
    传入的参数分别是远程UDP服务器地址,服务器端口和应用程序定义的指针(一般为NULL),
    返回的是建立的连接,如:
    client_conn = udp_new(&ipaddr, UIP_HTONS(3000), NULL);
    /* new connection with remote host */
      client_conn = udp_new(&ipaddr, UIP_HTONS(3000), NULL);/*远程端口*/
      udp_bind(client_conn, UIP_HTONS(3001)); /*本节点端口*/

    UDP Client 区别与 UDP Server
    UDP Server 的实现http://blog.sina.com.cn/s/blog_7e7fa4c80102w9da.html
    1.jpg
    Wireshark 抓取的数据
    2.jpg

    3.jpg

    源代码

    1. #include "contiki.h"
    2. #include "contiki-lib.h"
    3. #include "contiki-net.h"
    4. #include "net/ip/resolv.h"

    5. #include <string.h>
    6. #include <stdbool.h>

    7. #define DEBUG DEBUG_PRINT
    8. #include "net/ip/uip-debug.h"

    9. #define SEND_INTERVAL                15 * CLOCK_SECOND
    10. #define MAX_PAYLOAD_LEN                40

    11. static struct uip_udp_conn *client_conn;
    12. /*---------------------------------------------------------------------------*/
    13. PROCESS(udp_client_process, "UDP client process");
    14. AUTOSTART_PROCESSES(&resolv_process,&udp_client_process);
    15. /*---------------------------------------------------------------------------*/
    16. static void
    17. tcpip_handler(void)
    18. {
    19.   char *str;

    20.   if(uip_newdata()) {
    21.     str = uip_appdata;
    22.     str[uip_datalen()] = '\0';
    23.     printf("Response from the server: '%s'\n", str);
    24.   }
    25. }
    26. /*---------------------------------------------------------------------------*/
    27. static char buf[MAX_PAYLOAD_LEN];
    28. static void
    29. timeout_handler(void)
    30. {
    31.   static int seq_id;

    32.   printf("Client sending to: ");
    33.   PRINT6ADDR(&client_conn->ripaddr);
    34.   sprintf(buf, "Hello %d from the client", ++seq_id);
    35.   printf(" (msg: %s)\n", buf);
    36. #if SEND_TOO_LARGE_PACKET_TO_TEST_FRAGMENTATION
    37.   uip_udp_packet_send(client_conn, buf, UIP_APPDATA_SIZE);
    38. #else /* SEND_TOO_LARGE_PACKET_TO_TEST_FRAGMENTATION */
    39.   uip_udp_packet_send(client_conn, buf, strlen(buf));
    40. #endif /* SEND_TOO_LARGE_PACKET_TO_TEST_FRAGMENTATION */
    41. }
    42. /*---------------------------------------------------------------------------*/
    43. static void
    44. print_local_addresses(void)
    45. {
    46.   int i;
    47.   uint8_t state;

    48.   PRINTF("Client IPv6 addresses: ");
    49.   for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
    50.     state = uip_ds6_if.addr_list[i].state;
    51.     if(uip_ds6_if.addr_list[i].isused &&
    52.        (state == ADDR_TENTATIVE || state == ADDR_PREFERRED)) {
    53.       PRINT6ADDR(&uip_ds6_if.addr_list[i].ipaddr);
    54.       PRINTF("\n");
    55.     }
    56.   }
    57. }
    58. /*---------------------------------------------------------------------------*/
    59. #if UIP_CONF_ROUTER
    60. static void
    61. set_global_address(void)
    62. {
    63.   uip_ipaddr_t ipaddr;

    64.   uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
    65.   uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
    66.   uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
    67. }
    68. #endif /* UIP_CONF_ROUTER */
    69. /*---------------------------------------------------------------------------*/
    70. static resolv_status_t
    71. set_connection_address(uip_ipaddr_t *ipaddr)
    72. {
    73. #define UDP_CONNECTION_ADDR aaaa::1
    74.   
    75. #ifndef UDP_CONNECTION_ADDR
    76. #if RESOLV_CONF_SUPPORTS_MDNS
    77. #define UDP_CONNECTION_ADDR       contiki-udp-server.local
    78. #elif UIP_CONF_ROUTER
    79. #define UDP_CONNECTION_ADDR       aaaa:0:0:0:0212:7404:0004:0404
    80. #else
    81. #define UDP_CONNECTION_ADDR       fe80:0:0:0:6466:6666:6666:6666
    82. #endif
    83. #endif /* !UDP_CONNECTION_ADDR */

    84. #define _QUOTEME(x) #x
    85. #define QUOTEME(x) _QUOTEME(x)

    86.   resolv_status_t status = RESOLV_STATUS_ERROR;

    87.   if(uiplib_ipaddrconv(QUOTEME(UDP_CONNECTION_ADDR), ipaddr) == 0) {
    88.     uip_ipaddr_t *resolved_addr = NULL;
    89.     status = resolv_lookup(QUOTEME(UDP_CONNECTION_ADDR),&resolved_addr);
    90.     if(status == RESOLV_STATUS_UNCACHED || status == RESOLV_STATUS_EXPIRED) {
    91.       PRINTF("Attempting to look up %s\n",QUOTEME(UDP_CONNECTION_ADDR));
    92.       resolv_query(QUOTEME(UDP_CONNECTION_ADDR));
    93.       status = RESOLV_STATUS_RESOLVING;
    94.     } else if(status == RESOLV_STATUS_CACHED && resolved_addr != NULL) {
    95.       PRINTF("Lookup of "%s" succeded!\n",QUOTEME(UDP_CONNECTION_ADDR));
    96.     } else if(status == RESOLV_STATUS_RESOLVING) {
    97.       PRINTF("Still looking up "%s"...\n",QUOTEME(UDP_CONNECTION_ADDR));
    98.     } else {
    99.       PRINTF("Lookup of "%s" failed. status = %d\n",QUOTEME(UDP_CONNECTION_ADDR),status);
    100.     }
    101.     if(resolved_addr)
    102.       uip_ipaddr_copy(ipaddr, resolved_addr);
    103.   } else {
    104.     status = RESOLV_STATUS_CACHED;
    105.   }

    106.   return status;
    107. }
    108. /*---------------------------------------------------------------------------*/
    109. PROCESS_THREAD(udp_client_process, ev, data)
    110. {
    111.   static struct etimer et;
    112.   uip_ipaddr_t ipaddr;

    113.   PROCESS_BEGIN();
    114.   PRINTF("UDP client process started\n");

    115. #if UIP_CONF_ROUTER
    116.   set_global_address();
    117. #endif

    118.   print_local_addresses();

    119.   static resolv_status_t status = RESOLV_STATUS_UNCACHED;
    120.   while(status != RESOLV_STATUS_CACHED) {
    121.     status = set_connection_address(&ipaddr);

    122.     if(status == RESOLV_STATUS_RESOLVING) {
    123.       PROCESS_WAIT_EVENT_UNTIL(ev == resolv_event_found);
    124.     } else if(status != RESOLV_STATUS_CACHED) {
    125.       PRINTF("Can't get connection address.\n");
    126.       PROCESS_YIELD();
    127.     }
    128.   }

    129.   /* new connection with remote host */
    130.   client_conn = udp_new(&ipaddr, UIP_HTONS(3000), NULL);
    131.   udp_bind(client_conn, UIP_HTONS(3001));

    132.   PRINTF("Created a connection with the server ");
    133.   PRINT6ADDR(&client_conn->ripaddr);
    134.   PRINTF(" local/remote port %u/%u\n",
    135.         UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport));

    136.   etimer_set(&et, SEND_INTERVAL);
    137.   while(1) {
    138.     PROCESS_YIELD();
    139.     if(etimer_expired(&et)) {
    140.       timeout_handler();
    141.       etimer_restart(&et);
    142.     } else if(ev == tcpip_event) {
    143.       tcpip_handler();
    144.     }
    145.   }

    146.   PROCESS_END();
    147. }
    148. /*---------------------------------------------------------------------------*/
    复制代码
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2015-7-14 09:10
  • 签到天数: 9 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2016-2-1 08:50:08 | 显示全部楼层
    不明觉厉
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2018-11-19 16:39
  • 签到天数: 2 天

    连续签到: 1 天

    [LV.1]初来乍到

    发表于 2016-2-1 08:57:09 | 显示全部楼层
    亲,可以将内容一并发到经验频道,很有机会获得每月之星的呢http://jingyan.eeboard.com/
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-4-20 13:26 , Processed in 0.128248 second(s), 20 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.