add more Chinese comments.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@536 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
bernard.xiong 2010-03-24 15:26:48 +00:00
parent 7606f6d952
commit cd039a8b49
6 changed files with 300 additions and 245 deletions

View File

@ -1,30 +1,24 @@
/*
* 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 <dfs_posix.h>
#include <dfs_posix.h> /* 当需要使用文件操作时,需要包含这个头文件 */
#define TEST_FN "/test.dat"
/* 测试用的数据和缓冲 */
static char test_data[120], buffer[120];
/* 文件读写测试 */
/* 文件读写测试 */
void readwrite(const char* filename)
{
int fd;
int index, length;
/* 只写 & 创建 打开 */
/* 只写 & 创建 打开 */
fd = open(TEST_FN, O_WRONLY | O_CREAT | O_TRUNC, 0);
if (fd < 0)
{
@ -32,13 +26,13 @@ void readwrite(const char* filename)
return;
}
/* 准备写入数据 */
/* 准备写入数据 */
for (index = 0; index < sizeof(test_data); index ++)
{
test_data[index] = index + 27;
}
/* 写入数据 */
/* 写入数据 */
length = write(fd, test_data, sizeof(test_data));
if (length != sizeof(test_data))
{
@ -47,10 +41,10 @@ void readwrite(const char* filename)
return;
}
/* 关闭文件 */
/* 关闭文件 */
close(fd);
/* 只写并在末尾添加打开 */
/* 只写并在末尾添加打开 */
fd = open(TEST_FN, O_WRONLY | O_CREAT | O_APPEND, 0);
if (fd < 0)
{
@ -65,10 +59,10 @@ void readwrite(const char* filename)
close(fd);
return;
}
/* 关闭文件 */
/* 关闭文件 */
close(fd);
/* 只读打开进行数据校验 */
/* 只读打开进行数据校验 */
fd = open(TEST_FN, O_RDONLY, 0);
if (fd < 0)
{
@ -76,6 +70,7 @@ void readwrite(const char* filename)
return;
}
/* 读取数据(应该为第一次写入的数据) */
length = read(fd, buffer, sizeof(buffer));
if (length != sizeof(buffer))
{
@ -83,6 +78,8 @@ void readwrite(const char* filename)
close(fd);
return;
}
/* 检查数据是否正确 */
for (index = 0; index < sizeof(test_data); index ++)
{
if (test_data[index] != buffer[index])
@ -93,6 +90,7 @@ void readwrite(const char* filename)
}
}
/* 读取数据(应该为第二次写入的数据) */
length = read(fd, buffer, sizeof(buffer));
if (length != sizeof(buffer))
{
@ -100,6 +98,8 @@ void readwrite(const char* filename)
close(fd);
return;
}
/* 检查数据是否正确 */
for (index = 0; index < sizeof(test_data); index ++)
{
if (test_data[index] != buffer[index])
@ -109,13 +109,15 @@ void readwrite(const char* filename)
return;
}
}
/* 检查数据完毕,关闭文件 */
/* 检查数据完毕,关闭文件 */
close(fd);
/* 打印结果 */
/* 打印结果 */
rt_kprintf("read/write done.\n");
}
#ifdef RT_USING_FINSH
#include <finsh.h>
/* 输出函数到finsh shell命令行中 */
FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test);
#endif

View File

@ -14,9 +14,11 @@ static rt_thread_t tid1 = RT_NULL;
static rt_thread_t tid2 = 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;
@ -31,7 +33,7 @@ void thread1_entry(void *param)
}
rt_kprintf("thread1: delay 1s to prepare second event\n");
rt_thread_delay(100);
rt_thread_delay(10);
/* receive second event */
if (rt_event_recv(&event, ((1 << 3) | (1 << 5)),
@ -41,34 +43,37 @@ void thread1_entry(void *param)
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)
{
rt_kprintf("thread2: send event1\n");
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)
{
rt_kprintf("thread3: send event2\n");
rt_event_send(&event, (1 << 5));
rt_thread_delay(200);
rt_thread_delay(20);
}
}
int event_simple_init()
{
/* 初始化事件对象 */
rt_event_init(&event, "event", RT_IPC_FLAG_FIFO);
/* 创建线程1 */
@ -115,6 +120,9 @@ static void _tc_cleanup()
if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid3);
/* 执行事件对象脱离 */
rt_event_detach(&event);
/* 调度器解锁 */
rt_exit_critical();

View File

@ -1,81 +1,95 @@
#include <rtthread.h>
#include <lwip/netdb.h>
#include <lwip/sockets.h>
#include <lwip/netdb.h> /* 为了解析主机名需要包含netdb.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)
{
char *recv_data;
struct hostent *host;
int sock, bytes_received;
struct sockaddr_in server_addr;
char *recv_data;
struct hostent *host;
int sock, bytes_received;
struct sockaddr_in server_addr;
host = gethostbyname(url);
/* 通过函数入口参数url获得host地址如果是域名会做域名解析 */
host = gethostbyname(url);
recv_data = rt_malloc(1024);
if (recv_data == RT_NULL)
{
rt_kprintf("No memory\n");
return;
}
/* 分配用于存放接收数据的缓冲 */
recv_data = rt_malloc(1024);
if (recv_data == RT_NULL)
{
rt_kprintf("No memory\n");
return;
}
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
rt_kprintf("Socket error\n");
/* 创建一个socket类型是SOCKET_STREAMTCP类型 */
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
/* 创建socket失败 */
rt_kprintf("Socket error\n");
/* release recv buffer */
rt_free(recv_data);
return;
}
/* 释放接收缓冲 */
rt_free(recv_data);
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);
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)
{
rt_kprintf("Connect error\n");
/* 连接到服务端 */
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
/* 连接失败 */
rt_kprintf("Connect error\n");
/* release recv buffer */
rt_free(recv_data);
return;
}
/*释放接收缓冲 */
rt_free(recv_data);
return;
}
while(1)
{
bytes_received = recv(sock, recv_data, 1024, 0);
if (bytes_received < 0)
{
lwip_close(sock);
while(1)
{
/* 从sock连接中接收最大1024字节数据 */
bytes_received = recv(sock, recv_data, 1024, 0);
if (bytes_received < 0)
{
/* 接收失败,关闭这个连接 */
lwip_close(sock);
/* release recv buffer */
rt_free(recv_data);
break;
}
/* 释放接收缓冲 */
rt_free(recv_data);
break;
}
recv_data[bytes_received] = '\0';
/* 有接收到数据,把末端清零 */
recv_data[bytes_received] = '\0';
if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
{
lwip_close(sock);
if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
{
/* 如果是首字母是q或Q关闭这个连接 */
lwip_close(sock);
/* release recv buffer */
rt_free(recv_data);
break;
}
else
{
rt_kprintf("\nRecieved data = %s " , recv_data);
}
/* 释放接收缓冲 */
rt_free(recv_data);
break;
}
else
{
/* 在控制终端显示收到的数据 */
rt_kprintf("\nRecieved data = %s " , recv_data);
}
send(sock,send_data,strlen(send_data), 0);
}
/* 发送数据到sock连接 */
send(sock,send_data,strlen(send_data), 0);
}
return;
return;
}
#ifdef RT_USING_FINSH
#include <finsh.h>
/* 输出tcpclient函数到finsh shell中 */
FINSH_FUNCTION_EXPORT(tcpclient, startup tcp client);
#endif

View File

@ -1,109 +1,122 @@
#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)
{
char *recv_data;
rt_uint32_t sin_size;
int sock, connected, bytes_received;
struct sockaddr_in server_addr, client_addr;
rt_bool_t stop = RT_FALSE;
char *recv_data; /* 用于接收的指针,后面会做一次动态分配以请求可用内存 */
rt_uint32_t sin_size;
int sock, connected, bytes_received;
struct sockaddr_in server_addr, client_addr;
rt_bool_t stop = RT_FALSE; /* 停止标志 */
recv_data = rt_malloc(1024);
if (recv_data == RT_NULL)
{
rt_kprintf("No memory\n");
return;
}
recv_data = rt_malloc(1024); /* 分配接收用的数据缓冲 */
if (recv_data == RT_NULL)
{
rt_kprintf("No memory\n");
return;
}
/* create a socket */
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
rt_kprintf("Socket error\n");
/* 一个socket在使用前需要预先创建出来指定SOCK_STREAM为TCP的socket */
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
/* 创建失败的错误处理 */
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_addr.s_addr = INADDR_ANY;
rt_memset(&(server_addr.sin_zero),8, sizeof(server_addr.sin_zero));
/* 初始化服务端地址 */
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000); /* 服务端工作的端口 */
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)
{
rt_kprintf("Unable to bind\n");
/* 绑定socket到服务端地址 */
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
/* 绑定失败 */
rt_kprintf("Unable to bind\n");
/* release recv buffer */
rt_free(recv_data);
return;
}
/* 释放已分配的接收缓冲 */
rt_free(recv_data);
return;
}
/* listen socket */
if (listen(sock, 5) == -1)
{
rt_kprintf("Listen error\n");
/* 在socket上进行监听 */
if (listen(sock, 5) == -1)
{
rt_kprintf("Listen error\n");
/* release recv buffer */
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)
{
sin_size = sizeof(struct sockaddr_in);
rt_kprintf("\nTCPServer Waiting for client on port 5000...\n");
while(stop != RT_TRUE)
{
sin_size = sizeof(struct sockaddr_in);
/* accept a client socket */
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
/* 接受一个客户端连接socket的请求这个函数调用是阻塞式的 */
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
/* 返回的是连接成功的socket */
rt_kprintf("I got a connection from (%s , %d)\n",
inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
/* 接受返回的client_addr指向了客户端的地址信息 */
rt_kprintf("I got a connection from (%s , %d)\n",
inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
while (1)
{
send(connected, send_data, strlen(send_data), 0);
/* 客户端连接的处理 */
while (1)
{
/* 发送数据到connected socket */
send(connected, send_data, strlen(send_data), 0);
bytes_received = recv(connected,recv_data, 1024, 0);
if (bytes_received < 0)
{
/* close socket */
lwip_close(connected);
break;
}
/* 从connected socket中接收数据接收buffer是1024大小但并不一定能够收到1024大小的数据 */
bytes_received = recv(connected,recv_data, 1024, 0);
if (bytes_received < 0)
{
/* 接收失败关闭这个connected socket */
lwip_close(connected);
break;
}
recv_data[bytes_received] = '\0';
if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
{
lwip_close(connected);
break;
}
else if (strcmp(recv_data, "exit") == 0)
{
lwip_close(connected);
stop = RT_TRUE;
break;
}
else
{
rt_kprintf("RECIEVED DATA = %s \n" , recv_data);
}
}
}
/* 有接收到数据,把末端清零 */
recv_data[bytes_received] = '\0';
if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
{
/* 如果是首字母是q或Q关闭这个连接 */
lwip_close(connected);
break;
}
else if (strcmp(recv_data, "exit") == 0)
{
/* 如果接收的是exit则关闭整个服务端 */
lwip_close(connected);
stop = RT_TRUE;
break;
}
else
{
/* 在控制终端显示收到的数据 */
rt_kprintf("RECIEVED DATA = %s \n" , recv_data);
}
}
}
lwip_close(sock);
/* 退出服务 */
lwip_close(sock);
/* release recv buffer */
rt_free(recv_data);
/* 释放接收缓冲 */
rt_free(recv_data);
return ;
return ;
}
#ifdef RT_USING_FINSH
#include <finsh.h>
/* 输出tcpserv函数到finsh shell中 */
FINSH_FUNCTION_EXPORT(tcpserv, startup tcp server);
#endif

View File

@ -1,42 +1,50 @@
#include <rtthread.h>
#include <lwip/netdb.h>
#include <lwip/sockets.h>
#include <lwip/netdb.h> /* 为了解析主机名需要包含netdb.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)
{
int sock;
struct hostent *host;
struct sockaddr_in server_addr;
int sock;
struct hostent *host;
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)
{
rt_kprintf("Socket error\n");
return;
}
/* 创建一个socket类型是SOCK_DGRAMUDP类型 */
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
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);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
while (count)
{
sendto(sock, send_data, strlen(send_data), 0,
(struct sockaddr *)&server_addr, sizeof(struct sockaddr));
/* 总计发送count次数据 */
while (count)
{
/* 发送数据到服务远端 */
sendto(sock, send_data, strlen(send_data), 0,
(struct sockaddr *)&server_addr, sizeof(struct sockaddr));
rt_thread_delay(50);
/* 线程休眠一段时间 */
rt_thread_delay(50);
count --;
}
/* 计数值减一 */
count --;
}
/* close socket */
lwip_close(sock);
/* 关闭这个socket */
lwip_close(sock);
}
#ifdef RT_USING_FINSH
#include <finsh.h>
/* 输出udpclient函数到finsh shell中 */
FINSH_FUNCTION_EXPORT(udpclient, startup udp client);
#endif

View File

@ -1,74 +1,84 @@
#include <rtthread.h>
#include <lwip/sockets.h>
#include <lwip/sockets.h> /* 使用BSD socket需要包含sockets.h头文件 */
void udpserv(void* paramemter)
{
int sock;
int bytes_read;
char *recv_data;
rt_uint32_t addr_len;
struct sockaddr_in server_addr , client_addr;
int sock;
int bytes_read;
char *recv_data;
rt_uint32_t addr_len;
struct sockaddr_in server_addr, client_addr;
recv_data = rt_malloc(1024);
if (recv_data == RT_NULL)
{
rt_kprintf("No memory\n");
return;
}
/* 分配接收用的数据缓冲 */
recv_data = rt_malloc(1024);
if (recv_data == RT_NULL)
{
/* 分配内存失败,返回 */
rt_kprintf("No memory\n");
return;
}
/* create socket */
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
rt_kprintf("Socket error\n");
/* 创建一个socket类型是SOCK_DGRAMUDP类型 */
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
rt_kprintf("Socket error\n");
/* release recv buffer */
rt_free(recv_data);
return;
}
/* 释放接收用的数据缓冲 */
rt_free(recv_data);
return;
}
/* init server socket address */
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr.s_addr = INADDR_ANY;
rt_memset(&(server_addr.sin_zero),0, sizeof(server_addr.sin_zero));
/* 初始化服务端地址 */
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr.s_addr = INADDR_ANY;
rt_memset(&(server_addr.sin_zero),0, sizeof(server_addr.sin_zero));
if (bind(sock,(struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1)
{
rt_kprintf("Bind error\n");
/* 绑定socket到服务端地址 */
if (bind(sock,(struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1)
{
/* 绑定地址失败 */
rt_kprintf("Bind error\n");
/* release recv buffer */
rt_free(recv_data);
return;
}
/* 释放接收用的数据缓冲 */
rt_free(recv_data);
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)
{
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);
recv_data[bytes_read] = '\0'; /* 把末端清零 */
if (strcmp(recv_data, "exit") == 0)
{
lwip_close(sock);
/* 输出接收的数据 */
rt_kprintf("\n(%s , %d) said : ",inet_ntoa(client_addr.sin_addr),
ntohs(client_addr.sin_port));
rt_kprintf("%s", recv_data);
/* release recv buffer */
rt_free(recv_data);
break;
}
}
/* 如果接收数据是exit退出 */
if (strcmp(recv_data, "exit") == 0)
{
lwip_close(sock);
return;
/* 释放接收用的数据缓冲 */
rt_free(recv_data);
break;
}
}
return;
}
#ifdef RT_USING_FINSH
#include <finsh.h>
/* 输出udpserv函数到finsh shell中 */
FINSH_FUNCTION_EXPORT(udpserv, startup udp server);
#endif