From feec9197c41314931cd1978bb5e61e974e6deaba Mon Sep 17 00:00:00 2001 From: "bernard.xiong" Date: Mon, 1 Mar 2010 09:39:28 +0000 Subject: [PATCH] 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 --- examples/network/tcpclient.c | 81 ++++++++++++++++++++++++++ examples/network/tcpserver.c | 109 +++++++++++++++++++++++++++++++++++ examples/network/udpclient.c | 42 ++++++++++++++ examples/network/udpserver.c | 74 ++++++++++++++++++++++++ 4 files changed, 306 insertions(+) create mode 100644 examples/network/tcpclient.c create mode 100644 examples/network/tcpserver.c create mode 100644 examples/network/udpclient.c create mode 100644 examples/network/udpserver.c diff --git a/examples/network/tcpclient.c b/examples/network/tcpclient.c new file mode 100644 index 0000000000..559a108d46 --- /dev/null +++ b/examples/network/tcpclient.c @@ -0,0 +1,81 @@ +#include +#include +#include + +static const char send_data[] = "This is TCP Client from RT-Thread."; +void tcpclient(const char* url, int port) +{ + char *recv_data; + struct hostent *host; + int sock, bytes_received; + struct sockaddr_in server_addr; + + host = gethostbyname(url); + + recv_data = rt_malloc(1024); + if (recv_data == RT_NULL) + { + rt_kprintf("No memory\n"); + return; + } + + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) + { + rt_kprintf("Socket error\n"); + + /* release recv buffer */ + rt_free(recv_data); + 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)); + + if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) + { + rt_kprintf("Connect error\n"); + + /* release recv buffer */ + rt_free(recv_data); + return; + } + + while(1) + { + bytes_received = recv(sock, recv_data, 1024, 0); + if (bytes_received < 0) + { + lwip_close(sock); + + /* release recv buffer */ + rt_free(recv_data); + break; + } + + recv_data[bytes_received] = '\0'; + + if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0) + { + lwip_close(sock); + + /* release recv buffer */ + rt_free(recv_data); + break; + } + else + { + rt_kprintf("\nRecieved data = %s " , recv_data); + } + + send(sock,send_data,strlen(send_data), 0); + } + + return; +} + +#ifdef RT_USING_FINSH +#include +FINSH_FUNCTION_EXPORT(tcpclient, startup tcp client); +#endif diff --git a/examples/network/tcpserver.c b/examples/network/tcpserver.c new file mode 100644 index 0000000000..606e842c8d --- /dev/null +++ b/examples/network/tcpserver.c @@ -0,0 +1,109 @@ +#include +#include + +static const char send_data[] = "This is TCP Server from RT-Thread."; +void tcpserv(void* parameter) +{ + char *recv_data; + rt_uint32_t sin_size; + int sock, connected, bytes_received; + struct sockaddr_in server_addr, client_addr; + rt_bool_t stop = RT_FALSE; + + recv_data = rt_malloc(1024); + if (recv_data == RT_NULL) + { + rt_kprintf("No memory\n"); + return; + } + + /* create a socket */ + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) + { + rt_kprintf("Socket error\n"); + + /* release recv buffer */ + rt_free(recv_data); + return; + } + + /* init server address */ + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(5000); + server_addr.sin_addr.s_addr = INADDR_ANY; + rt_memset(&(server_addr.sin_zero),8, sizeof(server_addr.sin_zero)); + + /* bind socket */ + if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) + { + rt_kprintf("Unable to bind\n"); + + /* release recv buffer */ + rt_free(recv_data); + return; + } + + /* listen socket */ + if (listen(sock, 5) == -1) + { + rt_kprintf("Listen error\n"); + + /* release recv buffer */ + rt_free(recv_data); + return; + } + + rt_kprintf("\nTCPServer Waiting for client on port 5000...\n"); + while(stop != RT_TRUE) + { + sin_size = sizeof(struct sockaddr_in); + + /* accept a client socket */ + connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size); + + rt_kprintf("I got a connection from (%s , %d)\n", + inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port)); + + while (1) + { + send(connected, send_data, strlen(send_data), 0); + + bytes_received = recv(connected,recv_data, 1024, 0); + if (bytes_received < 0) + { + /* close socket */ + lwip_close(connected); + break; + } + + recv_data[bytes_received] = '\0'; + if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0) + { + lwip_close(connected); + break; + } + else if (strcmp(recv_data, "exit") == 0) + { + lwip_close(connected); + stop = RT_TRUE; + break; + } + else + { + rt_kprintf("RECIEVED DATA = %s \n" , recv_data); + } + } + } + + lwip_close(sock); + + /* release recv buffer */ + rt_free(recv_data); + + return ; +} + +#ifdef RT_USING_FINSH +#include +FINSH_FUNCTION_EXPORT(tcpserv, startup tcp server); +#endif 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 +#include +#include + +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_FUNCTION_EXPORT(udpclient, startup udp client); +#endif diff --git a/examples/network/udpserver.c b/examples/network/udpserver.c new file mode 100644 index 0000000000..9fede482fb --- /dev/null +++ b/examples/network/udpserver.c @@ -0,0 +1,74 @@ +#include +#include + +void udpserv(void* paramemter) +{ + int sock; + int bytes_read; + char *recv_data; + rt_uint32_t addr_len; + struct sockaddr_in server_addr , client_addr; + + recv_data = rt_malloc(1024); + if (recv_data == RT_NULL) + { + rt_kprintf("No memory\n"); + return; + } + + /* create socket */ + if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) + { + rt_kprintf("Socket error\n"); + + /* release recv buffer */ + rt_free(recv_data); + return; + } + + /* init server socket address */ + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(5000); + server_addr.sin_addr.s_addr = INADDR_ANY; + rt_memset(&(server_addr.sin_zero),0, sizeof(server_addr.sin_zero)); + + if (bind(sock,(struct sockaddr *)&server_addr, + sizeof(struct sockaddr)) == -1) + { + rt_kprintf("Bind error\n"); + + /* release recv buffer */ + rt_free(recv_data); + return; + } + + addr_len = sizeof(struct sockaddr); + + rt_kprintf("UDPServer Waiting for client on port 5000...\n"); + + while (1) + { + bytes_read = recvfrom(sock, recv_data, 1024, 0, + (struct sockaddr *)&client_addr, &addr_len); + recv_data[bytes_read] = '\0'; + rt_kprintf("\n(%s , %d) said : ",inet_ntoa(client_addr.sin_addr), + ntohs(client_addr.sin_port)); + rt_kprintf("%s", recv_data); + + if (strcmp(recv_data, "exit") == 0) + { + lwip_close(sock); + + /* release recv buffer */ + rt_free(recv_data); + break; + } + } + + return; +} + +#ifdef RT_USING_FINSH +#include +FINSH_FUNCTION_EXPORT(udpserv, startup udp server); +#endif