[examples] update:
1. cleanup for posix dfs_net. 2. fixed recv buf overwrite. 3. format code.
This commit is contained in:
parent
ff811a93ff
commit
e20fb2f5b2
|
@ -1,7 +1,14 @@
|
|||
#include <rtthread.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>
|
||||
#else
|
||||
#define read lwip_read
|
||||
#define write lwip_write
|
||||
#endif /* RT_USING_DFS_NET */
|
||||
|
||||
#include "netdb.h"
|
||||
|
||||
#define MAX_SERV 32 /* Maximum number of chargen services. Don't need too many */
|
||||
|
@ -113,6 +120,7 @@ static void chargen_thread(void *arg)
|
|||
closesocket(sock);
|
||||
}
|
||||
}
|
||||
|
||||
/* Go through list of connected clients and process data */
|
||||
for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
|
||||
{
|
||||
|
@ -124,6 +132,7 @@ static void chargen_thread(void *arg)
|
|||
if (do_read(p_charcb) < 0)
|
||||
break;
|
||||
}
|
||||
|
||||
if (FD_ISSET(p_charcb->socket, &writeset))
|
||||
{
|
||||
char line[80];
|
||||
|
@ -135,6 +144,7 @@ static void chargen_thread(void *arg)
|
|||
if (++setchar == 0x7f)
|
||||
setchar = 0x21;
|
||||
}
|
||||
|
||||
line[i] = 0;
|
||||
strcat(line, "\n\r");
|
||||
if (write(p_charcb->socket, line, strlen(line)) < 0)
|
||||
|
@ -142,6 +152,7 @@ static void chargen_thread(void *arg)
|
|||
close_chargen(p_charcb);
|
||||
break;
|
||||
}
|
||||
|
||||
if (++p_charcb->nextchar == 0x7f)
|
||||
p_charcb->nextchar = 0x21;
|
||||
}
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
#include <rtthread.h>
|
||||
|
||||
//#include <lwip/netdb.h> /* 为了解析主机名,需要包含netdb.h头文件 */
|
||||
//#include <lwip/sockets.h> /* 使用BSD socket,需要包含sockets.h头文件 */
|
||||
|
||||
#include <sys/socket.h> /* 使用BSD socket,需要包含sockets.h头文件 */
|
||||
#include <sys/socket.h> /* 使用BSD socket,需要包含socket.h头文件 */
|
||||
#include "netdb.h"
|
||||
|
||||
#define BUFSZ 1024
|
||||
|
@ -75,6 +72,7 @@ void tcpclient(const char* url, int port)
|
|||
{
|
||||
/* 打印recv函数返回值为0的警告信息 */
|
||||
rt_kprintf("\nReceived warning,recv function return 0.\r\n");
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <rtthread.h>
|
||||
#include <lwip/netdb.h> /* 为了解析主机名,需要包含netdb.h头文件 */
|
||||
#include <lwip/sockets.h> /* 使用BSD socket,需要包含sockets.h头文件 */
|
||||
|
||||
#include <netdb.h> /* 为了解析主机名,需要包含netdb.h头文件 */
|
||||
#include <sys/socket.h> /* 使用BSD socket,需要包含socket.h头文件 */
|
||||
|
||||
void tcp_senddata(const char *url, int port, int length)
|
||||
{
|
||||
|
@ -27,7 +28,7 @@ void tcp_senddata(const char* url, int port, int length)
|
|||
|
||||
timeout = 100;
|
||||
/* 设置发送超时时间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_port = htons(port);
|
||||
|
@ -45,7 +46,7 @@ void tcp_senddata(const char* url, int port, int length)
|
|||
if (result < 0) //数据发送错误处理
|
||||
{
|
||||
rt_kprintf("TCP thread send error: %d\n", result);
|
||||
lwip_close(sock);
|
||||
closesocket(sock);
|
||||
|
||||
/* 关闭连接,重新创建连接 */
|
||||
rt_thread_delay(10);
|
||||
|
|
|
@ -1,20 +1,21 @@
|
|||
#include <rtthread.h>
|
||||
|
||||
//#include <lwip/sockets.h> /* 使用BSD Socket接口必须包含sockets.h这个头文件 */
|
||||
#include <sys/socket.h> /* 使用BSD socket,需要包含sockets.h头文件 */
|
||||
#include <sys/socket.h> /* 使用BSD socket,需要包含socket.h头文件 */
|
||||
#include "netdb.h"
|
||||
|
||||
#define BUFSZ (1024)
|
||||
|
||||
static const char send_data[] = "This is TCP Server from RT-Thread."; /* 发送用到的数据 */
|
||||
void tcpserv(void* parameter)
|
||||
static void tcpserv(void *parameter)
|
||||
{
|
||||
char *recv_data; /* 用于接收的指针,后面会做一次动态分配以请求可用内存 */
|
||||
rt_uint32_t sin_size;
|
||||
socklen_t sin_size;
|
||||
int sock, connected, bytes_received;
|
||||
struct sockaddr_in server_addr, client_addr;
|
||||
rt_bool_t stop = RT_FALSE; /* 停止标志 */
|
||||
int ret;
|
||||
|
||||
recv_data = rt_malloc(1024); /* 分配接收用的数据缓冲 */
|
||||
recv_data = rt_malloc(BUFSZ + 1); /* 分配接收用的数据缓冲 */
|
||||
if (recv_data == RT_NULL)
|
||||
{
|
||||
rt_kprintf("No memory\n");
|
||||
|
@ -36,7 +37,7 @@ void tcpserv(void* parameter)
|
|||
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));
|
||||
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
|
||||
|
||||
/* 绑定socket到服务端地址 */
|
||||
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
|
||||
|
@ -67,6 +68,11 @@ void tcpserv(void* parameter)
|
|||
/* 接受一个客户端连接socket的请求,这个函数调用是阻塞式的 */
|
||||
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
|
||||
/* 返回的是连接成功的socket */
|
||||
if (connected < 0)
|
||||
{
|
||||
rt_kprintf("accept connection failed! errno = %d\n", errno);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* 接受返回的client_addr指向了客户端的地址信息 */
|
||||
rt_kprintf("I got a connection from (%s , %d)\n",
|
||||
|
@ -91,7 +97,7 @@ void tcpserv(void* parameter)
|
|||
}
|
||||
|
||||
/* 从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)
|
||||
{
|
||||
/* 接收失败,关闭这个connected socket */
|
||||
|
@ -102,7 +108,8 @@ void tcpserv(void* parameter)
|
|||
{
|
||||
/* 打印recv函数返回值为0的警告信息 */
|
||||
rt_kprintf("\nReceived warning,recv function return 0.\r\n");
|
||||
continue;
|
||||
closesocket(connected);
|
||||
break;
|
||||
}
|
||||
|
||||
/* 有接收到数据,把末端清零 */
|
||||
|
|
|
@ -1,17 +1,16 @@
|
|||
#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
|
||||
|
||||
void udpserv(void* paramemter)
|
||||
static void udpserv(void *paramemter)
|
||||
{
|
||||
int sock;
|
||||
int bytes_read;
|
||||
char *recv_data;
|
||||
rt_uint32_t addr_len;
|
||||
socklen_t addr_len;
|
||||
struct sockaddr_in server_addr, client_addr;
|
||||
|
||||
/* 分配接收用的数据缓冲 */
|
||||
|
|
Loading…
Reference in New Issue