-
Notifications
You must be signed in to change notification settings - Fork 5
/
tcpserver_sample.c
64 lines (56 loc) · 1.38 KB
/
tcpserver_sample.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-07-11 flybreak the first version
*/
#include "tcpserver.h"
#include <string.h>
#define DBG_TAG "tcpserv"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
static struct tcpserver *serv;
static rt_uint8_t buf[1024];
static void tcpserver_event_notify(tcpclient_t client, rt_uint8_t event)
{
int ret;
switch (event)
{
case TCPSERVER_EVENT_CONNECT:
LOG_D("client connect:%d", client->sock);
break;
case TCPSERVER_EVENT_RECV:
ret = tcpserver_recv(client, buf, 1024, -1);
if (ret > 0)
{
ret = tcpserver_send(client, buf, ret, 0);
}
break;
case TCPSERVER_EVENT_DISCONNECT:
LOG_D("client disconnect:%d", client->sock);
break;
default:
break;
}
}
static int tcpserver(int argc, char **argv)
{
if (argc != 3)
{
rt_kprintf("Usage:./select_server [ip] [port]\n");
return -1;
}
serv = tcpserver_create(argv[1], atoi(argv[2]));
tcpserver_set_notify_callback(serv, tcpserver_event_notify);
return 0;
}
MSH_CMD_EXPORT(tcpserver, server start)
static int tcpserver_stop(int argc, char **argv)
{
tcpserver_destroy(serv);
return 0;
}
MSH_CMD_EXPORT(tcpserver_stop, tcpserver stop)