diff options
| author | yangjie11 <[email protected]> | 2020-11-18 09:56:35 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-11-18 09:56:35 +0800 |
| commit | d4a682e8d97fa131e5a8d795f8c5451d55cdd5b0 (patch) | |
| tree | 4773b40cb012475c5daf74bfd55707e141477669 /examples/network | |
| parent | 2b98f4720c827ae1850df78299f6b273d568c8c7 (diff) | |
| parent | 2ef3c9d861f55253620996a94831a4be31ca6620 (diff) | |
Merge pull request #3 from RT-Thread/master
sync
Diffstat (limited to 'examples/network')
| -rw-r--r-- | examples/network/tcpclient.c | 10 | ||||
| -rw-r--r-- | examples/network/tcpsendpacket.c | 37 | ||||
| -rw-r--r-- | examples/network/tcpserver.c | 12 | ||||
| -rw-r--r-- | examples/network/udpclient.c | 14 | ||||
| -rw-r--r-- | examples/network/udpserver.c | 12 |
5 files changed, 42 insertions, 43 deletions
diff --git a/examples/network/tcpclient.c b/examples/network/tcpclient.c index d4c96f893a..cc67645868 100644 --- a/examples/network/tcpclient.c +++ b/examples/network/tcpclient.c @@ -12,14 +12,12 @@ #define DEBUG_TCP_CLIENT -#define DBG_ENABLE -#define DBG_SECTION_NAME "TCP" +#define DBG_TAG "TCP" #ifdef DEBUG_TCP_CLIENT -#define DBG_LEVEL DBG_LOG +#define DBG_LVL DBG_LOG #else -#define DBG_LEVEL DBG_INFO /* DBG_ERROR */ +#define DBG_LVL DBG_INFO /* DBG_ERROR */ #endif -#define DBG_COLOR #include <rtdbg.h> #define BUFSZ 1024 @@ -131,7 +129,7 @@ static void tcpclient(void *arg) ret = send(sock, send_data, rt_strlen(send_data), 0); if (ret < 0) { - /* 接收失败,关闭这个连接 */ + /* 发送失败,关闭这个连接 */ LOG_I("send error, close the socket."); goto __exit; } diff --git a/examples/network/tcpsendpacket.c b/examples/network/tcpsendpacket.c index 75134c19a2..47bb562506 100644 --- a/examples/network/tcpsendpacket.c +++ b/examples/network/tcpsendpacket.c @@ -1,7 +1,7 @@ #include <rtthread.h> -#include <netdb.h> /* Ϊ�˽�������������Ҫ����netdb.hͷ�ļ� */ -#include <sys/socket.h> /* ʹ��BSD socket����Ҫ����socket.hͷ�ļ� */ +#include <netdb.h> /* 为了解析主机名,需要包含netdb.h头文件 */ +#include <sys/socket.h> /* 使用BSD socket,需要包含socket.h头文件 */ void tcp_senddata(const char *url, int port, int length) { @@ -10,45 +10,52 @@ void tcp_senddata(const char *url, int port, int length) struct sockaddr_in server_addr; rt_uint8_t *buffer_ptr; - /* ͨ��������ڲ���url���host��ַ��������������������������� */ + /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */ host = gethostbyname(url); - /* ����һ��socket��������SOCKET_STREAM��TCP���� */ + /* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */ if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { - /* ����socketʧ�� */ + /* 创建socket失败 */ rt_kprintf("Socket error\n"); return; } - /* �����ڴ� */ + /* 申请内存 */ buffer_ptr = rt_malloc(length); - /* ���췢������ */ + if(RT_NULL == buffer_ptr) + { + /* 申请内存失败 */ + rt_kprintf("No memory\n"); + return; + } + + /* 构造发送数据 */ for (index = 0; index < length; index ++) buffer_ptr[index] = index & 0xff; timeout = 100; - /* ���÷��ͳ�ʱʱ��100ms */ + /* 设置发送超时时间100ms */ setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); - /* ��ʼ��Ԥ���ӵķ���˵�ַ */ + /* 初始化预连接的服务端地址 */ server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_addr.sin_addr = *((struct in_addr *)host->h_addr); rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero)); - /* ���ӵ������ */ + /* 连接到服务端 */ err = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); rt_kprintf("TCP thread connect error code: %d\n", err); while (1) { - /* �������ݵ�sock���� */ + /* 发送数据到sock连接 */ result = send(sock, buffer_ptr, length, MSG_DONTWAIT); - if (result < 0) //���ݷ��ʹ����� + if (result < 0) //数据发送错误处理 { rt_kprintf("TCP thread send error: %d\n", result); closesocket(sock); - /* �ر����ӣ����´������� */ + /* 关闭连接,重新创建连接 */ rt_thread_delay(10); if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) @@ -59,7 +66,7 @@ void tcp_senddata(const char *url, int port, int length) } else if (result == 0) { - /* ��ӡsend��������ֵΪ0�ľ�����Ϣ */ + /* 打印send函数返回值为0的警告信息 */ rt_kprintf("\n Send warning,send function return 0.\r\n"); } } @@ -67,7 +74,7 @@ void tcp_senddata(const char *url, int port, int length) #ifdef RT_USING_FINSH #include <finsh.h> -/* ���tcpclient������finsh shell�� */ +/* 输出tcpclient函数到finsh shell中 */ FINSH_FUNCTION_EXPORT(tcp_senddata, send a packet through tcp connection); #endif diff --git a/examples/network/tcpserver.c b/examples/network/tcpserver.c index 264b6f7c83..8c7382dcbb 100644 --- a/examples/network/tcpserver.c +++ b/examples/network/tcpserver.c @@ -12,14 +12,12 @@ #define DEBUG_TCP_SERVER -#define DBG_ENABLE -#define DBG_SECTION_NAME "TCP" +#define DBG_TAG "TCP" #ifdef DEBUG_TCP_SERVER -#define DBG_LEVEL DBG_LOG +#define DBG_LVL DBG_LOG #else -#define DBG_LEVEL DBG_INFO /* DBG_ERROR */ +#define DBG_LVL DBG_INFO /* DBG_ERROR */ #endif -#define DBG_COLOR #include <rtdbg.h> #define BUFSZ (1024) @@ -231,8 +229,8 @@ static void tcpserver_test(int argc, char** argv) { if (started) { - LOG_I("The tcpclient has started!"); - LOG_I("Please stop tcpclient firstly, by: tcpclient --stop"); + LOG_I("The tcpserver has started!"); + LOG_I("Please stop tcpserver firstly, by: tcpserver --stop"); return; } diff --git a/examples/network/udpclient.c b/examples/network/udpclient.c index 40542b9fa1..f8c42f3a12 100644 --- a/examples/network/udpclient.c +++ b/examples/network/udpclient.c @@ -5,14 +5,12 @@ #define DEBUG_UDP_CLIENT -#define DBG_ENABLE -#define DBG_SECTION_NAME "UDP" +#define DBG_TAG "UDP" #ifdef DEBUG_UDP_CLIENT -#define DBG_LEVEL DBG_LOG +#define DBG_LVL DBG_LOG #else -#define DBG_LEVEL DBG_INFO /* DBG_ERROR */ +#define DBG_LVL DBG_INFO /* DBG_ERROR */ #endif -#define DBG_COLOR #include <rtdbg.h> static int started = 0; @@ -91,7 +89,7 @@ static void usage(void) rt_kprintf(" -h Specify host address\n"); rt_kprintf(" -p Specify the host port number\n"); rt_kprintf(" --cnt Specify the send data count\n"); - rt_kprintf(" --stop Stop tcpclient program\n"); + rt_kprintf(" --stop Stop udpclient program\n"); rt_kprintf(" --help Print help information\n"); } @@ -119,8 +117,8 @@ static void udpclient_test(int argc, char** argv) { if (started) { - LOG_I("The tcpclient has started!"); - LOG_I("Please stop tcpclient firstly, by: tcpclient --stop"); + LOG_I("The udpclient has started!"); + LOG_I("Please stop udpclient firstly, by: udpclient --stop"); return; } diff --git a/examples/network/udpserver.c b/examples/network/udpserver.c index c40a1972b4..a249475e04 100644 --- a/examples/network/udpserver.c +++ b/examples/network/udpserver.c @@ -12,14 +12,12 @@ #define DEBUG_UDP_SERVER -#define DBG_ENABLE -#define DBG_SECTION_NAME "UDP" +#define DBG_TAG "UDP" #ifdef DEBUG_UDP_SERVER -#define DBG_LEVEL DBG_LOG +#define DBG_LVL DBG_LOG #else -#define DBG_LEVEL DBG_INFO /* DBG_ERROR */ +#define DBG_LVL DBG_INFO /* DBG_ERROR */ #endif -#define DBG_COLOR #include <rtdbg.h> #define BUFSZ 1024 @@ -165,8 +163,8 @@ static void udpserver_test(int argc, char** argv) { if (started) { - LOG_I("The tcpclient has started!"); - LOG_I("Please stop tcpclient firstly, by: tcpclient --stop"); + LOG_I("The udpserver has started!"); + LOG_I("Please stop udpserver firstly, by: udpserver --stop"); return; } |
