TA的每日心情 | 衰 2016-12-7 16:21 |
---|
签到天数: 2 天 连续签到: 1 天 [LV.1]初来乍到
|
一、目标
TCP Server程序跑通,监听端口8888
收到客户端的信息,再原文转发给客户端。
二、 大概流程
上一篇经验已经完成了获取IP的功能。接下来的流程:
1、创建TCP SOCKET
2、注册回掉函数:连接、收到数据、断开三个事件的回掉
3、监听端口8888
4、连接的回掉函数打印出客户端的IP
5、收到数据后转发给客户端
6、断开后重新监听8888
三、 堆代码
1-》3步代码:
/* Create a TCP server socket */ if ( wiced_tcp_create_socket( &tcp_server_socket, WICED_STA_INTERFACE ) != WICED_SUCCESS ) { WPRINT_APP_INFO( ("TCP socket creation failed\r\n") ); } /* Register callbacks to handle various TCP events */ ret = wiced_tcp_register_callbacks( &tcp_server_socket, client_connected_callback, received_data_callback, client_disconnected_callback, NULL ); if ( ret != WICED_SUCCESS ) { WPRINT_APP_INFO( ("TCP server socket initialization failed\r\n") ); } /* Start TCP server to listen for connections */ if ( wiced_tcp_listen( &tcp_server_socket, TCP_SERVER_LISTEN_PORT ) != WICED_SUCCESS ) { WPRINT_APP_INFO( ("TCP server socket initialization failed\r\n") ); wiced_tcp_delete_socket( &tcp_server_socket ); continue; } WPRINT_APP_INFO( ("TCP server socket initialization success\r\n") ); 4-》6步代码:
static wiced_result_t client_connected_callback( wiced_tcp_socket_t* socket, void* arg ){ wiced_result_t result; wiced_ip_address_t ipaddr; uint16_t port; UNUSED_PARAMETER( arg ); /* Accept connection request */ result = wiced_tcp_accept( &tcp_server_socket ); if( result == WICED_SUCCESS ) { /* Extract IP address and the Port of the connected client */ wiced_tcp_server_peer( socket, &ipaddr, &port ); WPRINT_APP_INFO(("Accepted connection from :: ")); WPRINT_APP_INFO ( ("IP %u.%u.%u.%u : %d\r\n", (unsigned char) ( ( GET_IPV4_ADDRESS(ipaddr) >> 24 ) & 0xff ), (unsigned char) ( ( GET_IPV4_ADDRESS(ipaddr) >> 16 ) & 0xff ), (unsigned char) ( ( GET_IPV4_ADDRESS(ipaddr) >> 8 ) & 0xff ), (unsigned char) ( ( GET_IPV4_ADDRESS(ipaddr) >> 0 ) & 0xff ), port ) ); return WICED_SUCCESS; } return WICED_ERROR;}static wiced_result_t client_disconnected_callback( wiced_tcp_socket_t* socket, void* arg ){ UNUSED_PARAMETER( arg ); WPRINT_APP_INFO(("Client disconnected\r\n\r\n")); /* Start listening on the socket again */ if ( wiced_tcp_listen( socket, TCP_SERVER_LISTEN_PORT ) != WICED_SUCCESS ) { WPRINT_APP_INFO( ("TCP server socket re-initialization failed\r\n") ); wiced_tcp_delete_socket( socket ); return WICED_ERROR; } return WICED_SUCCESS;}static wiced_result_t received_data_callback( wiced_tcp_socket_t* socket, void* arg ){ wiced_result_t result; wiced_packet_t* tx_packet; char* tx_data; wiced_packet_t* rx_packet = NULL; char* request; uint16_t request_length; uint16_t available_data_length; result = wiced_tcp_receive( socket, &rx_packet, WICED_WAIT_FOREVER ); if ( result != WICED_SUCCESS ) { return result; } wiced_packet_get_data( rx_packet, 0, (uint8_t**) &request, &request_length, &available_data_length ); /* Null terminate the received string */ request[request_length] = '\x0'; WPRINT_APP_INFO(("Received data: %s \r\n", request)); /* Send echo back */ if ( wiced_packet_create_tcp( socket, TCP_PACKET_MAX_DATA_LENGTH, &tx_packet, (uint8_t**)&tx_data, &available_data_length ) != WICED_SUCCESS ) { WPRINT_APP_INFO(("TCP packet creation failed\r\n")); return WICED_ERROR; } /* Write the message into tx_data" */ tx_data[request_length] = '\x0'; memcpy( tx_data, request, request_length ); /* Set the end of the data portion */ wiced_packet_set_data_end( tx_packet, (uint8_t*)tx_data + request_length ); /* Send the TCP packet */ if ( wiced_tcp_send_packet( socket, tx_packet ) != WICED_SUCCESS ) { WPRINT_APP_INFO( ("TCP packet send failed\r\n") ); /* Delete packet, since the send failed */ wiced_packet_delete( tx_packet ); } WPRINT_APP_INFO(("Echo data: %s\r\n", tx_data)); /* Release a packet */ wiced_packet_delete( rx_packet ); return WICED_SUCCESS;}四、 测试结果
本机IP:
测试结果:
|
|