TA的每日心情 | 衰 2016-12-7 16:21 |
---|
签到天数: 2 天 连续签到: 1 天 [LV.1]初来乍到
|
一、目标
TCP Server程序跑通,监听端口8888
收到客户端的信息,再原文转发给客户端。
二、 大概流程
上上一篇经验完成了获取IP的功能,上一篇完成了TCP Server。接下来的流程:
1、创建TCP SOCKET
2、绑定本地端口8888
3、连接服务器IP端口
4、连接成功后执行下面两个步骤
5、建立PACKET
6、发送PACKET
三、 具体代码
创建SOCKET和连接服务器
/* Create a TCP server socket */if ( wiced_tcp_create_socket( &tcp_client_socket, WICED_STA_INTERFACE ) != WICED_SUCCESS ){ WPRINT_APP_INFO( ("TCP socket creation failed\r\n") );}ret = wiced_tcp_bind(&tcp_client_socket, TCP_CLINET_BIND_PORT);if(ret == WICED_SUCCESS){ server_ip.ip.v4 = MAKE_IPV4_ADDRESS(172,16,8,199); server_ip.version = WICED_IPV4; do { ret = wiced_tcp_connect(&tcp_client_socket, &server_ip, TCP_SERVER_CONN_PORT, TCP_CONN_TIME_OUT); count_err++; }while(ret != WICED_SUCCESS && count_err < TCP_CONN_ERR_MAX);if(ret != WICED_SUCCESS){ WPRINT_APP_INFO(("connnect err\n"));}else{ WPRINT_APP_INFO(("connnect ok\n")); stNetworkInfo.stTcpStatus = 2;} 周期性发送数据
if(stNetworkInfo.stTcpStatus == 2){ wiced_rtos_delay_milliseconds(5000); WPRINT_APP_INFO(("CLIENT->EEBOARD.ICYLINKER\n")); wiced_packet_create_tcp(&tcp_client_socket, TCP_PACKET_MAX_DATA_LENGTH, &client_packet, &client_data, &client_available_space); wiced_packet_set_data_end(client_packet, (uint8_t *)client_data + strlen(TCP_CLIENT_SEND_STR)); sprintf(client_data, "%s", TCP_CLIENT_SEND_STR); if (wiced_tcp_send_packet(&tcp_client_socket, client_packet) != WICED_SUCCESS) { WPRINT_APP_INFO(("TCP packet send failed\n")); /* Delete packet, since the send failed */ wiced_packet_delete(client_packet); /* Close the connection */ wiced_tcp_disconnect(&tcp_client_socket); stNetworkInfo.stTcpStatus = 3; } else { wiced_packet_delete(client_packet); }}四、测试结果
|
|