Fixed buffer size issue.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2166 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
parent
7582459691
commit
5cf2b9c3a2
|
@ -2,6 +2,8 @@
|
|||
#include <lwip/netdb.h> /* 为了解析主机名,需要包含netdb.h头文件 */
|
||||
#include <lwip/sockets.h> /* 使用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)
|
||||
{
|
||||
/* 接收失败,关闭这个连接 */
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#include <rtthread.h>
|
||||
#include <lwip/sockets.h> /* 使用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,它基本不会出现收取的数据失败的情况,除非设置了超时等待 */
|
||||
|
||||
|
|
Loading…
Reference in New Issue