import RT-Thread@9217865c without bsp, libcpu and components/net
This commit is contained in:
commit
e2376a3709
1414 changed files with 390370 additions and 0 deletions
244
examples/network/chargen.c
Normal file
244
examples/network/chargen.c
Normal file
|
@ -0,0 +1,244 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#ifdef SAL_USING_POSIX
|
||||
#include <sys/select.h> // only dfs_net
|
||||
#include <dfs_file.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/statfs.h>
|
||||
#else
|
||||
#define read lwip_read
|
||||
#define write lwip_write
|
||||
#endif /* SAL_USING_POSIX */
|
||||
|
||||
#include "netdb.h"
|
||||
|
||||
#define MAX_SERV 32 /* Maximum number of chargen services. Don't need too many */
|
||||
#define CHARGEN_THREAD_NAME "chargen"
|
||||
#if RT_THREAD_PRIORITY_MAX == 32
|
||||
#define CHARGEN_PRIORITY 20 /* Really low priority */
|
||||
#else
|
||||
#define CHARGEN_PRIORITY 200 /* Really low priority */
|
||||
#endif
|
||||
#define CHARGEN_THREAD_STACKSIZE 1024
|
||||
struct charcb
|
||||
{
|
||||
struct charcb *next;
|
||||
int socket;
|
||||
struct sockaddr_in cliaddr;
|
||||
socklen_t clilen;
|
||||
char nextchar;
|
||||
};
|
||||
|
||||
static struct charcb *charcb_list = 0;
|
||||
static int do_read(struct charcb *p_charcb);
|
||||
static void close_chargen(struct charcb *p_charcb);
|
||||
extern int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
|
||||
|
||||
/**************************************************************
|
||||
* void chargen_thread(void *arg)
|
||||
*
|
||||
* chargen task. This server will wait for connections on well
|
||||
* known TCP port number: 19. For every connection, the server will
|
||||
* write as much data as possible to the tcp port.
|
||||
**************************************************************/
|
||||
static void chargen_thread(void *arg)
|
||||
{
|
||||
int listenfd;
|
||||
struct sockaddr_in chargen_saddr;
|
||||
fd_set readset;
|
||||
fd_set writeset;
|
||||
int i, maxfdp1;
|
||||
struct charcb *p_charcb;
|
||||
|
||||
/* First acquire our socket for listening for connections */
|
||||
listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
LWIP_ASSERT("chargen_thread(): Socket create failed.", listenfd >= 0);
|
||||
memset(&chargen_saddr, 0, sizeof(chargen_saddr));
|
||||
chargen_saddr.sin_family = AF_INET;
|
||||
chargen_saddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
chargen_saddr.sin_port = htons(19); // Chargen server port
|
||||
|
||||
if (bind(listenfd, (struct sockaddr *) &chargen_saddr, sizeof(chargen_saddr)) == -1)
|
||||
LWIP_ASSERT("chargen_thread(): Socket bind failed.", 0);
|
||||
|
||||
/* Put socket into listening mode */
|
||||
if (listen(listenfd, MAX_SERV) == -1)
|
||||
LWIP_ASSERT("chargen_thread(): Listen failed.", 0);
|
||||
|
||||
|
||||
/* Wait forever for network input: This could be connections or data */
|
||||
for (;;)
|
||||
{
|
||||
maxfdp1 = listenfd + 1;
|
||||
|
||||
/* Determine what sockets need to be in readset */
|
||||
FD_ZERO(&readset);
|
||||
FD_ZERO(&writeset);
|
||||
FD_SET(listenfd, &readset);
|
||||
for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
|
||||
{
|
||||
if (maxfdp1 < p_charcb->socket + 1)
|
||||
maxfdp1 = p_charcb->socket + 1;
|
||||
FD_SET(p_charcb->socket, &readset);
|
||||
FD_SET(p_charcb->socket, &writeset);
|
||||
}
|
||||
|
||||
/* Wait for data or a new connection */
|
||||
i = select(maxfdp1, &readset, &writeset, 0, 0);
|
||||
|
||||
if (i == 0) continue;
|
||||
|
||||
/* At least one descriptor is ready */
|
||||
if (FD_ISSET(listenfd, &readset))
|
||||
{
|
||||
/* We have a new connection request!!! */
|
||||
/* Lets create a new control block */
|
||||
p_charcb = (struct charcb *)rt_calloc(1, sizeof(struct charcb));
|
||||
if (p_charcb)
|
||||
{
|
||||
p_charcb->socket = accept(listenfd,
|
||||
(struct sockaddr *) &p_charcb->cliaddr,
|
||||
&p_charcb->clilen);
|
||||
if (p_charcb->socket < 0)
|
||||
rt_free(p_charcb);
|
||||
else
|
||||
{
|
||||
/* Keep this tecb in our list */
|
||||
p_charcb->next = charcb_list;
|
||||
charcb_list = p_charcb;
|
||||
p_charcb->nextchar = 0x21;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* No memory to accept connection. Just accept and then close */
|
||||
int sock;
|
||||
struct sockaddr cliaddr;
|
||||
socklen_t clilen;
|
||||
|
||||
sock = accept(listenfd, &cliaddr, &clilen);
|
||||
if (sock >= 0)
|
||||
closesocket(sock);
|
||||
}
|
||||
}
|
||||
|
||||
/* Go through list of connected clients and process data */
|
||||
for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
|
||||
{
|
||||
if (FD_ISSET(p_charcb->socket, &readset))
|
||||
{
|
||||
/* This socket is ready for reading. This could be because someone typed
|
||||
* some characters or it could be because the socket is now closed. Try reading
|
||||
* some data to see. */
|
||||
if (do_read(p_charcb) < 0)
|
||||
break;
|
||||
}
|
||||
|
||||
if (FD_ISSET(p_charcb->socket, &writeset))
|
||||
{
|
||||
char line[80];
|
||||
char setchar = p_charcb->nextchar;
|
||||
|
||||
for (i = 0; i < 59; i++)
|
||||
{
|
||||
line[i] = setchar;
|
||||
if (++setchar == 0x7f)
|
||||
setchar = 0x21;
|
||||
}
|
||||
|
||||
line[i] = 0;
|
||||
strcat(line, "\n\r");
|
||||
if (write(p_charcb->socket, line, strlen(line)) < 0)
|
||||
{
|
||||
close_chargen(p_charcb);
|
||||
break;
|
||||
}
|
||||
|
||||
if (++p_charcb->nextchar == 0x7f)
|
||||
p_charcb->nextchar = 0x21;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************
|
||||
* void close_chargen(struct charcb *p_charcb)
|
||||
*
|
||||
* Close the socket and remove this charcb from the list.
|
||||
**************************************************************/
|
||||
static void close_chargen(struct charcb *p_charcb)
|
||||
{
|
||||
struct charcb *p_search_charcb;
|
||||
|
||||
/* Either an error or tcp connection closed on other
|
||||
* end. Close here */
|
||||
closesocket(p_charcb->socket);
|
||||
|
||||
/* Free charcb */
|
||||
if (charcb_list == p_charcb)
|
||||
charcb_list = p_charcb->next;
|
||||
else
|
||||
for (p_search_charcb = charcb_list; p_search_charcb; p_search_charcb = p_search_charcb->next)
|
||||
{
|
||||
if (p_search_charcb->next == p_charcb)
|
||||
{
|
||||
p_search_charcb->next = p_charcb->next;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
rt_free(p_charcb);
|
||||
}
|
||||
|
||||
/**************************************************************
|
||||
* void do_read(struct charcb *p_charcb)
|
||||
*
|
||||
* Socket definitely is ready for reading. Read a buffer from the socket and
|
||||
* discard the data. If no data is read, then the socket is closed and the
|
||||
* charcb is removed from the list and freed.
|
||||
**************************************************************/
|
||||
static int do_read(struct charcb *p_charcb)
|
||||
{
|
||||
char buffer[80];
|
||||
int readcount;
|
||||
|
||||
/* Read some data */
|
||||
readcount = read(p_charcb->socket, &buffer, 80);
|
||||
if (readcount <= 0)
|
||||
{
|
||||
close_chargen(p_charcb);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void chargen_init(void)
|
||||
{
|
||||
rt_thread_t chargen;
|
||||
|
||||
chargen = rt_thread_create(CHARGEN_THREAD_NAME,
|
||||
chargen_thread, RT_NULL,
|
||||
CHARGEN_THREAD_STACKSIZE,
|
||||
CHARGEN_PRIORITY, 5);
|
||||
if (chargen != RT_NULL) rt_thread_startup(chargen);
|
||||
}
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
void chargen()
|
||||
{
|
||||
chargen_init();
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(chargen, start chargen server);
|
||||
#endif
|
16
examples/network/tcp_client.py
Normal file
16
examples/network/tcp_client.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
import socket
|
||||
|
||||
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||
|
||||
s.connect(('192.168.10.110',6001))
|
||||
|
||||
print s.recv(1024)
|
||||
|
||||
for data in ['rtt_nano','rtt_thread','rtt_bsp']:
|
||||
s.send(data)
|
||||
print s.recv(1024)
|
||||
|
||||
s.send('exit')
|
||||
s.close()
|
34
examples/network/tcp_server.py
Normal file
34
examples/network/tcp_server.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#引入模块
|
||||
import socket
|
||||
import threading
|
||||
import time
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
# 监听端口:
|
||||
s.bind(('192.168.10.110', 6001))
|
||||
|
||||
s.listen(5)
|
||||
print 'Waiting for connection...'
|
||||
|
||||
def tcp_link(sock,addr):
|
||||
print 'Accept new connection from %s:%s...' % addr
|
||||
sock.send('Welcome to RT-Thread!')
|
||||
while True:
|
||||
data=sock.recv(1024)
|
||||
time.sleep(1)
|
||||
if data=='exit' or not data:
|
||||
break
|
||||
print data
|
||||
sock.send('Hello,%s!'%data)
|
||||
sock.close()
|
||||
print 'Connection from %s:%s closed.'%addr
|
||||
|
||||
while True:
|
||||
|
||||
#接受一个新连接
|
||||
sock,addr=s.accept()
|
||||
|
||||
#创建新线程来处理TCP连接
|
||||
t=threading.Thread(target=tcp_link(sock,addr))
|
||||
|
260
examples/network/tcpclient.c
Normal file
260
examples/network/tcpclient.c
Normal file
|
@ -0,0 +1,260 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-01-24 ChungHsuan improve code comments
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <string.h>
|
||||
|
||||
#if !defined(SAL_USING_POSIX)
|
||||
#error "Please enable SAL_USING_POSIX!"
|
||||
#else
|
||||
#include <sys/time.h>
|
||||
#include <sys/select.h>
|
||||
#endif
|
||||
#include <sys/socket.h> /* socket.h header file is needed when using BSD socket */ /* 使用BSD socket,需要包含socket.h头文件 */
|
||||
#include "netdb.h"
|
||||
|
||||
#define DEBUG_TCP_CLIENT
|
||||
|
||||
#define DBG_TAG "TCP"
|
||||
#ifdef DEBUG_TCP_CLIENT
|
||||
#define DBG_LVL DBG_LOG
|
||||
#else
|
||||
#define DBG_LVL DBG_INFO /* DBG_ERROR */
|
||||
#endif
|
||||
#include <rtdbg.h>
|
||||
|
||||
#define BUFSZ 1024
|
||||
|
||||
static int started = 0;
|
||||
static int is_running = 0;
|
||||
static char url[256];
|
||||
static int port = 8080;
|
||||
static const char send_data[] = "This is TCP Client from RT-Thread."; /* The message be sent */ /* 发送用到的数据 */
|
||||
|
||||
/**
|
||||
* @brief This function is for creating a tcp client on RT-Thread
|
||||
*/
|
||||
static void tcpclient(void *arg)
|
||||
{
|
||||
int ret;
|
||||
char *recv_data;
|
||||
int bytes_received;
|
||||
int sock = -1;
|
||||
struct hostent *host = RT_NULL;
|
||||
struct sockaddr_in server_addr;
|
||||
|
||||
struct timeval timeout;
|
||||
fd_set readset;
|
||||
/* Get host address by parameter url(Domain name resolution if input domain) */
|
||||
/* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
|
||||
host = gethostbyname(url);
|
||||
if (host == RT_NULL)
|
||||
{
|
||||
LOG_E("Get host by name failed!");
|
||||
return;
|
||||
}
|
||||
/* Allocate space for recv_data */
|
||||
/* 分配用于存放接收数据的缓冲 */
|
||||
recv_data = rt_malloc(BUFSZ);
|
||||
if (recv_data == RT_NULL)
|
||||
{
|
||||
LOG_E("No memory");
|
||||
return;
|
||||
}
|
||||
/* Create a socket and set it to SOCK_STREAM(TCP) */
|
||||
/* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
|
||||
{
|
||||
/* Failed on creating socket */
|
||||
/* 创建socket失败 */
|
||||
LOG_E("Create socket error");
|
||||
goto __exit;
|
||||
}
|
||||
/* Initialize server side address */
|
||||
/* 初始化预连接的服务端地址 */
|
||||
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));
|
||||
/* Connect to server */
|
||||
/* 连接到服务端 */
|
||||
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
|
||||
{
|
||||
/* Failed on connecting to server */
|
||||
/* 连接失败 */
|
||||
LOG_E("Connect fail!");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
started = 1;
|
||||
is_running = 1;
|
||||
|
||||
timeout.tv_sec = 3;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
while (is_running)
|
||||
{
|
||||
FD_ZERO(&readset);
|
||||
FD_SET(sock, &readset);
|
||||
|
||||
/* Wait for read */
|
||||
if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0)
|
||||
continue;
|
||||
/* Receive the maximum size 1024 bytes from socket */
|
||||
/* 从sock连接中接收最大BUFSZ - 1字节数据 */
|
||||
bytes_received = recv(sock, recv_data, BUFSZ - 1, 0);
|
||||
if (bytes_received < 0)
|
||||
{
|
||||
/* Receive failed and close the connection */
|
||||
/* 接收失败,关闭这个连接 */
|
||||
LOG_E("Received error, close the socket.");
|
||||
goto __exit;
|
||||
}
|
||||
else if (bytes_received == 0)
|
||||
{
|
||||
/* Print warning message when recv function returns 0 */
|
||||
/* 打印recv函数返回值为0的警告信息 */
|
||||
LOG_W("Received warning, recv function returns 0.");
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Receive data successfully and append '\0' at the end of message */
|
||||
/* 有接收到数据,把末端清零 */
|
||||
recv_data[bytes_received] = '\0';
|
||||
|
||||
if (rt_strcmp(recv_data, "q") == 0 || rt_strcmp(recv_data, "Q") == 0)
|
||||
{
|
||||
/* If the first letter is 'q' or 'Q', close the connection */
|
||||
/* 如果是首字母是q或Q,关闭这个连接 */
|
||||
LOG_I("Got a 'q' or 'Q', close the socket.");
|
||||
goto __exit;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Show the message in terminal */
|
||||
/* 在控制终端显示收到的数据 */
|
||||
LOG_D("Received data = %s", recv_data);
|
||||
}
|
||||
}
|
||||
/* Send message to connected socket */
|
||||
/* 发送数据到sock连接 */
|
||||
ret = send(sock, send_data, rt_strlen(send_data), 0);
|
||||
if (ret < 0)
|
||||
{
|
||||
/* Send failed, close the connection */
|
||||
/* 发送失败,关闭这个连接 */
|
||||
LOG_I("send error, close the socket.");
|
||||
goto __exit;
|
||||
}
|
||||
else if (ret == 0)
|
||||
{
|
||||
/* Print warning message when send function returns 0 */
|
||||
/* 打印send函数返回值为0的警告信息 */
|
||||
LOG_W("Send warning, send function returns 0.");
|
||||
}
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (recv_data)
|
||||
{
|
||||
rt_free(recv_data);
|
||||
recv_data = RT_NULL;
|
||||
}
|
||||
if (sock >= 0)
|
||||
{
|
||||
closesocket(sock);
|
||||
sock = -1;
|
||||
}
|
||||
started = 0;
|
||||
is_running = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The usage description of tcp client on rt-Thread
|
||||
*/
|
||||
static void usage(void)
|
||||
{
|
||||
rt_kprintf("Usage: tcpclient -h <host> -p <port>\n");
|
||||
rt_kprintf(" tcpclient --stop\n");
|
||||
rt_kprintf(" tcpclient --help\n");
|
||||
rt_kprintf("\n");
|
||||
rt_kprintf("Miscellaneous:\n");
|
||||
rt_kprintf(" -h Specify host address\n");
|
||||
rt_kprintf(" -p Specify the host port number\n");
|
||||
rt_kprintf(" --stop Stop tcpclient program\n");
|
||||
rt_kprintf(" --help Print help information\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function is for testing tcp client on rt-Thread
|
||||
*/
|
||||
static void tcpclient_test(int argc, char** argv)
|
||||
{
|
||||
rt_thread_t tid;
|
||||
|
||||
if (argc == 1 || argc > 5)
|
||||
{
|
||||
LOG_I("Please check the command you entered!\n");
|
||||
goto __usage;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rt_strcmp(argv[1], "--help") == 0)
|
||||
{
|
||||
goto __usage;
|
||||
}
|
||||
else if (rt_strcmp(argv[1], "--stop") == 0)
|
||||
{
|
||||
is_running = 0;
|
||||
return;
|
||||
}
|
||||
else if (rt_strcmp(argv[1], "-h") == 0 && rt_strcmp(argv[3], "-p") == 0)
|
||||
{
|
||||
if (started)
|
||||
{
|
||||
LOG_I("The tcpclient has started!");
|
||||
LOG_I("Please stop tcpclient firstly, by: tcpclient --stop");
|
||||
return;
|
||||
}
|
||||
|
||||
if (rt_strlen(argv[2]) > sizeof(url))
|
||||
{
|
||||
LOG_E("The input url is too long, max %d bytes!", sizeof(url));
|
||||
return;
|
||||
}
|
||||
rt_memset(url, 0x0, sizeof(url));
|
||||
rt_strncpy(url, argv[2], rt_strlen(argv[2]));
|
||||
port = atoi(argv[4]);
|
||||
}
|
||||
else
|
||||
{
|
||||
goto __usage;
|
||||
}
|
||||
}
|
||||
|
||||
tid = rt_thread_create("tcp_client",
|
||||
tcpclient, RT_NULL,
|
||||
2048, RT_THREAD_PRIORITY_MAX/3, 20);
|
||||
if (tid != RT_NULL)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
return;
|
||||
|
||||
__usage:
|
||||
usage();
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
MSH_CMD_EXPORT_ALIAS(tcpclient_test, tcpclient,
|
||||
Start a tcp client. Help: tcpclient --help);
|
||||
#endif
|
89
examples/network/tcpsendpacket.c
Normal file
89
examples/network/tcpsendpacket.c
Normal file
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#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;
|
||||
int sock, err, result, timeout, index;
|
||||
struct sockaddr_in server_addr;
|
||||
rt_uint8_t *buffer_ptr;
|
||||
|
||||
/* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
|
||||
host = gethostbyname(url);
|
||||
/* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
|
||||
{
|
||||
/* 创建socket失败 */
|
||||
rt_kprintf("Socket error\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* 申请内存 */
|
||||
buffer_ptr = rt_malloc(length);
|
||||
if(RT_NULL == buffer_ptr)
|
||||
{
|
||||
/* 申请内存失败 */
|
||||
rt_kprintf("No memory\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* 构造发送数据 */
|
||||
for (index = 0; index < length; index ++)
|
||||
buffer_ptr[index] = index & 0xff;
|
||||
|
||||
timeout = 100;
|
||||
/* 设置发送超时时间100ms */
|
||||
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
|
||||
/* 初始化预连接的服务端地址 */
|
||||
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));
|
||||
|
||||
/* 连接到服务端 */
|
||||
err = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
|
||||
rt_kprintf("TCP thread connect error code: %d\n", err);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 发送数据到sock连接 */
|
||||
result = send(sock, buffer_ptr, length, MSG_DONTWAIT);
|
||||
if (result < 0) //数据发送错误处理
|
||||
{
|
||||
rt_kprintf("TCP thread send error: %d\n", result);
|
||||
closesocket(sock);
|
||||
|
||||
/* 关闭连接,重新创建连接 */
|
||||
rt_thread_delay(10);
|
||||
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
|
||||
rt_kprintf("TCP Socket error:%d\n", sock);
|
||||
|
||||
err = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
|
||||
rt_kprintf("TCP thread connect error code: %d\n", err);
|
||||
}
|
||||
else if (result == 0)
|
||||
{
|
||||
/* 打印send函数返回值为0的警告信息 */
|
||||
rt_kprintf("\n Send warning,send function returns 0.\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
/* 输出tcpclient函数到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(tcp_senddata, send a packet through tcp connection);
|
||||
#endif
|
||||
|
286
examples/network/tcpserver.c
Normal file
286
examples/network/tcpserver.c
Normal file
|
@ -0,0 +1,286 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-01-24 ChungHsuan improve code comments
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <string.h>
|
||||
|
||||
#if !defined(SAL_USING_POSIX)
|
||||
#error "Please enable SAL_USING_POSIX!"
|
||||
#else
|
||||
#include <sys/time.h>
|
||||
#include <sys/select.h>
|
||||
#endif
|
||||
#include <sys/socket.h> /* socket.h header file is needed when using BSD socket */ /* 使用BSD socket,需要包含socket.h头文件 */
|
||||
#include "netdb.h"
|
||||
|
||||
#define DEBUG_TCP_SERVER
|
||||
|
||||
#define DBG_TAG "TCP"
|
||||
#ifdef DEBUG_TCP_SERVER
|
||||
#define DBG_LVL DBG_LOG
|
||||
#else
|
||||
#define DBG_LVL DBG_INFO /* DBG_ERROR */
|
||||
#endif
|
||||
#include <rtdbg.h>
|
||||
|
||||
#define BUFSZ (1024)
|
||||
|
||||
static int started = 0;
|
||||
static int is_running = 0;
|
||||
static int port = 5000;
|
||||
static const char send_data[] = "This is TCP Server from RT-Thread."; /* The message be sent */ /* 发送用到的数据 */
|
||||
|
||||
/**
|
||||
* @brief This function is for creating a tcp server on RT-Thread
|
||||
*/
|
||||
static void tcpserv(void *arg)
|
||||
{
|
||||
int ret;
|
||||
char *recv_data; /* recv_data is a pointer used to receive data */ /* 用于接收的指针,后面会做一次动态分配以请求可用内存 */
|
||||
int sock, connected, bytes_received;
|
||||
struct sockaddr_in server_addr, client_addr;
|
||||
|
||||
struct timeval timeout;
|
||||
fd_set readset, readset_c;
|
||||
socklen_t sin_size = sizeof(struct sockaddr_in);
|
||||
|
||||
recv_data = rt_malloc(BUFSZ + 1);/* Allocate space for recv_data */ /* 分配接收用的数据缓冲 */
|
||||
if (recv_data == RT_NULL)
|
||||
{
|
||||
LOG_E("No memory");
|
||||
return;
|
||||
}
|
||||
/* Before making use of socket, socket should be created first and set the socket created to SOCK_STREAM(TCP) */
|
||||
/* 一个socket在使用前,需要预先创建出来,指定SOCK_STREAM为TCP的socket */
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
|
||||
{
|
||||
LOG_E("Create socket error");
|
||||
goto __exit;
|
||||
}
|
||||
/* Initialize server side address */
|
||||
/* 初始化服务端地址 */
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_port = htons(port); /*Server side port number*//* 服务端工作的端口 */
|
||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
rt_memset(&(server_addr.sin_zero), 0x0, sizeof(server_addr.sin_zero));
|
||||
/* Bind socket to server side address */
|
||||
/* 绑定socket到服务端地址 */
|
||||
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
|
||||
{
|
||||
LOG_E("Unable to bind");
|
||||
goto __exit;
|
||||
}
|
||||
/* Listen on socket */
|
||||
/* 在socket上进行监听 */
|
||||
if (listen(sock, 10) == -1)
|
||||
{
|
||||
LOG_E("Listen error");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
LOG_I("\nTCPServer Waiting for client on port %d...\n", port);
|
||||
|
||||
started = 1;
|
||||
is_running = 1;
|
||||
|
||||
timeout.tv_sec = 3;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
while (is_running)
|
||||
{
|
||||
FD_ZERO(&readset);
|
||||
FD_SET(sock, &readset);
|
||||
|
||||
LOG_I("Waiting for a new connection...");
|
||||
|
||||
/* Wait for read or write */
|
||||
if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0)
|
||||
continue;
|
||||
/* Accept a request from client and the function is blocking */
|
||||
/* 接受一个客户端连接socket的请求,这个函数调用是阻塞式的 */
|
||||
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
|
||||
/* Return the socket connected successfully */
|
||||
/* 返回的是连接成功的socket */
|
||||
if (connected < 0)
|
||||
{
|
||||
LOG_E("accept connection failed! errno = %d", errno);
|
||||
continue;
|
||||
}
|
||||
/* Accept the message which points by client address */
|
||||
/* 接受返回的client_addr指向了客户端的地址信息 */
|
||||
LOG_I("I got a connection from (%s , %d)\n",
|
||||
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
|
||||
/* Handle method of client connection */
|
||||
/* 客户端连接的处理 */
|
||||
while (is_running)
|
||||
{
|
||||
FD_ZERO(&readset_c);
|
||||
FD_SET(connected, &readset_c);
|
||||
|
||||
/* Wait for read or write */
|
||||
if (select(connected + 1, &readset_c, RT_NULL, RT_NULL, &timeout) == 0)
|
||||
continue;
|
||||
/* Receive message from connected socket. Buffer size is 1024 bytes,but it's not guranteed to receive size exactly 1024 */
|
||||
/* 从connected socket中接收数据,接收buffer是1024大小,但并不一定能够收到1024大小的数据 */
|
||||
bytes_received = recv(connected, recv_data, BUFSZ, 0);
|
||||
if (bytes_received < 0)
|
||||
{
|
||||
LOG_E("Received error, close the connect.");
|
||||
closesocket(connected);
|
||||
connected = -1;
|
||||
break;
|
||||
}
|
||||
else if (bytes_received == 0)
|
||||
{
|
||||
/* Print warning message when recv function returns 0 */
|
||||
/* 打印recv函数返回值为0的警告信息 */
|
||||
LOG_W("Received warning, recv function returns 0.");
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{ /* Receive data successfully and append '\0' at the end of message */
|
||||
/* 有接收到数据,把末端清零 */
|
||||
recv_data[bytes_received] = '\0';
|
||||
if (strcmp(recv_data, "q") == 0 || strcmp(recv_data, "Q") == 0)
|
||||
{
|
||||
/* If the first letter is 'q' or 'Q', close the connection */
|
||||
/* 如果是首字母是q或Q,关闭这个连接 */
|
||||
LOG_I("Got a 'q' or 'Q', close the connect.");
|
||||
closesocket(connected);
|
||||
connected = -1;
|
||||
break;
|
||||
}
|
||||
else if (strcmp(recv_data, "exit") == 0)
|
||||
{
|
||||
/* If the message received is 'exit', close the whole server side. */
|
||||
/* 如果接收的是exit,则关闭整个服务端 */
|
||||
closesocket(connected);
|
||||
connected = -1;
|
||||
goto __exit;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Show the message in terminal */
|
||||
/* 在控制终端显示收到的数据 */
|
||||
LOG_D("Received data = %s", recv_data);
|
||||
}
|
||||
}
|
||||
/* Send message to connected socket */
|
||||
/* 发送数据到connected socket */
|
||||
ret = send(connected, send_data, rt_strlen(send_data), 0);
|
||||
if (ret < 0)
|
||||
{
|
||||
LOG_E("send error, close the connect.");
|
||||
closesocket(connected);
|
||||
connected = -1;
|
||||
break;
|
||||
}
|
||||
else if (ret == 0)
|
||||
{
|
||||
/* Print warning message when send function returns 0 */
|
||||
/* 打印send函数返回值为0的警告信息 */
|
||||
LOG_W("Send warning, send function returns 0.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (recv_data)
|
||||
{
|
||||
rt_free(recv_data);
|
||||
recv_data = RT_NULL;
|
||||
}
|
||||
if (connected >= 0)
|
||||
{
|
||||
closesocket(connected);
|
||||
connected = -1;
|
||||
}
|
||||
if (sock >= 0)
|
||||
{
|
||||
closesocket(sock);
|
||||
sock = -1;
|
||||
}
|
||||
started = 0;
|
||||
is_running = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The usage description of tcp server on rt-Thread
|
||||
*/
|
||||
static void usage(void)
|
||||
{
|
||||
rt_kprintf("Usage: tcpserver -p <port>\n");
|
||||
rt_kprintf(" tcpserver --stop\n");
|
||||
rt_kprintf(" tcpserver --help\n");
|
||||
rt_kprintf("\n");
|
||||
rt_kprintf("Miscellaneous:\n");
|
||||
rt_kprintf(" -p Specify the host port number\n");
|
||||
rt_kprintf(" --stop Stop tcpserver program\n");
|
||||
rt_kprintf(" --help Print help information\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function is for testing tcp server on rt-Thread
|
||||
*/
|
||||
static void tcpserver_test(int argc, char** argv)
|
||||
{
|
||||
rt_thread_t tid;
|
||||
|
||||
if (argc == 1 || argc > 3)
|
||||
{
|
||||
LOG_I("Please check the command you entered!\n");
|
||||
goto __usage;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rt_strcmp(argv[1], "--help") == 0)
|
||||
{
|
||||
goto __usage;
|
||||
}
|
||||
else if (rt_strcmp(argv[1], "--stop") == 0)
|
||||
{
|
||||
is_running = 0;
|
||||
return;
|
||||
}
|
||||
else if (rt_strcmp(argv[1], "-p") == 0)
|
||||
{
|
||||
if (started)
|
||||
{
|
||||
LOG_I("The tcpserver has started!");
|
||||
LOG_I("Please stop tcpserver firstly, by: tcpserver --stop");
|
||||
return;
|
||||
}
|
||||
|
||||
port = atoi(argv[2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
goto __usage;
|
||||
}
|
||||
}
|
||||
|
||||
tid = rt_thread_create("tcp_serv",
|
||||
tcpserv, RT_NULL,
|
||||
2048, RT_THREAD_PRIORITY_MAX/3, 20);
|
||||
if (tid != RT_NULL)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
return;
|
||||
|
||||
__usage:
|
||||
usage();
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
MSH_CMD_EXPORT_ALIAS(tcpserver_test, tcpserver,
|
||||
Start a tcp server. Help: tcpserver --help);
|
||||
#endif
|
182
examples/network/udpclient.c
Normal file
182
examples/network/udpclient.c
Normal file
|
@ -0,0 +1,182 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-01-24 ChungHsuan improve code comments
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#include <sys/socket.h> /* socket.h header file is needed when using BSD socket */ /* 使用BSD socket,需要包含sockets.h头文件 */
|
||||
#include "netdb.h"
|
||||
|
||||
#define DEBUG_UDP_CLIENT
|
||||
|
||||
#define DBG_TAG "UDP"
|
||||
#ifdef DEBUG_UDP_CLIENT
|
||||
#define DBG_LVL DBG_LOG
|
||||
#else
|
||||
#define DBG_LVL DBG_INFO /* DBG_ERROR */
|
||||
#endif
|
||||
#include <rtdbg.h>
|
||||
|
||||
static int started = 0;
|
||||
static int is_running = 0;
|
||||
static char url[256];
|
||||
static int port = 8080;
|
||||
static int count = 10;
|
||||
const char send_data[] = "This is UDP Client from RT-Thread.\n";/* The message be sent */ /* 发送用到的数据 */
|
||||
|
||||
/**
|
||||
* @brief This function is for creating a udp client on RT-Thread
|
||||
*/
|
||||
static void udpclient(void *arg)
|
||||
{
|
||||
int sock;
|
||||
struct hostent *host;
|
||||
struct sockaddr_in server_addr;
|
||||
/* Get host address by parameter URL (domain name resolution if input domain) */
|
||||
/* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
|
||||
host = (struct hostent *) gethostbyname(url);
|
||||
if (host == RT_NULL)
|
||||
{
|
||||
LOG_E("Get host by name failed!");
|
||||
return;
|
||||
}
|
||||
/* Create a socket and set it to SOCK_DGRAM(UDP) */
|
||||
/* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */
|
||||
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
|
||||
{
|
||||
/* Failed on creating socket */
|
||||
LOG_E("Create socket error");
|
||||
return;
|
||||
}
|
||||
/* Initialize server side address */
|
||||
/* 初始化预连接的服务端地址 */
|
||||
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));
|
||||
|
||||
started = 1;
|
||||
is_running = 1;
|
||||
/* The total sending number(count) */
|
||||
/* 总计发送count次数据 */
|
||||
while (count && is_running)
|
||||
{
|
||||
/* Send message to server side */
|
||||
/* 发送数据到服务远端 */
|
||||
sendto(sock, send_data, rt_strlen(send_data), 0,
|
||||
(struct sockaddr *)&server_addr, sizeof(struct sockaddr));
|
||||
/* Thread sleep for 1 second */
|
||||
/* 线程休眠一段时间 */
|
||||
rt_thread_mdelay(1000);
|
||||
/* count decrease 1 */
|
||||
/* 计数值减一 */
|
||||
count --;
|
||||
}
|
||||
|
||||
if (count == 0)
|
||||
{
|
||||
LOG_I("UDP client send data finished!");
|
||||
}
|
||||
/* Close the socket */
|
||||
/* 关闭这个socket */
|
||||
if (sock >= 0)
|
||||
{
|
||||
closesocket(sock);
|
||||
sock = -1;
|
||||
}
|
||||
started = 0;
|
||||
is_running = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The usage description of udp client on rt-Thread
|
||||
*/
|
||||
static void usage(void)
|
||||
{
|
||||
rt_kprintf("Usage: udpclient -h <host> -p <port> [--cnt] [count]\n");
|
||||
rt_kprintf(" udpclient --stop\n");
|
||||
rt_kprintf(" udpclient --help\n");
|
||||
rt_kprintf("\n");
|
||||
rt_kprintf("Miscellaneous:\n");
|
||||
rt_kprintf(" -h Specify host address\n");
|
||||
rt_kprintf(" -p Specify the host port number\n");
|
||||
rt_kprintf(" --cnt Specify the send data count\n");
|
||||
rt_kprintf(" --stop Stop udpclient program\n");
|
||||
rt_kprintf(" --help Print help information\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function is for testing udp client on rt-Thread
|
||||
*/
|
||||
static void udpclient_test(int argc, char** argv)
|
||||
{
|
||||
rt_thread_t tid;
|
||||
|
||||
if (argc == 1 || argc > 7)
|
||||
{
|
||||
LOG_I("Please check the command you entered!\n");
|
||||
goto __usage;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rt_strcmp(argv[1], "--help") == 0)
|
||||
{
|
||||
goto __usage;
|
||||
}
|
||||
else if (rt_strcmp(argv[1], "--stop") == 0)
|
||||
{
|
||||
is_running = 0;
|
||||
return;
|
||||
}
|
||||
else if (rt_strcmp(argv[1], "-h") == 0 && rt_strcmp(argv[3], "-p") == 0)
|
||||
{
|
||||
if (started)
|
||||
{
|
||||
LOG_I("The udpclient has started!");
|
||||
LOG_I("Please stop udpclient firstly, by: udpclient --stop");
|
||||
return;
|
||||
}
|
||||
|
||||
if (argc == 7 && rt_strcmp(argv[6], "--cnt") == 0)
|
||||
{
|
||||
count = atoi(argv[7]);
|
||||
}
|
||||
|
||||
if (rt_strlen(argv[2]) > sizeof(url))
|
||||
{
|
||||
LOG_E("The input url is too long, max %d bytes!", sizeof(url));
|
||||
return;
|
||||
}
|
||||
rt_memset(url, 0x0, sizeof(url));
|
||||
rt_strncpy(url, argv[2], rt_strlen(argv[2]));
|
||||
port = atoi(argv[4]);
|
||||
}
|
||||
else
|
||||
{
|
||||
goto __usage;
|
||||
}
|
||||
}
|
||||
|
||||
tid = rt_thread_create("udp_client",
|
||||
udpclient, RT_NULL,
|
||||
2048, RT_THREAD_PRIORITY_MAX/3, 20);
|
||||
if (tid != RT_NULL)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
return;
|
||||
|
||||
__usage:
|
||||
usage();
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
MSH_CMD_EXPORT_ALIAS(udpclient_test, udpclient,
|
||||
Start a udp client. Help: udpclient --help);
|
||||
#endif
|
214
examples/network/udpserver.c
Normal file
214
examples/network/udpserver.c
Normal file
|
@ -0,0 +1,214 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-01-24 ChungHsuan improve code comments
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <string.h>
|
||||
|
||||
#if !defined(SAL_USING_POSIX)
|
||||
#error "Please enable SAL_USING_POSIX!"
|
||||
#else
|
||||
#include <sys/time.h>
|
||||
#include <sys/select.h>
|
||||
#endif
|
||||
#include <sys/socket.h> /* socket.h header file is needed when using BSD socket */ /* 使用BSD socket,需要包含socket.h头文件 */
|
||||
#include "netdb.h"
|
||||
|
||||
#define DEBUG_UDP_SERVER
|
||||
|
||||
#define DBG_TAG "UDP"
|
||||
#ifdef DEBUG_UDP_SERVER
|
||||
#define DBG_LVL DBG_LOG
|
||||
#else
|
||||
#define DBG_LVL DBG_INFO /* DBG_ERROR */
|
||||
#endif
|
||||
#include <rtdbg.h>
|
||||
|
||||
#define BUFSZ 1024
|
||||
|
||||
static int started = 0;
|
||||
static int is_running = 0;
|
||||
static int port = 5000;
|
||||
|
||||
/**
|
||||
* @brief This function is for creating a udp server on RT-Thread
|
||||
*/
|
||||
static void udpserv(void *paramemter)
|
||||
{
|
||||
int sock;
|
||||
int bytes_read;
|
||||
char *recv_data;
|
||||
socklen_t addr_len;
|
||||
struct sockaddr_in server_addr, client_addr;
|
||||
|
||||
struct timeval timeout;
|
||||
fd_set readset;
|
||||
/* Allocate space for recv_data */
|
||||
/* 分配接收用的数据缓冲 */
|
||||
recv_data = rt_malloc(BUFSZ);
|
||||
if (recv_data == RT_NULL)
|
||||
{
|
||||
LOG_E("No memory");
|
||||
return;
|
||||
}
|
||||
/* Create a socket and set it to SOCK_DGRAM(UDP) */
|
||||
/* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */
|
||||
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
|
||||
{
|
||||
LOG_E("Create socket error");
|
||||
goto __exit;
|
||||
}
|
||||
/* Initialize server side address */
|
||||
/* 初始化服务端地址 */
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_port = htons(port);
|
||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
|
||||
/* Bind socket to server side address */
|
||||
/* 绑定socket到服务端地址 */
|
||||
if (bind(sock, (struct sockaddr *)&server_addr,
|
||||
sizeof(struct sockaddr)) == -1)
|
||||
{
|
||||
LOG_E("Unable to bind");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
addr_len = sizeof(struct sockaddr);
|
||||
LOG_I("UDPServer Waiting for client on port %d...", port);
|
||||
|
||||
started = 1;
|
||||
is_running = 1;
|
||||
|
||||
timeout.tv_sec = 3;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
while (is_running)
|
||||
{
|
||||
FD_ZERO(&readset);
|
||||
FD_SET(sock, &readset);
|
||||
|
||||
/* Wait for read or write */
|
||||
if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0)
|
||||
continue;
|
||||
/* The maximum size received from sock is BUFSZ-1 bytes*/
|
||||
/* 从sock中收取最大BUFSZ - 1字节数据 */
|
||||
bytes_read = recvfrom(sock, recv_data, BUFSZ - 1, 0,
|
||||
(struct sockaddr *)&client_addr, &addr_len);
|
||||
if (bytes_read < 0)
|
||||
{
|
||||
LOG_E("Received error, close the connect.");
|
||||
goto __exit;
|
||||
}
|
||||
else if (bytes_read == 0)
|
||||
{
|
||||
LOG_W("Received warning, recv function returns 0.");
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
recv_data[bytes_read] = '\0'; /* Append '\0' at the end of message *//* 把末端清零 */
|
||||
/* Output received message */
|
||||
/* 输出接收的数据 */
|
||||
LOG_D("Received data = %s", recv_data);
|
||||
/* If the message received is 'exit', quit. */
|
||||
/* 如果接收数据是exit,退出 */
|
||||
if (strcmp(recv_data, "exit") == 0)
|
||||
{
|
||||
goto __exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (recv_data)
|
||||
{
|
||||
rt_free(recv_data);
|
||||
recv_data = RT_NULL;
|
||||
}
|
||||
if (sock >= 0)
|
||||
{
|
||||
closesocket(sock);
|
||||
sock = -1;
|
||||
}
|
||||
started = 0;
|
||||
is_running = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The usage description of udp server on rt-Thread
|
||||
*/
|
||||
static void usage(void)
|
||||
{
|
||||
rt_kprintf("Usage: udpserver -p <port>\n");
|
||||
rt_kprintf(" udpserver --stop\n");
|
||||
rt_kprintf(" udpserver --help\n");
|
||||
rt_kprintf("\n");
|
||||
rt_kprintf("Miscellaneous:\n");
|
||||
rt_kprintf(" -p Specify the host port number\n");
|
||||
rt_kprintf(" --stop Stop udpserver program\n");
|
||||
rt_kprintf(" --help Print help information\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function is for testing udp server on rt-Thread
|
||||
*/
|
||||
static void udpserver_test(int argc, char** argv)
|
||||
{
|
||||
rt_thread_t tid;
|
||||
|
||||
if (argc == 1 || argc > 3)
|
||||
{
|
||||
LOG_I("Please check the command you entered!\n");
|
||||
goto __usage;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rt_strcmp(argv[1], "--help") == 0)
|
||||
{
|
||||
goto __usage;
|
||||
}
|
||||
else if (rt_strcmp(argv[1], "--stop") == 0)
|
||||
{
|
||||
is_running = 0;
|
||||
return;
|
||||
}
|
||||
else if (rt_strcmp(argv[1], "-p") == 0)
|
||||
{
|
||||
if (started)
|
||||
{
|
||||
LOG_I("The udpserver has started!");
|
||||
LOG_I("Please stop udpserver firstly, by: udpserver --stop");
|
||||
return;
|
||||
}
|
||||
|
||||
port = atoi(argv[2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
goto __usage;
|
||||
}
|
||||
}
|
||||
|
||||
tid = rt_thread_create("udp_serv",
|
||||
udpserv, RT_NULL,
|
||||
2048, RT_THREAD_PRIORITY_MAX/3, 20);
|
||||
if (tid != RT_NULL)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
return;
|
||||
|
||||
__usage:
|
||||
usage();
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
MSH_CMD_EXPORT_ALIAS(udpserver_test, udpserver,
|
||||
Start a udp server. Help: udpserver --help);
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue