udp 客户端
更多...
udp 客户端
源码
#include <rtthread.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <finsh.h>
const char send_data[] = "This is UDP Client from RT-Thread.\n";
void udpclient(int argc, char **argv)
{
int sock, port, count;
const char *url;
if (argc < 3)
{
rt_kprintf(
"Usage: udpclient URL PORT [COUNT = 10]\n");
rt_kprintf(
"Like: tcpclient 192.168.12.44 5000\n");
return ;
}
url = argv[1];
port = strtoul(argv[2], 0, 10);
if (argc > 3)
count = strtoul(argv[3], 0, 10);
else
count = 10;
if ((sock =
socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
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));
while (count)
{
sendto(sock, send_data, strlen(send_data), 0,
count --;
}
}