📄 tcpserv.c
字号:
/***********************************************************
* MODULE: Init.c
*
* PURPOSE:
* This file is the test for network application.
* you can run it with the help.
*
* COPYRIGHT (C) 2000-2001, CoreTek Systems Inc.
* ALL RIGHTS RESERVED
***********************************************************
* AUTHOR(S):
*
* DATE CREATED: 2001
*
* MODIFICATIONS:
* Date userName Description
* 2001.2.20 Wang lijie
* 2001.4.10 ChengShuai
* 2001.10.16 ChenTie
***********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "usr_config.h"
#include <irq.h>
#include "sys/types.h"
#include "DnetStat.h"
#include "DnetBase.h"
#include "DnetSock.h"
#include "osaware.h"
#define DEMO_TASKSLEEP(t) delta_task_wake_after(t)
#define DEMO_DEBUG printf
#define DEMO_CLOCKGET(t) delta_clock_get(DELTA_CLOCK_GET_TICKS_SINCE_BOOT,(void *)t);
#ifdef DELTA_NET
#if ( defined(Dnet_USE_PPP) && !defined(Dnet_USE_ETHER) )
#define conf_DIALUPCONNECTMODE (unsigned char *)"DialUp connect mode: ALWAYS" /*always dialup*/
#elif ( !defined(Dnet_USE_PPP) && defined(Dnet_USE_ETHER) )
#define conf_DIALUPCONNECTMODE (unsigned char *)"DialUp connect mode: NEVER" /*never dialup*/
#define conf_NetPlinkTaskPRI (unsigned char *)"network ppp link task priority: 6"
#elif ( defined(Dnet_USE_PPP) && defined(Dnet_USE_ETHER) )
#define conf_DIALUPCONNECTMODE (unsigned char *)"DialUp connect mode: AUTO" /*dialup when ethernet is invalid*/
#endif
#define MAXPARMS 40
struct dnet_confparam user_parms[MAXPARMS] = {
{conf_NETDEBUG},
#ifdef Dnet_USE_ETHER
{conf_IPADDR}, /* Local IP address */
{conf_SUBMASK}, /* Subnetwork mask */
{conf_GATEWAY}, /* Gateway address */
{conf_RARPCLN}, /*RARP */
#endif/*fDnet_USE_ETHER*/
#ifdef Dnet_USE_PPP
{conf_CONSOLELOG}, /* Log Modem & PPP events to console */
{conf_KEEPALIVE}, /* seconds between PPP echos, 0=disable */
{conf_CLIENTTMO}, /* timeout for connects as client */
{conf_REQUESTVJ}, /* request that the other side do VJ compression */
{conf_REQUIREPAP}, /* PAP support */
{conf_DIALPHONE}, /* phone number to dial*/
{conf_MODEMINIT}, /* initialization "AT" command */
{conf_USERNAME}, /* user name */
{conf_PASSWORD}, /* user password */
{conf_LINETMO}, /* idle modem line timeout, 0=infinite */
{conf_LINEPROTOCOL}, /* LineProtocol:1=PPP, 2=SLIP */
#endif/*Dnet_USE_PPP*/
{conf_DHCPCLN}, /* DHCP */
{conf_DNSCLN}, /* DNS client */
{conf_DNSSVR1}, /* DNS server */
{conf_DNSSVR2},
/*{conf_DNSSVR3},*/
#ifdef Dnet_SNMP_AGENT
{conf_SNMPGET},
{conf_SNMPSET},
{conf_SNMPSYSCON},
{conf_SNMPSYSNAME},
{conf_SNMPSYSLOC},
{conf_SNMPTRAPTARGET},
{conf_SNMPTRAPCOMM},
#endif/*Dnet_SNMP_AGENT*/
{conf_PINGAPP},
{conf_TELENTCLN},
{conf_NetRcvTaskPRI},
{conf_NetTimrTaskPRI},
{conf_NetPlinkTaskPRI},
{conf_NetMaxSocks},
{conf_END},
};
#endif/*DELTA_NET*/
#ifdef Dnet_USE_ETHER
extern int Ethdrv_Prepare(void);
#endif
#ifdef Dnet_USE_PPP
extern int Uartdrv_Prepare(void);
#endif
#define MAXSENDTIMES 10
#define MAXDATALEN 1460//
#define TCPSERV_ECHOPORT 5000
static SOCKET tsMSock = INVALID_SOCKET; /* master server socket */
static fd_set tsAfds; /*active file descriptor set*/
static char tsInBuf[MAXDATALEN];
/***********************************************************
* MODULE: Ts_Init
*
* PURPOSE:
* Initialization of tcp server (socket)
*
* PARAMETERS
* Input: none
* Output: none
* Return value: int
*
***********************************************************/
static int Ts_Init()
{
int e;
struct sockaddr_in me;
int reuse = 1;
DEMO_DEBUG("tcp server -- starting.\r\n");
tsMSock = socket(AF_INET, SOCK_STREAM, 0);
if(tsMSock == INVALID_SOCKET)
{
DEMO_DEBUG("tcp server -- socket create failed: %d\r\n", INVALID_SOCKET);
return INVALID_SOCKET;
}
e = setsockopt(tsMSock,SOL_SOCKET,SO_REUSEADDR,(opt_type *)&reuse,sizeof(int));
if(e != 0)
{
e = Dnet_ErrnoGet(tsMSock);
DEMO_DEBUG("tcp server -- set socket option SO_REUSEADDR failed: %d\r\n", e);
closesocket(tsMSock);
return e;
}
me.sin_family = AF_INET;
me.sin_addr.s_addr = INADDR_ANY;
me.sin_port = htons(TCPSERV_ECHOPORT);
e = bind(tsMSock, (struct sockaddr*)&me,sizeof(struct sockaddr));
if(e != 0)
{
e = Dnet_ErrnoGet(tsMSock);
DEMO_DEBUG("tcp server -- bind failed: %d\r\n", e);
closesocket(tsMSock);
return e;
}
e = listen(tsMSock, 3);
if(e != 0)
{
e = Dnet_ErrnoGet(tsMSock);
DEMO_DEBUG("tcp server -- listen failed %d\r\n", e);
closesocket(tsMSock);
return e;
}
FD_ZERO(&tsAfds);
FD_SET(tsMSock,&tsAfds);
return 0;
}
/***********************************************************
* MODULE: Ts_Close
*
* PURPOSE:
* Close the socket when tcp server want to close
*
* PARAMETERS
* Input: none
* Output: none
* Return value: int
*
***********************************************************/
static int Ts_Close()
{
int e = 0; /* scratch error holder */
unsigned i;
DEMO_DEBUG("tcp server -- closing.\r\n");
for(i=0;i<tsAfds.fd_count;++i)
{
e = closesocket(tsAfds.fd_array[i]);
if(e)
{
e = Dnet_ErrnoGet(tsAfds.fd_array[i]);
DEMO_DEBUG("tcp server -- close all socket failed: %d\r\n", e);
}
FD_CLR(tsAfds.fd_array[i],&tsAfds);
}
tsMSock = INVALID_SOCKET;
return e;
}
/***********************************************************
* MODULE: Ts_ProcessFd
*
* PURPOSE:
* process tcp client request!
*
* PARAMETERS
* Input: none
* Output: none
* Return value: none
*
***********************************************************/
static int Ts_ProcessFd(int fd)
{
int len; /* length of recv data */
int e=0;
len = recv(fd, tsInBuf, MAXDATALEN, 0);
if(len < 0)
{
e = Dnet_ErrnoGet(fd);
if(e != WSAEWOULDBLOCK)
DEMO_DEBUG("tcp server -- receive failed: %d\r\n", e);
}
else
{
if(len >0) /* if(len > 0) - got some echo data */
{
DEMO_DEBUG("tcp server -- receive data length = %d \r\n", len);
/* we must be server, send echo reply */
e = send(fd, tsInBuf, len, 0);
if(e < 0)
{
/* Print the error to console */
e = Dnet_ErrnoGet(fd);
DEMO_DEBUG("tcp server -- send failed %d \r\n", e);
}
DEMO_DEBUG("tcp server -- send data length = %d \r\n", e);
}
else
{
if(len==0)
DEMO_DEBUG("tcp server -- the connect is terminated\r\n");
}
}
return e;
}
/***********************************************************
* MODULE: Ts_Task
*
* PURPOSE:
* Listen task when Initilization is ok!
*
* PARAMETERS
* Input: none
* Output: none
* Return value: none
*
***********************************************************/
static void Ts_Task()
{
unsigned nfds; /* error holder */
struct sockaddr_in client;
int e;
unsigned i;
int addrLen = sizeof(struct sockaddr_in);
fd_set rfds; /*read file descriptor set*/
struct timeval waitTime;
e = Ts_Init();
while (e)
{
DEMO_TASKSLEEP(1000);
e = Ts_Init();
}
while(1)
{
if(tsMSock == INVALID_SOCKET)
{
DEMO_DEBUG("tcp server -- master socket is invalid\r\n");
goto error;
}
waitTime.tv_sec = 30;
waitTime.tv_usec = 0;
FD_ZERO(&rfds);
memcpy(&rfds,&tsAfds,sizeof(fd_set));
nfds = select(0,&rfds,0, 0,&waitTime);
if (nfds == SOCKET_ERROR)
{
DEMO_DEBUG("tcp server -- bad fd_set \r\n");
goto error;
}
if (nfds == 0)
{
DEMO_DEBUG("tcp server -- select timeout \r\n");
goto error;
}
if (FD_ISSET(tsMSock,&rfds))
{
SOCKET tmpsock = INVALID_SOCKET; /* scratch socket */
/* check for received echo connection on server */
tmpsock = accept(tsMSock, (struct sockaddr*)&client,&addrLen);
if(tmpsock == INVALID_SOCKET)
{
e = Dnet_ErrnoGet(tsMSock);
DEMO_DEBUG("tcp server -- accept failed: %d\r\n", e);
goto error;
}
FD_SET(tmpsock,&tsAfds);
}
for(i=0;i<rfds.fd_count;++i)
{
if (rfds.fd_array[i] != tsMSock)
{
e = Ts_ProcessFd(rfds.fd_array[i]);
if (e <= 0)
{
e = closesocket(rfds.fd_array[i]);
if(e)
{
e = Dnet_ErrnoGet(rfds.fd_array[i]);
DEMO_DEBUG("tcp server -- close one error %d\r\n", e);
}
FD_CLR(rfds.fd_array[i],&tsAfds);
}
}
}
continue;
error:
Ts_Close();
e = Ts_Init();
while (e)
{
DEMO_TASKSLEEP(1000);
e = Ts_Init();
}
}
}
/***********************************************************
* MODULE: Init()
*
* PURPOSE:
* This routine is the initialization task for this test program.
* It is a user initialization task and has the responsibility for creating
* and starting the tasks that make up the test. If the time of day
* clock is required for the test, it should also be set to a known
* value by this function.
*
* PARAMETERS
* Input: none
* Output: none
* Return value: none
*
***********************************************************/
#define UART1 1
extern void UartInit(int baudrate, int serial);
delta_task Init(void)
{
int e=0;
delta_name taskname;
int taskpri=10;
delta_id taskid;
/*初始化串口,设置波特率为38400*/
UartInit( 115200, UART1);
#if (!CONFIGURE_USE_BURN_MODE)
mount_osaware();
#endif
/* 设置输出设备,设置底层的字符输出函数,实现输出重定向
* 在使用pintf,putc,puts等I/O输出函数前必须先用该函数设置字符输出函数,否则输出无效。
*/
set_outputfunc((outputfunc_ptr)uart_putchar_to_uart1);
#ifdef DELTA_NET
#ifdef Dnet_USE_ETHER
/*准备以太网*/
e = Ethdrv_Prepare();
if (e != DNET_SUCCESS)
{
printf("ether driver prepare failed\n");
delta_task_delete( DELTA_SELF );
}
#endif
#ifdef Dnet_USE_PPP
/*准备UART*/
e = Uartdrv_Prepare();
if (e != DNET_SUCCESS)
{
printf("uart drviver prepare failed\n");
delta_task_delete( DELTA_SELF );
}
#endif
/*配置DeltaNET*/
e = Dnet_Config((struct dnet_confparam *)&user_parms);
if (e != DNET_SUCCESS)
{
printf("DeltaNet config failed\n");
delta_task_delete( DELTA_SELF );
}
/*初始化DeltaNET*/
e = Dnet_Init();
if (e != DNET_SUCCESS)
{
printf("DeltaNet init failed\n");
delta_task_delete( DELTA_SELF );
}
#endif
taskname = delta_build_name('T','C','P','S');
e = delta_task_create ( taskname,
taskpri++,
1024 * 10,
DELTA_DEFAULT_MODES,
DELTA_DEFAULT_ATTRIBUTES,
&taskid
);
if (e != DELTA_SUCCESSFUL)
{
printf("tcp server task create failed:%d\n\r",e);
delta_task_delete( DELTA_SELF );
}
e = delta_task_start ( taskid,(delta_task_entry)Ts_Task, 0 );\
if (e != DELTA_SUCCESSFUL)
{
printf("tcp server task start failed:%d\n\r",e);
delta_task_delete( DELTA_SELF );
}
delta_task_delete( DELTA_SELF );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -