diff options
| author | bernard.xiong <[email protected]> | 2010-03-01 09:39:28 +0000 |
|---|---|---|
| committer | bernard.xiong <[email protected]> | 2010-03-01 09:39:28 +0000 |
| commit | feec9197c41314931cd1978bb5e61e974e6deaba (patch) | |
| tree | 6b1b5abd87f66f26283a20f29250e6f97ba132ea /examples/network/udpclient.c | |
| parent | 6ccd04543c7928151d9b4fb782470e7d8f3e1e14 (diff) | |
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
Diffstat (limited to 'examples/network/udpclient.c')
| -rw-r--r-- | examples/network/udpclient.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/examples/network/udpclient.c b/examples/network/udpclient.c new file mode 100644 index 0000000000..5634627cdb --- /dev/null +++ b/examples/network/udpclient.c @@ -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
|
