1 #include <rtthread.h> 2 3 #include <sys/socket.h> /* 使用BSD socket,需要包含sockets.h头文件 */ 4 #include "netdb.h" 5 6 #define DEBUG_UDP_CLIENT 7 8 #define DBG_ENABLE 9 #define DBG_SECTION_NAME "UDP" 10 #ifdef DEBUG_UDP_CLIENT 11 #define DBG_LEVEL DBG_LOG 12 #else 13 #define DBG_LEVEL DBG_INFO /* DBG_ERROR */ 14 #endif 15 #define DBG_COLOR 16 #include <rtdbg.h> 17 18 static int started = 0; 19 static int is_running = 0; 20 static char url[256]; 21 static int port = 8080; 22 static int count = 10; 23 const char send_data[] = "This is UDP Client from RT-Thread.\n"; /* 发送用到的数据 */ 24 25 static void udpclient(void *arg) 26 { 27 int sock; 28 struct hostent *host; 29 struct sockaddr_in server_addr; 30 31 /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */ 32 host = (struct hostent *) gethostbyname(url); 33 if (host == RT_NULL) 34 { 35 LOG_E("Get host by name failed!"); 36 return; 37 } 38 39 /* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */ 40 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) 41 { 42 LOG_E("Create socket error"); 43 return; 44 } 45 46 /* 初始化预连接的服务端地址 */ 47 server_addr.sin_family = AF_INET; 48 server_addr.sin_port = htons(port); 49 server_addr.sin_addr = *((struct in_addr *)host->h_addr); 50 rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero)); 51 52 started = 1; 53 is_running = 1; 54 55 /* 总计发送count次数据 */ 56 while (count && is_running) 57 { 58 /* 发送数据到服务远端 */ 59 sendto(sock, send_data, rt_strlen(send_data), 0, 60 (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); 61 62 /* 线程休眠一段时间 */ 63 rt_thread_mdelay(1000); 64 65 /* 计数值减一 */ 66 count --; 67 } 68 69 if (count == 0) 70 { 71 LOG_I("UDP client send data finished!"); 72 } 73 74 /* 关闭这个socket */ 75 if (sock >= 0) 76 { 77 closesocket(sock); 78 sock = -1; 79 } 80 started = 0; 81 is_running = 0; 82 } 83 84 static void usage(void) 85 { 86 rt_kprintf("Usage: udpclient -h <host> -p <port> [--cnt] [count]\n"); 87 rt_kprintf(" udpclient --stop\n"); 88 rt_kprintf(" udpclient --help\n"); 89 rt_kprintf("\n"); 90 rt_kprintf("Miscellaneous:\n"); 91 rt_kprintf(" -h Specify host address\n"); 92 rt_kprintf(" -p Specify the host port number\n"); 93 rt_kprintf(" --cnt Specify the send data count\n"); 94 rt_kprintf(" --stop Stop tcpclient program\n"); 95 rt_kprintf(" --help Print help information\n"); 96 } 97 98 static void udpclient_test(int argc, char** argv) 99 { 100 rt_thread_t tid; 101 102 if (argc == 1 || argc > 7) 103 { 104 LOG_I("Please check the command you entered!\n"); 105 goto __usage; 106 } 107 else 108 { 109 if (rt_strcmp(argv[1], "--help") == 0) 110 { 111 goto __usage; 112 } 113 else if (rt_strcmp(argv[1], "--stop") == 0) 114 { 115 is_running = 0; 116 return; 117 } 118 else if (rt_strcmp(argv[1], "-h") == 0 && rt_strcmp(argv[3], "-p") == 0) 119 { 120 if (started) 121 { 122 LOG_I("The tcpclient has started!"); 123 LOG_I("Please stop tcpclient firstly, by: tcpclient --stop"); 124 return; 125 } 126 127 if (argc == 7 && rt_strcmp(argv[6], "--cnt") == 0) 128 { 129 count = atoi(argv[7]); 130 } 131 132 if (rt_strlen(argv[2]) > sizeof(url)) 133 { 134 LOG_E("The input url is too long, max %d bytes!", sizeof(url)); 135 return; 136 } 137 rt_memset(url, 0x0, sizeof(url)); 138 rt_strncpy(url, argv[2], rt_strlen(argv[2])); 139 port = atoi(argv[4]); 140 } 141 else 142 { 143 goto __usage; 144 } 145 } 146 147 tid = rt_thread_create("udp_client", 148 udpclient, RT_NULL, 149 2048, RT_THREAD_PRIORITY_MAX/3, 20); 150 if (tid != RT_NULL) 151 { 152 rt_thread_startup(tid); 153 } 154 return; 155 156 __usage: 157 usage(); 158 } 159 160 #ifdef RT_USING_FINSH 161 MSH_CMD_EXPORT_ALIAS(udpclient_test, udpclient, 162 Start a udp client. Help: udpclient --help); 163 #endif 164