open1081 TCP UDP ECHO例程测试
时间:10-02
整理:3721RD
点击:
IAR的例程不知道为什么打不开,这里使用的是MDK版本。
首先修改AP_NAME和AP_PASSWORD
启动后,程序自动连接无线网络,一旦连接上即回调socket_connected,不知道这个函数是怎么被回调的,没有看到注册这个函数的地方。
然后开始读取www.baidu.com的数据。
例程太乱,为了单纯测试TCP和UDP的echo功能,我将其他代码都去掉。只保留基本功能。
下面是我修改后的代码。
TCP监听8080端口,UDP监听8090端口
- #include "stdio.h"
- #include "ctype.h"
- #include "stm32f2xx.h"
- #include "platform.h"
- #include "mxchipWNET.h"
- /*
- Enable IEEE powersave mode,
- */
- //#define LowPowerMode
- /*
- Under this configuration, you can allocate every socket's send/recv buffer for application's requirement.
- The more buffer is allocated, faster the speed would be, but the max connection number would be less.
- If there is not enough memory for a connection:
- TCP client: function: connect would return fail
- TCP server: Any connection to the server would reset
- If the buffer is not setted, stack would use the default size: 2048bytes.
- */
- //#define DynamicMemAlloc
- #define AP_NAME "TP-604"
- #define AP_PASSWORD "robot604"
- #define WEB_SERVER "www.baidu.com"
- #define APP_INFO "Open1081: TCP[8080] UDP[8090] ECHO"
- //#define BlockMode
- network_InitTypeDef_st wNetConfig;
- lib_config_t libConfig;
- int wifi_up = 0;
- int webserverTest = 1;
- int cloud_ip_addr = -1;
- int cloud_connected = 0;
- int dns_pending = 0;
- extern u32 MS_TIMER;
- void print_msg(void);
- /* ========================================
- User provide callback functions
- ======================================== */
- void system_version(char *str, int len)
- {
- snprintf(str, len, "%s", APP_INFO);
- }
- void WifiStatusHandler(int event)
- {
- switch (event) {
- case MXCHIP_WIFI_UP:
- wifi_up = 1;
- printf("Station up \r\n");
- break;
- case MXCHIP_WIFI_DOWN:
- printf("Station down \r\n");
- break;
- case MXCHIP_UAP_UP:
- break;
- case MXCHIP_UAP_DOWN:
- break;
- default:
- break;
- }
- }
- void NetCallback(net_para_st *pnet)
- {
- printf("IP address: %s \r\n", pnet->ip);
- printf("NetMask address: %s \r\n", pnet->mask);
- printf("Gateway address: %s \r\n", pnet->gate);
- printf("DNS server address: %s \r\n", pnet->dns);
- printf("MAC address: %s \r\n", pnet->mac);
- }
- //#ifndef BlockMode
- #if 0
- void dns_ip_set(u8 *hostname, u32 ip)
- {
- char ipstr[16];
- cloud_ip_addr = ip;
- dns_pending = 0;
- if(cloud_ip_addr == -1)
- printf("DNS test: %s failed \r\n", WEB_SERVER);
- else {
- inet_ntoa(ipstr, ip);
- printf("DNS test: %s address is %s \r\n", WEB_SERVER, ipstr);
- }
- }
- void socket_connected(int fd)
- {
- printf("Connect to web server success! Reading web pages...\r\n");
- cloud_connected = 1;
- }
- #endif
- int main(void)
- {
- int i, j, fd_listen = -1, fd_udp = -1, fd_client = -1;
- char *buf, ip_address[16];
- int len;
- int con = -1;
- int clientfd[8];
- fd_set readfds, exceptfds;
- struct timeval_t t;
- struct sockaddr_t addr;
- socklen_t addrLen;
- int bufferSize;
- libConfig.tcp_buf_dynamic = mxEnable;
- libConfig.tcp_max_connection_num = 12;
- libConfig.tcp_rx_size = 2048;
- libConfig.tcp_tx_size = 2048;
- libConfig.hw_watchdog = 0;
- libConfig.wifi_channel = WIFI_CHANNEL_1_13;
- lib_config(&libConfig);
- for(i = 0; i < 8; i++)
- clientfd[i] = -1;
- buf = (char*)malloc(3*1024);
- mxchipInit();
- UART_Init();
- printf("\r\n%s\r\nmxchipWNet library version: %s\r\n", APP_INFO, system_lib_version());
- ps_enable();
- memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st));
- wNetConfig.wifi_mode = Station;
- strcpy((char*)wNetConfig.wifi_ssid, AP_NAME);
- strcpy((char*)wNetConfig.wifi_key, AP_PASSWORD);
- wNetConfig.dhcpMode = DHCP_Client;
- StartNetwork(&wNetConfig);
- printf("Connect to %s.....\r\n", wNetConfig.wifi_ssid);
- t.tv_sec = 0;
- t.tv_usec = 100;
- set_tcp_keepalive(3, 60);
- for (;;) {
- mxchipTick();
- /* Establish a TCP server that accept the tcp clients connections */
- if (fd_listen == -1) {
- fd_listen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- bufferSize = 5*1024;
- setsockopt(fd_listen, 0, SO_RDBUFLEN, &bufferSize, 4);
- bufferSize = 5*1024;
- setsockopt(fd_listen, 0, SO_WRBUFLEN, &bufferSize, 4);
- addr.s_port = 8080;
- bind(fd_listen, &addr, sizeof(addr));
- listen(fd_listen, 0);
- printf("TCP server established at port: %d \r\n", addr.s_port);
- }
- /* Establish a UDP port to receive any data sent to this port */
- if (fd_udp == -1) {
- fd_udp = socket(AF_INET, SOCK_DGRM, IPPROTO_UDP);
- addr.s_port = 8090;
- bind(fd_udp, &addr, sizeof(addr));
- printf("Open UDP port %d\r\n", addr.s_port);
- }
- /* Check status on erery sockets */
- FD_ZERO(&readfds);
- FD_SET(fd_listen, &readfds);
- FD_SET(fd_udp, &readfds);
- if(fd_client!=-1)
- FD_SET(fd_client, &readfds);
- for(i = 0; i < 8; i++) {
- if (clientfd[i] != -1)
- FD_SET(clientfd[i], &readfds);
- }
- select(1, &readfds, NULL, &exceptfds, &t);
- /* Check tcp connection requests */
- if(FD_ISSET(fd_listen, &readfds)) {
- j = accept(fd_listen, &addr, &len);
- if (j > 0) {
- inet_ntoa(ip_address, addr.s_ip);
- printf("Client %s:%d connected\r\n", ip_address, addr.s_port);
- for(i = 0; i < 8; i++) {
- if (clientfd[i] == -1) {
- clientfd[i] = j;
- break;
- }
- }
- }
- }
- /* Read data from tcp clients and send data back */
- for(i = 0; i < 8; i++) {
- if (clientfd[i] != -1) {
- if (FD_ISSET(clientfd[i], &readfds)) {
- con = recv(clientfd[i], buf, 1*1024, 0);
- if (con > 0)
- send(clientfd[i], buf, con, 0);
- else {
- close(clientfd[i]);
- clientfd[i] = -1;
- }
- }
- else if (FD_ISSET(clientfd[i], &exceptfds))
- clientfd[i] = -1;
- }
- }
- /* Read data from udp and send data back */
- if (FD_ISSET(fd_udp, &readfds)) {
- con = recvfrom(fd_udp, buf, 3*1024, 0, &addr, &addrLen);
- sendto(fd_udp, buf, con, 0, &addr, sizeof(struct sockaddr_t));
- }
- }
- }
从log中可以看到open1081的IP地址是192.168.1.107
TCP echo测试,发送12,返回12
UDP echo测试,发送df,回复df
通过这个简单的echo测试例程,我们了解了socket编程的基本知识。
这个怎么看不到?
带有外链的帖子一般需要经过后台审核的,平时会很快,周末我们有人值班,可能略慢一些。