diff --git a/examples/network/tcp_client.py b/examples/network/tcp_client.py new file mode 100644 index 0000000000..d8ae125fdb --- /dev/null +++ b/examples/network/tcp_client.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- + +import socket + +s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) + +s.connect(('192.168.10.110',6001)) + +print s.recv(1024) + +for data in ['rtt_nano','rtt_thread','rtt_bsp']: + s.send(data) + print s.recv(1024) + +s.send('exit') +s.close() \ No newline at end of file diff --git a/examples/network/tcp_server.py b/examples/network/tcp_server.py new file mode 100644 index 0000000000..537e35e043 --- /dev/null +++ b/examples/network/tcp_server.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +#引入模块 +import socket +import threading +import time +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +# 监听端口: +s.bind(('192.168.10.110', 6001)) + +s.listen(5) +print 'Waiting for connection...' + +def tcp_link(sock,addr): + print 'Accept new connection from %s:%s...' % addr + sock.send('Welcome to RT-Thread!') + while True: + data=sock.recv(1024) + time.sleep(1) + if data=='exit' or not data: + break + print data + sock.send('Hello,%s!'%data) + sock.close() + print 'Connection from %s:%s closed.'%addr + +while True: + + #接受一个新连接 + sock,addr=s.accept() + + #创建新线程来处理TCP连接 + t=threading.Thread(target=tcp_link(sock,addr)) + diff --git a/examples/network/tcpclient.c b/examples/network/tcpclient.c index 7a0d4b5365..529c612b9a 100644 --- a/examples/network/tcpclient.c +++ b/examples/network/tcpclient.c @@ -10,6 +10,7 @@ void tcpclient(const char* url, int port) char *recv_data; struct hostent *host; int sock, bytes_received; + int ret; struct sockaddr_in server_addr; /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */ @@ -56,15 +57,19 @@ void tcpclient(const char* url, int port) { /* 从sock连接中接收最大BUFSZ - 1字节数据 */ bytes_received = recv(sock, recv_data, BUFSZ - 1, 0); - if (bytes_received <= 0) + if (bytes_received < 0) { /* 接收失败,关闭这个连接 */ lwip_close(sock); - + rt_kprintf("\nreceived error,close the socket.\r\n"); /* 释放接收缓冲 */ rt_free(recv_data); break; - } + }else if (bytes_received == 0) + { + /* 打印recv函数返回值为0的警告信息 */ + rt_kprintf("\nReceived warning,recv function return 0.\r\n"); + } /* 有接收到数据,把末端清零 */ recv_data[bytes_received] = '\0'; @@ -73,7 +78,7 @@ void tcpclient(const char* url, int port) { /* 如果是首字母是q或Q,关闭这个连接 */ lwip_close(sock); - + rt_kprintf("\n got a 'q' or 'Q',close the socket.\r\n"); /* 释放接收缓冲 */ rt_free(recv_data); break; @@ -85,7 +90,18 @@ void tcpclient(const char* url, int port) } /* 发送数据到sock连接 */ - send(sock,send_data,strlen(send_data), 0); + ret = send(sock,send_data,strlen(send_data), 0); + if (ret < 0) + { + /* 接收失败,关闭这个连接 */ + lwip_close(sock); + rt_kprintf("\nsend error,close the socket.\r\n"); + break; + }else if (ret == 0) + { + /* 打印send函数返回值为0的警告信息 */ + rt_kprintf("\n Send warning,send function return 0.\r\n"); + } } return; diff --git a/examples/network/tcpsendpacket.c b/examples/network/tcpsendpacket.c index 50841e0149..ec23a15d23 100644 --- a/examples/network/tcpsendpacket.c +++ b/examples/network/tcpsendpacket.c @@ -19,9 +19,9 @@ void tcp_senddata(const char* url, int port, int length) return; } - /* ڴ */ + /* ڴ */ buffer_ptr = rt_malloc(length); - /* 췢 */ + /* 췢 */ for (index = 0; index < length; index ++) buffer_ptr[index] = index & 0xff; @@ -51,7 +51,11 @@ void tcp_senddata(const char* url, int port, int length) rt_kprintf("TCP Socket error:%d\n",sock); err = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); rt_kprintf("TCP thread connect error code: %d\n", err); - } + }else if (result == 0) + { + /* ӡsendֵΪ0ľϢ */ + rt_kprintf("\n Send warning,send function return 0.\r\n"); + } } } diff --git a/examples/network/tcpserver.c b/examples/network/tcpserver.c index 3f34bc2c29..e68d9e53f0 100644 --- a/examples/network/tcpserver.c +++ b/examples/network/tcpserver.c @@ -9,6 +9,7 @@ void tcpserv(void* parameter) int sock, connected, bytes_received; struct sockaddr_in server_addr, client_addr; rt_bool_t stop = RT_FALSE; /* 停止标志 */ + int ret; recv_data = rt_malloc(1024); /* 分配接收用的数据缓冲 */ if (recv_data == RT_NULL) @@ -72,16 +73,31 @@ void tcpserv(void* parameter) while (1) { /* 发送数据到connected socket */ - send(connected, send_data, strlen(send_data), 0); - + ret = send(connected, send_data, strlen(send_data), 0); + if (ret < 0) + { + /* 接收失败,关闭这个连接 */ + lwip_close(sock); + rt_kprintf("\nsend error,close the socket.\r\n"); + break; + }else if (ret == 0) + { + /* 打印send函数返回值为0的警告信息 */ + rt_kprintf("\n Send warning,send function return 0.\r\n"); + } + /* 从connected socket中接收数据,接收buffer是1024大小,但并不一定能够收到1024大小的数据 */ bytes_received = recv(connected,recv_data, 1024, 0); - if (bytes_received <= 0) + if (bytes_received < 0) { /* 接收失败,关闭这个connected socket */ lwip_close(connected); break; - } + }else if (bytes_received == 0) + { + /* 打印recv函数返回值为0的警告信息 */ + rt_kprintf("\nReceived warning,recv function return 0.\r\n"); + } /* 有接收到数据,把末端清零 */ recv_data[bytes_received] = '\0';