TA的每日心情 | 衰 2016-12-7 16:21 |
---|
签到天数: 2 天 连续签到: 1 天 [LV.1]初来乍到
|
一、目标
在之前两篇经验中完成了TCP的客户端和服务端测试。有了TCP自然来玩玩UDP咯
接下来完成UDP的发送以及接受。
二、 流程
UDP的整个流程相对于TCP要简单的多,流程这里也少了好几步:
1、创建UDP SOCKET
2、等待接收UDP
3、建立PACKET
4、发送PACKET
三、 具体代码
获取IP啥的就先不贴了,要看的可以看之前的几篇经验。
#ifdef _UDP_TEST ret = wiced_udp_create_socket(&udp_socket, UDP_PORT, WICED_STA_INTERFACE); if(WICED_SUCCESS == ret) { WPRINT_APP_INFO(("create udp socket success!\n")); stNetworkInfo.stTcpStatus = 2; }#endif#ifdef _UDP_TEST ret = wiced_udp_receive(&udp_socket, &rec_pack, 5000); if(ret == WICED_ERROR || ret == WICED_TIMEOUT) { ; } else { wiced_udp_packet_get_info(rec_pack, &rec_ip, &rec_port); WPRINT_APP_INFO(("REC->%d.%d.%d.%d:%d\n", (rec_ip.ip.v4 & 0xff000000) >> 24, (rec_ip.ip.v4 & 0x00ff0000) >> 16, (rec_ip.ip.v4 & 0x0000ff00) >> 8, (rec_ip.ip.v4 & 0x000000ff), rec_port)); } WPRINT_APP_INFO(("UDP->EEBOARD.ICYLINKER\n")); wiced_packet_create_udp(&udp_socket, UDP_MAX_LEN, &udp_packet, &udp_data, &udp_available_space); wiced_packet_set_data_end(udp_packet, (uint8_t *)udp_data + strlen(UDP_STRING)); sprintf(udp_data, "%s", UDP_STRING); if(wiced_udp_send(&udp_socket, &udp_ip, UDP_PORT, udp_packet) != WICED_SUCCESS) { WPRINT_APP_INFO(("udp packet send failed\n")); /* Delete packet, since the send failed */ stNetworkInfo.stTcpStatus = 3; } wiced_packet_delete(udp_packet);#endif四、测试结果
|
|