udpclient.c 4.1 KB
Newer Older
B
Bernard Xiong 已提交
1
#include <rtthread.h>
2

3 4
#include <sys/socket.h> /* 使用BSD socket,需要包含sockets.h头文件 */
#include "netdb.h"
B
Bernard Xiong 已提交
5

6 7
#define DEBUG_UDP_CLIENT

8
#define DBG_TAG               "UDP"
9
#ifdef DEBUG_UDP_CLIENT
10
#define DBG_LVL               DBG_LOG
11
#else
12
#define DBG_LVL               DBG_INFO /* DBG_ERROR */
13 14 15 16 17 18 19 20
#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;
B
Bernard Xiong 已提交
21
const char send_data[] = "This is UDP Client from RT-Thread.\n"; /* 发送用到的数据 */
22 23

static void udpclient(void *arg)
B
Bernard Xiong 已提交
24
{
wuyangyong's avatar
wuyangyong 已提交
25 26 27 28 29 30
    int sock;
    struct hostent *host;
    struct sockaddr_in server_addr;

    /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
    host = (struct hostent *) gethostbyname(url);
31 32 33 34 35
    if (host == RT_NULL)
    {
        LOG_E("Get host by name failed!");
        return;
    }
wuyangyong's avatar
wuyangyong 已提交
36 37

    /* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */
38
    if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
wuyangyong's avatar
wuyangyong 已提交
39
    {
40
        LOG_E("Create socket error");
wuyangyong's avatar
wuyangyong 已提交
41 42 43 44 45 46 47 48 49
        return;
    }

    /* 初始化预连接的服务端地址 */
    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));

50 51 52
    started = 1;
    is_running = 1;

wuyangyong's avatar
wuyangyong 已提交
53
    /* 总计发送count次数据 */
54
    while (count && is_running)
wuyangyong's avatar
wuyangyong 已提交
55 56
    {
        /* 发送数据到服务远端 */
57
        sendto(sock, send_data, rt_strlen(send_data), 0,
wuyangyong's avatar
wuyangyong 已提交
58 59 60
               (struct sockaddr *)&server_addr, sizeof(struct sockaddr));

        /* 线程休眠一段时间 */
61
        rt_thread_mdelay(1000);
wuyangyong's avatar
wuyangyong 已提交
62 63 64 65 66

        /* 计数值减一 */
        count --;
    }

67 68 69 70 71
    if (count == 0)
    {
        LOG_I("UDP client send data finished!");
    }

wuyangyong's avatar
wuyangyong 已提交
72
    /* 关闭这个socket */
73 74 75 76 77 78 79 80 81 82 83
    if (sock >= 0)
    {
        closesocket(sock);
        sock = -1;
    }
    started = 0;
    is_running = 0;
}

static void usage(void)
{
84 85 86 87 88 89 90 91 92 93
    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 tcpclient program\n");
    rt_kprintf("  --help       Print help information\n");
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
}

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();
B
Bernard Xiong 已提交
156 157 158
}

#ifdef RT_USING_FINSH
159 160
MSH_CMD_EXPORT_ALIAS(udpclient_test, udpclient,
    Start a udp client. Help: udpclient --help);
B
Bernard Xiong 已提交
161
#endif