Merge pull request #796 from SummerGGift/master
[examples] add some python example for tcp and complete some error handling.
This commit is contained in:
commit
f6b6a5cf22
|
@ -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()
|
|
@ -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))
|
||||
|
|
@ -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;
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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';
|
||||
|
|
Loading…
Reference in New Issue