diff options
Diffstat (limited to 'examples/network/udpclient.c')
| -rw-r--r-- | examples/network/udpclient.c | 135 |
1 files changed, 123 insertions, 12 deletions
diff --git a/examples/network/udpclient.c b/examples/network/udpclient.c index 5060c9a087..80cf521adf 100644 --- a/examples/network/udpclient.c +++ b/examples/network/udpclient.c @@ -1,11 +1,28 @@ #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 "netdb.h" +#define DEBUG_UDP_CLIENT + +#define DBG_ENABLE +#define DBG_SECTION_NAME "UDP" +#ifdef DEBUG_UDP_CLIENT +#define DBG_LEVEL DBG_LOG +#else +#define DBG_LEVEL DBG_INFO /* DBG_ERROR */ +#endif +#define DBG_COLOR +#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"; /* 发送用到的数据 */ -void udpclient(const char *url, int port, int count) + +static void udpclient(void *arg) { int sock; struct hostent *host; @@ -13,11 +30,16 @@ void udpclient(const char *url, int port, int count) /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */ host = (struct hostent *) gethostbyname(url); + if (host == RT_NULL) + { + LOG_E("Get host by name failed!"); + return; + } /* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */ - if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) + if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { - rt_kprintf("Socket error\n"); + LOG_E("Create socket error"); return; } @@ -27,26 +49,115 @@ void udpclient(const char *url, int port, int count) 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; + /* 总计发送count次数据 */ - while (count) + while (count && is_running) { /* 发送数据到服务远端 */ - sendto(sock, send_data, strlen(send_data), 0, + sendto(sock, send_data, rt_strlen(send_data), 0, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); /* 线程休眠一段时间 */ - rt_thread_delay(50); + rt_thread_mdelay(1000); /* 计数值减一 */ count --; } + if (count == 0) + { + LOG_I("UDP client send data finished!"); + } + /* 关闭这个socket */ - closesocket(sock); + if (sock >= 0) + { + closesocket(sock); + sock = -1; + } + started = 0; + is_running = 0; +} + +static void usage(void) +{ + LOG_I("Usage: udpclient -h <host> -p <port> [--cnt] [count]"); + LOG_I(" udpclient --stop"); + LOG_I(" udpclient --help"); + LOG_I(""); + LOG_I("Miscellaneous:"); + LOG_I(" -h Specify host address"); + LOG_I(" -p Specify the host port number"); + LOG_I(" --cnt Specify the send data count"); + LOG_I(" --stop Stop tcpclient program"); + LOG_I(" --help Print help information"); +} + +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 tcpclient has started!"); + LOG_I("Please stop tcpclient firstly, by: tcpclient --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 -#include <finsh.h> -/* 输出udpclient函数到finsh shell中 */ -FINSH_FUNCTION_EXPORT(udpclient, startup udp client); +MSH_CMD_EXPORT_ALIAS(udpclient_test, udpclient, + Start a udp client. Help: udpclient --help); #endif |
