提交 6d7e6d8f 编写于 作者: B bernard.xiong

remove tcp and udp example and change the maximal connection of chargen to 32.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@564 bbd45198-f89e-11dd-88c7-29a3b14d5316
上级 1c2f8235
#include <rtthread.h>
#include "lwip/sockets.h"
#define MAX_SERV 5 /* Maximum number of chargen services. Don't need too many */
#define MAX_SERV 32 /* Maximum number of chargen services. Don't need too many */
#define CHARGEN_THREAD_NAME "chargen"
#if RT_THREAD_PRIORITY_MAX == 32
#define CHARGEN_PRIORITY 20 /* Really low priority */
#else
#define CHARGEN_PRIORITY 200 /* Really low priority */
#endif
#define CHARGEN_THREAD_STACKSIZE 1024
struct charcb
{
......@@ -202,3 +206,11 @@ void chargen_init(void)
CHARGEN_PRIORITY, 5);
if (chargen != RT_NULL) rt_thread_startup(chargen);
}
#ifdef RT_USING_FINSH
#include <finsh.h>
void chargen()
{
chargen_init();
}
FINSH_FUNCTION_EXPORT(chargen, start chargen server);
#endif
#include <rtthread.h>
#include <lwip/netdb.h>
#include <lwip/sockets.h>
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.h>
FINSH_FUNCTION_EXPORT(tcpclient, startup tcp client);
#endif
#include <rtthread.h>
#include <lwip/sockets.h>
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.h>
FINSH_FUNCTION_EXPORT(tcpserv, startup tcp server);
#endif
#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
#include <rtthread.h>
#include <lwip/sockets.h>
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.h>
FINSH_FUNCTION_EXPORT(udpserv, startup udp server);
#endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册