mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2025-12-26 09:08:25 +00:00
move TCP client/server and UDP client/server example to this directory.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@457 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
42
examples/network/udpclient.c
Normal file
42
examples/network/udpclient.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <rtthread.h>
|
||||
#include <lwip/netdb.h>
|
||||
#include <lwip/sockets.h>
|
||||
|
||||
const char send_data[] = "This is UDP Client from RT-Thread.\n";
|
||||
void udpclient(const char* url, int port, int count)
|
||||
{
|
||||
int sock;
|
||||
struct hostent *host;
|
||||
struct sockaddr_in server_addr;
|
||||
|
||||
host= (struct hostent *) gethostbyname(url);
|
||||
|
||||
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
|
||||
{
|
||||
rt_kprintf("Socket error\n");
|
||||
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,
|
||||
(struct sockaddr *)&server_addr, sizeof(struct sockaddr));
|
||||
|
||||
rt_thread_delay(50);
|
||||
|
||||
count --;
|
||||
}
|
||||
|
||||
/* close socket */
|
||||
lwip_close(sock);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
FINSH_FUNCTION_EXPORT(udpclient, startup udp client);
|
||||
#endif
|
||||
Reference in New Issue
Block a user