[examples] update:

1. cleanup for posix dfs_net.
2. fixed recv buf overwrite.
3. format code.
This commit is contained in:
aozima 2018-02-06 18:59:24 +08:00
parent ff811a93ff
commit e20fb2f5b2
6 changed files with 254 additions and 238 deletions

View File

@ -1,7 +1,14 @@
#include <rtthread.h> #include <rtthread.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/select.h>
#ifdef RT_USING_DFS_NET
#include <sys/select.h> // only dfs_net
#include <dfs_posix.h> #include <dfs_posix.h>
#else
#define read lwip_read
#define write lwip_write
#endif /* RT_USING_DFS_NET */
#include "netdb.h" #include "netdb.h"
#define MAX_SERV 32 /* Maximum number of chargen services. Don't need too many */ #define MAX_SERV 32 /* Maximum number of chargen services. Don't need too many */
@ -61,7 +68,7 @@ static void chargen_thread(void *arg)
/* Wait forever for network input: This could be connections or data */ /* Wait forever for network input: This could be connections or data */
for (;;) for (;;)
{ {
maxfdp1 = listenfd+1; maxfdp1 = listenfd + 1;
/* Determine what sockets need to be in readset */ /* Determine what sockets need to be in readset */
FD_ZERO(&readset); FD_ZERO(&readset);
@ -113,6 +120,7 @@ static void chargen_thread(void *arg)
closesocket(sock); closesocket(sock);
} }
} }
/* Go through list of connected clients and process data */ /* Go through list of connected clients and process data */
for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next) for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
{ {
@ -124,17 +132,19 @@ static void chargen_thread(void *arg)
if (do_read(p_charcb) < 0) if (do_read(p_charcb) < 0)
break; break;
} }
if (FD_ISSET(p_charcb->socket, &writeset)) if (FD_ISSET(p_charcb->socket, &writeset))
{ {
char line[80]; char line[80];
char setchar = p_charcb->nextchar; char setchar = p_charcb->nextchar;
for( i = 0; i < 59; i++) for (i = 0; i < 59; i++)
{ {
line[i] = setchar; line[i] = setchar;
if (++setchar == 0x7f) if (++setchar == 0x7f)
setchar = 0x21; setchar = 0x21;
} }
line[i] = 0; line[i] = 0;
strcat(line, "\n\r"); strcat(line, "\n\r");
if (write(p_charcb->socket, line, strlen(line)) < 0) if (write(p_charcb->socket, line, strlen(line)) < 0)
@ -142,6 +152,7 @@ static void chargen_thread(void *arg)
close_chargen(p_charcb); close_chargen(p_charcb);
break; break;
} }
if (++p_charcb->nextchar == 0x7f) if (++p_charcb->nextchar == 0x7f)
p_charcb->nextchar = 0x21; p_charcb->nextchar = 0x21;
} }

View File

@ -1,15 +1,12 @@
#include <rtthread.h> #include <rtthread.h>
//#include <lwip/netdb.h> /* 为了解析主机名需要包含netdb.h头文件 */ #include <sys/socket.h> /* 使用BSD socket需要包含socket.h头文件 */
//#include <lwip/sockets.h> /* 使用BSD socket需要包含sockets.h头文件 */
#include <sys/socket.h> /* 使用BSD socket需要包含sockets.h头文件 */
#include "netdb.h" #include "netdb.h"
#define BUFSZ 1024 #define BUFSZ 1024
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)
{ {
int ret; int ret;
char *recv_data; char *recv_data;
@ -57,7 +54,7 @@ void tcpclient(const char* url, int port)
return; return;
} }
while(1) while (1)
{ {
/* 从sock连接中接收最大BUFSZ - 1字节数据 */ /* 从sock连接中接收最大BUFSZ - 1字节数据 */
bytes_received = recv(sock, recv_data, BUFSZ - 1, 0); bytes_received = recv(sock, recv_data, BUFSZ - 1, 0);
@ -75,13 +72,14 @@ void tcpclient(const char* url, int port)
{ {
/* 打印recv函数返回值为0的警告信息 */ /* 打印recv函数返回值为0的警告信息 */
rt_kprintf("\nReceived warning,recv function return 0.\r\n"); rt_kprintf("\nReceived warning,recv function return 0.\r\n");
continue; continue;
} }
/* 有接收到数据,把末端清零 */ /* 有接收到数据,把末端清零 */
recv_data[bytes_received] = '\0'; recv_data[bytes_received] = '\0';
if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0) if (strcmp(recv_data, "q") == 0 || strcmp(recv_data, "Q") == 0)
{ {
/* 如果是首字母是q或Q关闭这个连接 */ /* 如果是首字母是q或Q关闭这个连接 */
closesocket(sock); closesocket(sock);
@ -94,11 +92,11 @@ void tcpclient(const char* url, int port)
else else
{ {
/* 在控制终端显示收到的数据 */ /* 在控制终端显示收到的数据 */
rt_kprintf("\nReceived data = %s " , recv_data); rt_kprintf("\nReceived data = %s ", recv_data);
} }
/* 发送数据到sock连接 */ /* 发送数据到sock连接 */
ret = send(sock,send_data,strlen(send_data), 0); ret = send(sock, send_data, strlen(send_data), 0);
if (ret < 0) if (ret < 0)
{ {
/* 接收失败,关闭这个连接 */ /* 接收失败,关闭这个连接 */

View File

@ -1,8 +1,9 @@
#include <rtthread.h> #include <rtthread.h>
#include <lwip/netdb.h> /* 为了解析主机名需要包含netdb.h头文件 */
#include <lwip/sockets.h> /* 使用BSD socket需要包含sockets.h头文件 */
void tcp_senddata(const char* url, int port, int length) #include <netdb.h> /* 为了解析主机名需要包含netdb.h头文件 */
#include <sys/socket.h> /* 使用BSD socket需要包含socket.h头文件 */
void tcp_senddata(const char *url, int port, int length)
{ {
struct hostent *host; struct hostent *host;
int sock, err, result, timeout, index; int sock, err, result, timeout, index;
@ -27,7 +28,7 @@ void tcp_senddata(const char* url, int port, int length)
timeout = 100; timeout = 100;
/* 设置发送超时时间100ms */ /* 设置发送超时时间100ms */
lwip_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
/* 初始化预连接的服务端地址 */ /* 初始化预连接的服务端地址 */
server_addr.sin_family = AF_INET; server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port); server_addr.sin_port = htons(port);
@ -38,20 +39,20 @@ void tcp_senddata(const char* url, int port, int length)
err = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); err = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
rt_kprintf("TCP thread connect error code: %d\n", err); rt_kprintf("TCP thread connect error code: %d\n", err);
while(1) while (1)
{ {
/* 发送数据到sock连接 */ /* 发送数据到sock连接 */
result = send(sock, buffer_ptr, length, MSG_DONTWAIT); result = send(sock, buffer_ptr, length, MSG_DONTWAIT);
if(result < 0) //数据发送错误处理 if (result < 0) //数据发送错误处理
{ {
rt_kprintf("TCP thread send error: %d\n", result); rt_kprintf("TCP thread send error: %d\n", result);
lwip_close(sock); closesocket(sock);
/* 关闭连接,重新创建连接 */ /* 关闭连接,重新创建连接 */
rt_thread_delay(10); rt_thread_delay(10);
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
rt_kprintf("TCP Socket error:%d\n",sock); rt_kprintf("TCP Socket error:%d\n", sock);
err = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); err = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
rt_kprintf("TCP thread connect error code: %d\n", err); rt_kprintf("TCP thread connect error code: %d\n", err);

View File

@ -1,20 +1,21 @@
#include <rtthread.h> #include <rtthread.h>
//#include <lwip/sockets.h> /* 使用BSD Socket接口必须包含sockets.h这个头文件 */ #include <sys/socket.h> /* 使用BSD socket需要包含socket.h头文件 */
#include <sys/socket.h> /* 使用BSD socket需要包含sockets.h头文件 */
#include "netdb.h" #include "netdb.h"
#define BUFSZ (1024)
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) static void tcpserv(void *parameter)
{ {
char *recv_data; /* 用于接收的指针,后面会做一次动态分配以请求可用内存 */ char *recv_data; /* 用于接收的指针,后面会做一次动态分配以请求可用内存 */
rt_uint32_t sin_size; socklen_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; /* 停止标志 */
int ret; int ret;
recv_data = rt_malloc(1024); /* 分配接收用的数据缓冲 */ recv_data = rt_malloc(BUFSZ + 1); /* 分配接收用的数据缓冲 */
if (recv_data == RT_NULL) if (recv_data == RT_NULL)
{ {
rt_kprintf("No memory\n"); rt_kprintf("No memory\n");
@ -36,7 +37,7 @@ void tcpserv(void* parameter)
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),8, sizeof(server_addr.sin_zero)); rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
/* 绑定socket到服务端地址 */ /* 绑定socket到服务端地址 */
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
@ -60,17 +61,22 @@ void tcpserv(void* parameter)
} }
rt_kprintf("\nTCPServer Waiting for client on port 5000...\n"); rt_kprintf("\nTCPServer Waiting for client on port 5000...\n");
while(stop != RT_TRUE) while (stop != RT_TRUE)
{ {
sin_size = sizeof(struct sockaddr_in); sin_size = sizeof(struct sockaddr_in);
/* 接受一个客户端连接socket的请求这个函数调用是阻塞式的 */ /* 接受一个客户端连接socket的请求这个函数调用是阻塞式的 */
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size); connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
/* 返回的是连接成功的socket */ /* 返回的是连接成功的socket */
if (connected < 0)
{
rt_kprintf("accept connection failed! errno = %d\n", errno);
continue;
}
/* 接受返回的client_addr指向了客户端的地址信息 */ /* 接受返回的client_addr指向了客户端的地址信息 */
rt_kprintf("I got a connection from (%s , %d)\n", rt_kprintf("I got a connection from (%s , %d)\n",
inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port)); inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
/* 客户端连接的处理 */ /* 客户端连接的处理 */
while (1) while (1)
@ -91,7 +97,7 @@ void tcpserv(void* parameter)
} }
/* 从connected socket中接收数据接收buffer是1024大小但并不一定能够收到1024大小的数据 */ /* 从connected socket中接收数据接收buffer是1024大小但并不一定能够收到1024大小的数据 */
bytes_received = recv(connected,recv_data, 1024, 0); bytes_received = recv(connected, recv_data, BUFSZ, 0);
if (bytes_received < 0) if (bytes_received < 0)
{ {
/* 接收失败关闭这个connected socket */ /* 接收失败关闭这个connected socket */
@ -102,12 +108,13 @@ void tcpserv(void* parameter)
{ {
/* 打印recv函数返回值为0的警告信息 */ /* 打印recv函数返回值为0的警告信息 */
rt_kprintf("\nReceived warning,recv function return 0.\r\n"); rt_kprintf("\nReceived warning,recv function return 0.\r\n");
continue; closesocket(connected);
break;
} }
/* 有接收到数据,把末端清零 */ /* 有接收到数据,把末端清零 */
recv_data[bytes_received] = '\0'; recv_data[bytes_received] = '\0';
if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0) if (strcmp(recv_data, "q") == 0 || strcmp(recv_data, "Q") == 0)
{ {
/* 如果是首字母是q或Q关闭这个连接 */ /* 如果是首字母是q或Q关闭这个连接 */
closesocket(connected); closesocket(connected);
@ -123,7 +130,7 @@ void tcpserv(void* parameter)
else else
{ {
/* 在控制终端显示收到的数据 */ /* 在控制终端显示收到的数据 */
rt_kprintf("RECEIVED DATA = %s \n" , recv_data); rt_kprintf("RECEIVED DATA = %s \n", recv_data);
} }
} }
} }

View File

@ -5,14 +5,14 @@
#include "netdb.h" #include "netdb.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;
/* 通过函数入口参数url获得host地址如果是域名会做域名解析 */ /* 通过函数入口参数url获得host地址如果是域名会做域名解析 */
host= (struct hostent *) gethostbyname(url); host = (struct hostent *) gethostbyname(url);
/* 创建一个socket类型是SOCK_DGRAMUDP类型 */ /* 创建一个socket类型是SOCK_DGRAMUDP类型 */
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)

View File

@ -1,17 +1,16 @@
#include <rtthread.h> #include <rtthread.h>
//#include <lwip/sockets.h> /* 使用BSD socket需要包含sockets.h头文件 */
#include <sys/socket.h> /* 使用BSD socket需要包含sockets.h头文件 */
#include "netdb.h"
#include <sys/socket.h> /* 使用BSD socket需要包含socket.h头文件 */
#include "netdb.h"
#define BUFSZ 1024 #define BUFSZ 1024
void udpserv(void* paramemter) static 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; socklen_t addr_len;
struct sockaddr_in server_addr, client_addr; struct sockaddr_in server_addr, client_addr;
/* 分配接收用的数据缓冲 */ /* 分配接收用的数据缓冲 */
@ -37,10 +36,10 @@ void udpserv(void* paramemter)
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));
/* 绑定socket到服务端地址 */ /* 绑定socket到服务端地址 */
if (bind(sock,(struct sockaddr *)&server_addr, if (bind(sock, (struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1) sizeof(struct sockaddr)) == -1)
{ {
/* 绑定地址失败 */ /* 绑定地址失败 */
@ -64,7 +63,7 @@ void udpserv(void* paramemter)
recv_data[bytes_read] = '\0'; /* 把末端清零 */ recv_data[bytes_read] = '\0'; /* 把末端清零 */
/* 输出接收的数据 */ /* 输出接收的数据 */
rt_kprintf("\n(%s , %d) said : ",inet_ntoa(client_addr.sin_addr), rt_kprintf("\n(%s , %d) said : ", inet_ntoa(client_addr.sin_addr),
ntohs(client_addr.sin_port)); ntohs(client_addr.sin_port));
rt_kprintf("%s", recv_data); rt_kprintf("%s", recv_data);