From 5cf2b9c3a2ac321cc994d0810fb17740a2fe999e Mon Sep 17 00:00:00 2001 From: "bernard.xiong@gmail.com" Date: Thu, 14 Jun 2012 15:47:27 +0000 Subject: [PATCH] Fixed buffer size issue. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2166 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- examples/network/tcpclient.c | 8 +++++--- examples/network/udpserver.c | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/examples/network/tcpclient.c b/examples/network/tcpclient.c index cc15b1178b..9927847f74 100644 --- a/examples/network/tcpclient.c +++ b/examples/network/tcpclient.c @@ -2,6 +2,8 @@ #include /* 为了解析主机名,需要包含netdb.h头文件 */ #include /* 使用BSD socket,需要包含sockets.h头文件 */ +#define BUFSZ 1024 + static const char send_data[] = "This is TCP Client from RT-Thread."; /* 发送用到的数据 */ void tcpclient(const char* url, int port) { @@ -14,7 +16,7 @@ void tcpclient(const char* url, int port) host = gethostbyname(url); /* 分配用于存放接收数据的缓冲 */ - recv_data = rt_malloc(1024); + recv_data = rt_malloc(BUFSZ); if (recv_data == RT_NULL) { rt_kprintf("No memory\n"); @@ -51,8 +53,8 @@ void tcpclient(const char* url, int port) while(1) { - /* 从sock连接中接收最大1024字节数据 */ - bytes_received = recv(sock, recv_data, 1024, 0); + /* 从sock连接中接收最大BUFSZ - 1字节数据 */ + bytes_received = recv(sock, recv_data, BUFSZ - 1, 0); if (bytes_received <= 0) { /* 接收失败,关闭这个连接 */ diff --git a/examples/network/udpserver.c b/examples/network/udpserver.c index d480ff3d14..ee84e9d061 100644 --- a/examples/network/udpserver.c +++ b/examples/network/udpserver.c @@ -1,6 +1,8 @@ #include #include /* 使用BSD socket,需要包含sockets.h头文件 */ +#define BUFSZ 1024 + void udpserv(void* paramemter) { int sock; @@ -10,7 +12,7 @@ void udpserv(void* paramemter) struct sockaddr_in server_addr, client_addr; /* 分配接收用的数据缓冲 */ - recv_data = rt_malloc(1024); + recv_data = rt_malloc(BUFSZ); if (recv_data == RT_NULL) { /* 分配内存失败,返回 */ @@ -51,8 +53,8 @@ void udpserv(void* paramemter) while (1) { - /* 从sock中收取最大1024字节数据 */ - bytes_read = recvfrom(sock, recv_data, 1024, 0, + /* 从sock中收取最大BUFSZ - 1字节数据 */ + bytes_read = recvfrom(sock, recv_data, BUFSZ - 1, 0, (struct sockaddr *)&client_addr, &addr_len); /* UDP不同于TCP,它基本不会出现收取的数据失败的情况,除非设置了超时等待 */