提交 cd039a8b 编写于 作者: B bernard.xiong

add more Chinese comments.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@536 bbd45198-f89e-11dd-88c7-29a3b14d5316
上级 7606f6d9
/* /*
* File : readwrite.c * 代码清单:文件读写例子
* This file is part of RT-TestCase in RT-Thread RTOS
* COPYRIGHT (C) 2010, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * 这个例子演示了如何读写一个文件,特别是写的时候应该如何操作。
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2010-02-10 Bernard first version
*/ */
#include <rtthread.h> #include <rtthread.h>
#include <dfs_posix.h> #include <dfs_posix.h> /* 当需要使用文件操作时,需要包含这个头文件 */
#define TEST_FN "/test.dat" #define TEST_FN "/test.dat"
/* 测试用的数据和缓冲 */
static char test_data[120], buffer[120]; static char test_data[120], buffer[120];
/* 文件读写测试 */ /* 文件读写测试 */
void readwrite(const char* filename) void readwrite(const char* filename)
{ {
int fd; int fd;
int index, length; int index, length;
/* 只写 & 创建 打开 */ /* 只写 & 创建 打开 */
fd = open(TEST_FN, O_WRONLY | O_CREAT | O_TRUNC, 0); fd = open(TEST_FN, O_WRONLY | O_CREAT | O_TRUNC, 0);
if (fd < 0) if (fd < 0)
{ {
...@@ -32,13 +26,13 @@ void readwrite(const char* filename) ...@@ -32,13 +26,13 @@ void readwrite(const char* filename)
return; return;
} }
/* 准备写入数据 */ /* 准备写入数据 */
for (index = 0; index < sizeof(test_data); index ++) for (index = 0; index < sizeof(test_data); index ++)
{ {
test_data[index] = index + 27; test_data[index] = index + 27;
} }
/* 写入数据 */ /* 写入数据 */
length = write(fd, test_data, sizeof(test_data)); length = write(fd, test_data, sizeof(test_data));
if (length != sizeof(test_data)) if (length != sizeof(test_data))
{ {
...@@ -47,10 +41,10 @@ void readwrite(const char* filename) ...@@ -47,10 +41,10 @@ void readwrite(const char* filename)
return; return;
} }
/* 关闭文件 */ /* 关闭文件 */
close(fd); close(fd);
/* 只写并在末尾添加打开 */ /* 只写并在末尾添加打开 */
fd = open(TEST_FN, O_WRONLY | O_CREAT | O_APPEND, 0); fd = open(TEST_FN, O_WRONLY | O_CREAT | O_APPEND, 0);
if (fd < 0) if (fd < 0)
{ {
...@@ -65,10 +59,10 @@ void readwrite(const char* filename) ...@@ -65,10 +59,10 @@ void readwrite(const char* filename)
close(fd); close(fd);
return; return;
} }
/* 关闭文件 */ /* 关闭文件 */
close(fd); close(fd);
/* 只读打开进行数据校验 */ /* 只读打开进行数据校验 */
fd = open(TEST_FN, O_RDONLY, 0); fd = open(TEST_FN, O_RDONLY, 0);
if (fd < 0) if (fd < 0)
{ {
...@@ -76,6 +70,7 @@ void readwrite(const char* filename) ...@@ -76,6 +70,7 @@ void readwrite(const char* filename)
return; return;
} }
/* 读取数据(应该为第一次写入的数据) */
length = read(fd, buffer, sizeof(buffer)); length = read(fd, buffer, sizeof(buffer));
if (length != sizeof(buffer)) if (length != sizeof(buffer))
{ {
...@@ -83,6 +78,8 @@ void readwrite(const char* filename) ...@@ -83,6 +78,8 @@ void readwrite(const char* filename)
close(fd); close(fd);
return; return;
} }
/* 检查数据是否正确 */
for (index = 0; index < sizeof(test_data); index ++) for (index = 0; index < sizeof(test_data); index ++)
{ {
if (test_data[index] != buffer[index]) if (test_data[index] != buffer[index])
...@@ -93,6 +90,7 @@ void readwrite(const char* filename) ...@@ -93,6 +90,7 @@ void readwrite(const char* filename)
} }
} }
/* 读取数据(应该为第二次写入的数据) */
length = read(fd, buffer, sizeof(buffer)); length = read(fd, buffer, sizeof(buffer));
if (length != sizeof(buffer)) if (length != sizeof(buffer))
{ {
...@@ -100,6 +98,8 @@ void readwrite(const char* filename) ...@@ -100,6 +98,8 @@ void readwrite(const char* filename)
close(fd); close(fd);
return; return;
} }
/* 检查数据是否正确 */
for (index = 0; index < sizeof(test_data); index ++) for (index = 0; index < sizeof(test_data); index ++)
{ {
if (test_data[index] != buffer[index]) if (test_data[index] != buffer[index])
...@@ -109,13 +109,15 @@ void readwrite(const char* filename) ...@@ -109,13 +109,15 @@ void readwrite(const char* filename)
return; return;
} }
} }
/* 检查数据完毕,关闭文件 */
/* 检查数据完毕,关闭文件 */
close(fd); close(fd);
/* 打印结果 */ /* 打印结果 */
rt_kprintf("read/write done.\n"); rt_kprintf("read/write done.\n");
} }
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
#include <finsh.h> #include <finsh.h>
/* 输出函数到finsh shell命令行中 */
FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test); FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test);
#endif #endif
...@@ -14,9 +14,11 @@ static rt_thread_t tid1 = RT_NULL; ...@@ -14,9 +14,11 @@ static rt_thread_t tid1 = RT_NULL;
static rt_thread_t tid2 = RT_NULL; static rt_thread_t tid2 = RT_NULL;
static rt_thread_t tid3 = RT_NULL; static rt_thread_t tid3 = RT_NULL;
struct rt_event event; /* 事件控制块 */
static struct rt_event event;
void thread1_entry(void *param) /* 线程1入口函数 */
static void thread1_entry(void *param)
{ {
rt_uint32_t e; rt_uint32_t e;
...@@ -31,7 +33,7 @@ void thread1_entry(void *param) ...@@ -31,7 +33,7 @@ void thread1_entry(void *param)
} }
rt_kprintf("thread1: delay 1s to prepare second event\n"); rt_kprintf("thread1: delay 1s to prepare second event\n");
rt_thread_delay(100); rt_thread_delay(10);
/* receive second event */ /* receive second event */
if (rt_event_recv(&event, ((1 << 3) | (1 << 5)), if (rt_event_recv(&event, ((1 << 3) | (1 << 5)),
...@@ -41,34 +43,37 @@ void thread1_entry(void *param) ...@@ -41,34 +43,37 @@ void thread1_entry(void *param)
rt_kprintf("thread1: OR recv event 0x%x\n", e); rt_kprintf("thread1: OR recv event 0x%x\n", e);
} }
rt_thread_delay(50); rt_thread_delay(5);
} }
} }
void thread2_entry(void *param) /* 线程2入口函数 */
static void thread2_entry(void *param)
{ {
while (1) while (1)
{ {
rt_kprintf("thread2: send event1\n"); rt_kprintf("thread2: send event1\n");
rt_event_send(&event, (1 << 3)); rt_event_send(&event, (1 << 3));
rt_thread_delay(100); rt_thread_delay(10);
} }
} }
void thread3_entry(void *param) /* 线程3入口函数 */
static void thread3_entry(void *param)
{ {
while (1) while (1)
{ {
rt_kprintf("thread3: send event2\n"); rt_kprintf("thread3: send event2\n");
rt_event_send(&event, (1 << 5)); rt_event_send(&event, (1 << 5));
rt_thread_delay(200); rt_thread_delay(20);
} }
} }
int event_simple_init() int event_simple_init()
{ {
/* 初始化事件对象 */
rt_event_init(&event, "event", RT_IPC_FLAG_FIFO); rt_event_init(&event, "event", RT_IPC_FLAG_FIFO);
/* 创建线程1 */ /* 创建线程1 */
...@@ -115,6 +120,9 @@ static void _tc_cleanup() ...@@ -115,6 +120,9 @@ static void _tc_cleanup()
if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE) if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid3); rt_thread_delete(tid3);
/* 执行事件对象脱离 */
rt_event_detach(&event);
/* 调度器解锁 */ /* 调度器解锁 */
rt_exit_critical(); rt_exit_critical();
......
#include <rtthread.h> #include <rtthread.h>
#include <lwip/netdb.h> #include <lwip/netdb.h> /* 为了解析主机名,需要包含netdb.h头文件 */
#include <lwip/sockets.h> #include <lwip/sockets.h> /* 使用BSD socket,需要包含sockets.h头文件 */
static const char send_data[] = "This is TCP Client from RT-Thread."; static const char send_data[] = "This is TCP Client from RT-Thread."; /* 发送用到的数据 */
void tcpclient(const char* url, int port) void tcpclient(const char* url, int port)
{ {
char *recv_data; char *recv_data;
struct hostent *host; struct hostent *host;
int sock, bytes_received; int sock, bytes_received;
struct sockaddr_in server_addr; struct sockaddr_in server_addr;
host = gethostbyname(url); /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
host = gethostbyname(url);
recv_data = rt_malloc(1024);
if (recv_data == RT_NULL) /* 分配用于存放接收数据的缓冲 */
{ recv_data = rt_malloc(1024);
rt_kprintf("No memory\n"); if (recv_data == RT_NULL)
return; {
} rt_kprintf("No memory\n");
return;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) }
{
rt_kprintf("Socket error\n"); /* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
/* release recv buffer */ {
rt_free(recv_data); /* 创建socket失败 */
return; rt_kprintf("Socket error\n");
}
/* 释放接收缓冲 */
server_addr.sin_family = AF_INET; rt_free(recv_data);
server_addr.sin_port = htons(port); return;
server_addr.sin_addr = *((struct in_addr *)host->h_addr); }
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
/* 初始化预连接的服务端地址 */
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) server_addr.sin_family = AF_INET;
{ server_addr.sin_port = htons(port);
rt_kprintf("Connect error\n"); server_addr.sin_addr = *((struct in_addr *)host->h_addr);
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
/* release recv buffer */
rt_free(recv_data); /* 连接到服务端 */
return; if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
} {
/* 连接失败 */
while(1) rt_kprintf("Connect error\n");
{
bytes_received = recv(sock, recv_data, 1024, 0); /*释放接收缓冲 */
if (bytes_received < 0) rt_free(recv_data);
{ return;
lwip_close(sock); }
/* release recv buffer */ while(1)
rt_free(recv_data); {
break; /* 从sock连接中接收最大1024字节数据 */
} bytes_received = recv(sock, recv_data, 1024, 0);
if (bytes_received < 0)
recv_data[bytes_received] = '\0'; {
/* 接收失败,关闭这个连接 */
if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0) lwip_close(sock);
{
lwip_close(sock); /* 释放接收缓冲 */
rt_free(recv_data);
/* release recv buffer */ break;
rt_free(recv_data); }
break;
} /* 有接收到数据,把末端清零 */
else recv_data[bytes_received] = '\0';
{
rt_kprintf("\nRecieved data = %s " , recv_data); if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
} {
/* 如果是首字母是q或Q,关闭这个连接 */
send(sock,send_data,strlen(send_data), 0); lwip_close(sock);
}
/* 释放接收缓冲 */
return; rt_free(recv_data);
break;
}
else
{
/* 在控制终端显示收到的数据 */
rt_kprintf("\nRecieved data = %s " , recv_data);
}
/* 发送数据到sock连接 */
send(sock,send_data,strlen(send_data), 0);
}
return;
} }
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
#include <finsh.h> #include <finsh.h>
/* 输出tcpclient函数到finsh shell中 */
FINSH_FUNCTION_EXPORT(tcpclient, startup tcp client); FINSH_FUNCTION_EXPORT(tcpclient, startup tcp client);
#endif #endif
#include <rtthread.h> #include <rtthread.h>
#include <lwip/sockets.h> #include <lwip/sockets.h> /* 使用BSD Socket接口必须包含sockets.h这个头文件 */
static const char send_data[] = "This is TCP Server from RT-Thread."; static const char send_data[] = "This is TCP Server from RT-Thread."; /* 发送用到的数据 */
void tcpserv(void* parameter) void tcpserv(void* parameter)
{ {
char *recv_data; char *recv_data; /* 用于接收的指针,后面会做一次动态分配以请求可用内存 */
rt_uint32_t sin_size; rt_uint32_t sin_size;
int sock, connected, bytes_received; int sock, connected, bytes_received;
struct sockaddr_in server_addr, client_addr; struct sockaddr_in server_addr, client_addr;
rt_bool_t stop = RT_FALSE; rt_bool_t stop = RT_FALSE; /* 停止标志 */
recv_data = rt_malloc(1024); recv_data = rt_malloc(1024); /* 分配接收用的数据缓冲 */
if (recv_data == RT_NULL) if (recv_data == RT_NULL)
{ {
rt_kprintf("No memory\n"); rt_kprintf("No memory\n");
return; return;
} }
/* create a socket */ /* 一个socket在使用前,需要预先创建出来,指定SOCK_STREAM为TCP的socket */
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{ {
rt_kprintf("Socket error\n"); /* 创建失败的错误处理 */
rt_kprintf("Socket error\n");
/* release recv buffer */
rt_free(recv_data); /* 释放已分配的接收缓冲 */
return; rt_free(recv_data);
} return;
}
/* init server address */
server_addr.sin_family = AF_INET; /* 初始化服务端地址 */
server_addr.sin_port = htons(5000); server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_port = htons(5000); /* 服务端工作的端口 */
rt_memset(&(server_addr.sin_zero),8, sizeof(server_addr.sin_zero)); server_addr.sin_addr.s_addr = INADDR_ANY;
rt_memset(&(server_addr.sin_zero),8, sizeof(server_addr.sin_zero));
/* bind socket */
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) /* 绑定socket到服务端地址 */
{ if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
rt_kprintf("Unable to bind\n"); {
/* 绑定失败 */
/* release recv buffer */ rt_kprintf("Unable to bind\n");
rt_free(recv_data);
return; /* 释放已分配的接收缓冲 */
} rt_free(recv_data);
return;
/* listen socket */ }
if (listen(sock, 5) == -1)
{ /* 在socket上进行监听 */
rt_kprintf("Listen error\n"); if (listen(sock, 5) == -1)
{
/* release recv buffer */ rt_kprintf("Listen error\n");
rt_free(recv_data);
return; /* release recv buffer */
} rt_free(recv_data);
return;
rt_kprintf("\nTCPServer Waiting for client on port 5000...\n"); }
while(stop != RT_TRUE)
{ rt_kprintf("\nTCPServer Waiting for client on port 5000...\n");
sin_size = sizeof(struct sockaddr_in); while(stop != RT_TRUE)
{
/* accept a client socket */ sin_size = sizeof(struct sockaddr_in);
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
/* 接受一个客户端连接socket的请求,这个函数调用是阻塞式的 */
rt_kprintf("I got a connection from (%s , %d)\n", connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port)); /* 返回的是连接成功的socket */
while (1) /* 接受返回的client_addr指向了客户端的地址信息 */
{ rt_kprintf("I got a connection from (%s , %d)\n",
send(connected, send_data, strlen(send_data), 0); inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
bytes_received = recv(connected,recv_data, 1024, 0); /* 客户端连接的处理 */
if (bytes_received < 0) while (1)
{ {
/* close socket */ /* 发送数据到connected socket */
lwip_close(connected); send(connected, send_data, strlen(send_data), 0);
break;
} /* 从connected socket中接收数据,接收buffer是1024大小,但并不一定能够收到1024大小的数据 */
bytes_received = recv(connected,recv_data, 1024, 0);
recv_data[bytes_received] = '\0'; if (bytes_received < 0)
if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0) {
{ /* 接收失败,关闭这个connected socket */
lwip_close(connected); lwip_close(connected);
break; break;
} }
else if (strcmp(recv_data, "exit") == 0)
{ /* 有接收到数据,把末端清零 */
lwip_close(connected); recv_data[bytes_received] = '\0';
stop = RT_TRUE; if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
break; {
} /* 如果是首字母是q或Q,关闭这个连接 */
else lwip_close(connected);
{ break;
rt_kprintf("RECIEVED DATA = %s \n" , recv_data); }
} else if (strcmp(recv_data, "exit") == 0)
} {
} /* 如果接收的是exit,则关闭整个服务端 */
lwip_close(connected);
lwip_close(sock); stop = RT_TRUE;
break;
/* release recv buffer */ }
rt_free(recv_data); else
{
return ; /* 在控制终端显示收到的数据 */
rt_kprintf("RECIEVED DATA = %s \n" , recv_data);
}
}
}
/* 退出服务 */
lwip_close(sock);
/* 释放接收缓冲 */
rt_free(recv_data);
return ;
} }
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
#include <finsh.h> #include <finsh.h>
/* 输出tcpserv函数到finsh shell中 */
FINSH_FUNCTION_EXPORT(tcpserv, startup tcp server); FINSH_FUNCTION_EXPORT(tcpserv, startup tcp server);
#endif #endif
#include <rtthread.h> #include <rtthread.h>
#include <lwip/netdb.h> #include <lwip/netdb.h> /* 为了解析主机名,需要包含netdb.h头文件 */
#include <lwip/sockets.h> #include <lwip/sockets.h> /* 使用BSD socket,需要包含sockets.h头文件 */
const char send_data[] = "This is UDP Client from RT-Thread.\n"; const char send_data[] = "This is UDP Client from RT-Thread.\n"; /* 发送用到的数据 */
void udpclient(const char* url, int port, int count) void udpclient(const char* url, int port, int count)
{ {
int sock; int sock;
struct hostent *host; struct hostent *host;
struct sockaddr_in server_addr; struct sockaddr_in server_addr;
host= (struct hostent *) gethostbyname(url); /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
host= (struct hostent *) gethostbyname(url);
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{ /* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */
rt_kprintf("Socket error\n"); if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
return; {
} rt_kprintf("Socket error\n");
return;
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)); server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
while (count) server_addr.sin_addr = *((struct in_addr *)host->h_addr);
{ rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
sendto(sock, send_data, strlen(send_data), 0,
(struct sockaddr *)&server_addr, sizeof(struct sockaddr)); /* 总计发送count次数据 */
while (count)
rt_thread_delay(50); {
/* 发送数据到服务远端 */
count --; sendto(sock, send_data, strlen(send_data), 0,
} (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
/* close socket */ /* 线程休眠一段时间 */
lwip_close(sock); rt_thread_delay(50);
/* 计数值减一 */
count --;
}
/* 关闭这个socket */
lwip_close(sock);
} }
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
#include <finsh.h> #include <finsh.h>
/* 输出udpclient函数到finsh shell中 */
FINSH_FUNCTION_EXPORT(udpclient, startup udp client); FINSH_FUNCTION_EXPORT(udpclient, startup udp client);
#endif #endif
#include <rtthread.h> #include <rtthread.h>
#include <lwip/sockets.h> #include <lwip/sockets.h> /* 使用BSD socket,需要包含sockets.h头文件 */
void udpserv(void* paramemter) void udpserv(void* paramemter)
{ {
int sock; int sock;
int bytes_read; int bytes_read;
char *recv_data; char *recv_data;
rt_uint32_t addr_len; rt_uint32_t addr_len;
struct sockaddr_in server_addr , client_addr; struct sockaddr_in server_addr, client_addr;
recv_data = rt_malloc(1024); /* 分配接收用的数据缓冲 */
if (recv_data == RT_NULL) recv_data = rt_malloc(1024);
{ if (recv_data == RT_NULL)
rt_kprintf("No memory\n"); {
return; /* 分配内存失败,返回 */
} rt_kprintf("No memory\n");
return;
}
/* create socket */ /* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{ {
rt_kprintf("Socket error\n"); rt_kprintf("Socket error\n");
/* release recv buffer */ /* 释放接收用的数据缓冲 */
rt_free(recv_data); rt_free(recv_data);
return; return;
} }
/* init server socket address */ /* 初始化服务端地址 */
server_addr.sin_family = AF_INET; server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000); server_addr.sin_port = htons(5000);
server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_addr.s_addr = INADDR_ANY;
rt_memset(&(server_addr.sin_zero),0, sizeof(server_addr.sin_zero)); rt_memset(&(server_addr.sin_zero),0, sizeof(server_addr.sin_zero));
if (bind(sock,(struct sockaddr *)&server_addr, /* 绑定socket到服务端地址 */
sizeof(struct sockaddr)) == -1) if (bind(sock,(struct sockaddr *)&server_addr,
{ sizeof(struct sockaddr)) == -1)
rt_kprintf("Bind error\n"); {
/* 绑定地址失败 */
rt_kprintf("Bind error\n");
/* release recv buffer */ /* 释放接收用的数据缓冲 */
rt_free(recv_data); rt_free(recv_data);
return; return;
} }
addr_len = sizeof(struct sockaddr); addr_len = sizeof(struct sockaddr);
rt_kprintf("UDPServer Waiting for client on port 5000...\n");
rt_kprintf("UDPServer Waiting for client on port 5000...\n"); while (1)
{
/* 从sock中收取最大1024字节数据 */
bytes_read = recvfrom(sock, recv_data, 1024, 0,
(struct sockaddr *)&client_addr, &addr_len);
/* UDP不同于TCP,它基本不会出现收取的数据失败的情况,除非设置了超时等待 */
while (1) recv_data[bytes_read] = '\0'; /* 把末端清零 */
{
bytes_read = recvfrom(sock, recv_data, 1024, 0,
(struct sockaddr *)&client_addr, &addr_len);
recv_data[bytes_read] = '\0';
rt_kprintf("\n(%s , %d) said : ",inet_ntoa(client_addr.sin_addr),
ntohs(client_addr.sin_port));
rt_kprintf("%s", recv_data);
if (strcmp(recv_data, "exit") == 0) /* 输出接收的数据 */
{ rt_kprintf("\n(%s , %d) said : ",inet_ntoa(client_addr.sin_addr),
lwip_close(sock); ntohs(client_addr.sin_port));
rt_kprintf("%s", recv_data);
/* release recv buffer */ /* 如果接收数据是exit,退出 */
rt_free(recv_data); if (strcmp(recv_data, "exit") == 0)
break; {
} lwip_close(sock);
}
return; /* 释放接收用的数据缓冲 */
rt_free(recv_data);
break;
}
}
return;
} }
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
#include <finsh.h> #include <finsh.h>
/* 输出udpserv函数到finsh shell中 */
FINSH_FUNCTION_EXPORT(udpserv, startup udp server); FINSH_FUNCTION_EXPORT(udpserv, startup udp server);
#endif #endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册