2013-01-08 21:05:02 +08:00
|
|
|
|
#include <rtthread.h>
|
2018-11-05 14:34:40 +08:00
|
|
|
|
#include <string.h>
|
2017-10-27 11:39:19 +08:00
|
|
|
|
|
2018-11-05 14:34:40 +08:00
|
|
|
|
#if !defined(SAL_USING_POSIX)
|
|
|
|
|
#error "Please enable SAL_USING_POSIX!"
|
|
|
|
|
#else
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
#endif
|
2018-02-06 18:59:24 +08:00
|
|
|
|
#include <sys/socket.h> /* 使用BSD socket,需要包含socket.h头文件 */
|
2017-10-27 11:39:19 +08:00
|
|
|
|
#include "netdb.h"
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
2018-11-05 14:34:40 +08:00
|
|
|
|
#define DEBUG_TCP_SERVER
|
|
|
|
|
|
2019-04-12 10:18:57 +08:00
|
|
|
|
#define DBG_TAG "TCP"
|
2018-11-05 14:34:40 +08:00
|
|
|
|
#ifdef DEBUG_TCP_SERVER
|
2019-04-12 10:18:57 +08:00
|
|
|
|
#define DBG_LVL DBG_LOG
|
2018-11-05 14:34:40 +08:00
|
|
|
|
#else
|
2019-04-12 10:18:57 +08:00
|
|
|
|
#define DBG_LVL DBG_INFO /* DBG_ERROR */
|
2018-11-05 14:34:40 +08:00
|
|
|
|
#endif
|
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
|
2018-02-06 18:59:24 +08:00
|
|
|
|
#define BUFSZ (1024)
|
|
|
|
|
|
2018-11-05 14:34:40 +08:00
|
|
|
|
static int started = 0;
|
|
|
|
|
static int is_running = 0;
|
|
|
|
|
static int port = 5000;
|
2013-01-08 21:05:02 +08:00
|
|
|
|
static const char send_data[] = "This is TCP Server from RT-Thread."; /* 发送用到的数据 */
|
2018-11-05 14:34:40 +08:00
|
|
|
|
|
|
|
|
|
static void tcpserv(void *arg)
|
2013-01-08 21:05:02 +08:00
|
|
|
|
{
|
2018-11-05 14:34:40 +08:00
|
|
|
|
int ret;
|
2018-02-06 18:59:24 +08:00
|
|
|
|
char *recv_data; /* 用于接收的指针,后面会做一次动态分配以请求可用内存 */
|
|
|
|
|
int sock, connected, bytes_received;
|
|
|
|
|
struct sockaddr_in server_addr, client_addr;
|
2018-11-05 14:34:40 +08:00
|
|
|
|
|
|
|
|
|
struct timeval timeout;
|
|
|
|
|
fd_set readset, readset_c;
|
|
|
|
|
socklen_t sin_size = sizeof(struct sockaddr_in);
|
2018-02-06 18:59:24 +08:00
|
|
|
|
|
|
|
|
|
recv_data = rt_malloc(BUFSZ + 1); /* 分配接收用的数据缓冲 */
|
|
|
|
|
if (recv_data == RT_NULL)
|
|
|
|
|
{
|
2018-11-05 14:34:40 +08:00
|
|
|
|
LOG_E("No memory");
|
2018-02-06 18:59:24 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 一个socket在使用前,需要预先创建出来,指定SOCK_STREAM为TCP的socket */
|
2018-11-05 14:34:40 +08:00
|
|
|
|
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
|
2018-02-06 18:59:24 +08:00
|
|
|
|
{
|
2018-11-05 14:34:40 +08:00
|
|
|
|
LOG_E("Create socket error");
|
|
|
|
|
goto __exit;
|
2018-02-06 18:59:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 初始化服务端地址 */
|
|
|
|
|
server_addr.sin_family = AF_INET;
|
2018-11-05 14:34:40 +08:00
|
|
|
|
server_addr.sin_port = htons(port); /* 服务端工作的端口 */
|
2018-02-06 18:59:24 +08:00
|
|
|
|
server_addr.sin_addr.s_addr = INADDR_ANY;
|
2018-11-05 14:34:40 +08:00
|
|
|
|
rt_memset(&(server_addr.sin_zero), 0x0, sizeof(server_addr.sin_zero));
|
2018-02-06 18:59:24 +08:00
|
|
|
|
|
|
|
|
|
/* 绑定socket到服务端地址 */
|
|
|
|
|
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
|
|
|
|
|
{
|
2018-11-05 14:34:40 +08:00
|
|
|
|
LOG_E("Unable to bind");
|
|
|
|
|
goto __exit;
|
2018-02-06 18:59:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 在socket上进行监听 */
|
2018-11-05 14:34:40 +08:00
|
|
|
|
if (listen(sock, 10) == -1)
|
2018-02-06 18:59:24 +08:00
|
|
|
|
{
|
2018-11-05 14:34:40 +08:00
|
|
|
|
LOG_E("Listen error");
|
|
|
|
|
goto __exit;
|
2018-02-06 18:59:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 14:34:40 +08:00
|
|
|
|
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)
|
2018-02-06 18:59:24 +08:00
|
|
|
|
{
|
2018-11-05 14:34:40 +08:00
|
|
|
|
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;
|
2018-02-06 18:59:24 +08:00
|
|
|
|
|
|
|
|
|
/* 接受一个客户端连接socket的请求,这个函数调用是阻塞式的 */
|
|
|
|
|
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
|
|
|
|
|
/* 返回的是连接成功的socket */
|
|
|
|
|
if (connected < 0)
|
|
|
|
|
{
|
2018-11-05 14:34:40 +08:00
|
|
|
|
LOG_E("accept connection failed! errno = %d", errno);
|
2018-02-06 18:59:24 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 接受返回的client_addr指向了客户端的地址信息 */
|
2018-11-05 14:34:40 +08:00
|
|
|
|
LOG_I("I got a connection from (%s , %d)\n",
|
2018-02-06 18:59:24 +08:00
|
|
|
|
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
|
|
|
|
|
|
|
|
|
|
/* 客户端连接的处理 */
|
2018-11-05 14:34:40 +08:00
|
|
|
|
while (is_running)
|
2018-02-06 18:59:24 +08:00
|
|
|
|
{
|
2018-11-05 14:34:40 +08:00
|
|
|
|
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;
|
2018-02-06 18:59:24 +08:00
|
|
|
|
|
|
|
|
|
/* 从connected socket中接收数据,接收buffer是1024大小,但并不一定能够收到1024大小的数据 */
|
|
|
|
|
bytes_received = recv(connected, recv_data, BUFSZ, 0);
|
|
|
|
|
if (bytes_received < 0)
|
|
|
|
|
{
|
2018-11-05 14:34:40 +08:00
|
|
|
|
LOG_E("Received error, close the connect.");
|
2018-02-06 18:59:24 +08:00
|
|
|
|
closesocket(connected);
|
2018-11-05 14:34:40 +08:00
|
|
|
|
connected = -1;
|
2018-02-06 18:59:24 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else if (bytes_received == 0)
|
|
|
|
|
{
|
|
|
|
|
/* 打印recv函数返回值为0的警告信息 */
|
2018-11-05 14:34:40 +08:00
|
|
|
|
LOG_W("Received warning, recv function return 0.");
|
|
|
|
|
continue;
|
2018-02-06 18:59:24 +08:00
|
|
|
|
}
|
2018-11-05 14:34:40 +08:00
|
|
|
|
else
|
2018-02-06 18:59:24 +08:00
|
|
|
|
{
|
2018-11-05 14:34:40 +08:00
|
|
|
|
/* 有接收到数据,把末端清零 */
|
|
|
|
|
recv_data[bytes_received] = '\0';
|
|
|
|
|
if (strcmp(recv_data, "q") == 0 || strcmp(recv_data, "Q") == 0)
|
|
|
|
|
{
|
|
|
|
|
/* 如果是首字母是q或Q,关闭这个连接 */
|
|
|
|
|
LOG_I("Got a 'q' or 'Q', close the connect.");
|
|
|
|
|
closesocket(connected);
|
|
|
|
|
connected = -1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp(recv_data, "exit") == 0)
|
|
|
|
|
{
|
|
|
|
|
/* 如果接收的是exit,则关闭整个服务端 */
|
|
|
|
|
closesocket(connected);
|
|
|
|
|
connected = -1;
|
|
|
|
|
goto __exit;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* 在控制终端显示收到的数据 */
|
|
|
|
|
LOG_D("Received data = %s", recv_data);
|
|
|
|
|
}
|
2018-02-06 18:59:24 +08:00
|
|
|
|
}
|
2018-11-05 14:34:40 +08:00
|
|
|
|
|
|
|
|
|
/* 发送数据到connected socket */
|
|
|
|
|
ret = send(connected, send_data, rt_strlen(send_data), 0);
|
|
|
|
|
if (ret < 0)
|
2018-02-06 18:59:24 +08:00
|
|
|
|
{
|
2018-11-05 14:34:40 +08:00
|
|
|
|
LOG_E("send error, close the connect.");
|
2018-02-06 18:59:24 +08:00
|
|
|
|
closesocket(connected);
|
2018-11-05 14:34:40 +08:00
|
|
|
|
connected = -1;
|
2018-02-06 18:59:24 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2018-11-05 14:34:40 +08:00
|
|
|
|
else if (ret == 0)
|
2018-02-06 18:59:24 +08:00
|
|
|
|
{
|
2018-11-05 14:34:40 +08:00
|
|
|
|
/* 打印send函数返回值为0的警告信息 */
|
|
|
|
|
LOG_W("Send warning, send function return 0.");
|
2018-02-06 18:59:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 14:34:40 +08:00
|
|
|
|
__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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
|
{
|
2018-11-06 09:45:31 +08:00
|
|
|
|
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");
|
2018-11-05 14:34:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2020-06-13 11:01:24 +08:00
|
|
|
|
LOG_I("The tcpserver has started!");
|
|
|
|
|
LOG_I("Please stop tcpserver firstly, by: tcpserver --stop");
|
2018-11-05 14:34:40 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
port = atoi(argv[2]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
goto __usage;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-06 18:59:24 +08:00
|
|
|
|
|
2018-11-05 14:34:40 +08:00
|
|
|
|
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;
|
2018-02-06 18:59:24 +08:00
|
|
|
|
|
2018-11-05 14:34:40 +08:00
|
|
|
|
__usage:
|
|
|
|
|
usage();
|
2013-01-08 21:05:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef RT_USING_FINSH
|
2018-11-05 14:34:40 +08:00
|
|
|
|
MSH_CMD_EXPORT_ALIAS(tcpserver_test, tcpserver,
|
|
|
|
|
Start a tcp server. Help: tcpserver --help);
|
2013-01-08 21:05:02 +08:00
|
|
|
|
#endif
|