⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 udpserv.c

📁 在嵌入式系统DeltaSYSTEM中实现PING主机
💻 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   40struct 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_ETHERextern int Ethdrv_Prepare(void);#endif#ifdef Dnet_USE_PPPextern int Uartdrv_Prepare(void);#endif#define	MAXSENDTIMES	10#define	MAXDATALEN		1460//#define	UDPSERV_ECHOPORT	5000static SOCKET usSock = INVALID_SOCKET;static 	char usInBuf[MAXDATALEN];/*********************************************************** * MODULE: Us_Init * * PURPOSE: *    Initialization of udp server (socket) * * PARAMETERS *    Input:      none *    Output:     none *    Return value: int * ***********************************************************/static int Us_Init(){	int e;	struct sockaddr_in me;	int reuse = 1;		DEMO_DEBUG("udp server -- starting.\r\n");	usSock = socket(AF_INET, SOCK_DGRAM, 0);	if(usSock == INVALID_SOCKET)	{		DEMO_DEBUG("udp server -- socket create failed: %d\r\n", usSock);		return -1;	}	e = setsockopt(usSock,SOL_SOCKET,SO_REUSEADDR,(opt_type *)&reuse,sizeof(int));	if(e != 0)	{		e = Dnet_ErrnoGet(usSock);		DEMO_DEBUG("udp server -- set socket option SO_REUSEADDR failed: %d\r\n", e);		closesocket(usSock);		return e;    }	me.sin_family = AF_INET;	me.sin_addr.s_addr = INADDR_ANY;	me.sin_port = htons(UDPSERV_ECHOPORT);	e = bind(usSock, (struct sockaddr*)&me,sizeof(struct sockaddr));	if(e != 0)	{		e = Dnet_ErrnoGet(usSock);		DEMO_DEBUG("udp server -- socket bind failed: %d\r\n", e);		closesocket(usSock);		return e;    }	return  0;}/*********************************************************** * MODULE: Us_Close * * PURPOSE: *     Close the socket when udp server want to close * * PARAMETERS *    Input:    none *    Output:   none *    Return value:  int * ***********************************************************/static int Us_Close(){	int e=0;    if ( usSock != INVALID_SOCKET )	{        DEMO_DEBUG("udp server -- closing...\r\n");        e = closesocket(usSock);        if(e)        {			e = Dnet_ErrnoGet(usSock);			DEMO_DEBUG("udp server -- socket close failed %d\r\n", e);			usSock = INVALID_SOCKET;			return -1;        }        else        {			usSock = INVALID_SOCKET;			return 0 ;        }	}	return -1;}/*********************************************************** * MODULE: Us_Task * * PURPOSE: *     Listen task when Initilization is ok! * * PARAMETERS *    Input:   none *    Output:  none *    Return value: none * ***********************************************************/static void Us_Task(){	int len;            /* length of recv data */	int e;              /* error holder */	struct sockaddr_in him;	int addrLen = sizeof(struct sockaddr_in);	fd_set recvSet;	struct timeval waitTime;	                              		e = Us_Init();	while ( e )	{		DEMO_TASKSLEEP(1000);		e = Us_Init();	}	while(1)	{		if (usSock == INVALID_SOCKET)		{			DEMO_DEBUG("udp server -- master socket is invalid \r\n");			goto error;		}		waitTime.tv_sec = 60;		waitTime.tv_usec = 0;		FD_ZERO(&recvSet);		FD_SET(usSock,&recvSet);		e = select(0,&recvSet ,0, 0,NULL);//&waitTime);		if (e == SOCKET_ERROR)		{			DEMO_DEBUG("udp server -- bad fd_set \r\n");			goto error;		}		if (e == 0) 		{			DEMO_DEBUG("udp server -- select timeout \r\n");			goto error;		}		if (e > 0)		{			len = recvfrom(usSock, usInBuf, MAXDATALEN, 0, (struct sockaddr *)&him,(int *)&addrLen);			if(len > 0)			{				DEMO_DEBUG("udp server -- echo \r\n");				sendto(usSock, usInBuf, len, 0, (struct sockaddr *)&him,sizeof(struct sockaddr));			}			else if(len < 0)			{				e = Dnet_ErrnoGet(usSock);				if(e != WSAEWOULDBLOCK)					DEMO_DEBUG("udp server -- recvfrom failed %d\r\n", e);			}		}		continue;error:			Us_Close();		e = Us_Init();		while ( e )		{			DEMO_TASKSLEEP(10000);			e = Us_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 UART2 2extern void UartInit(int baudrate, int serial);delta_task Init(void){	int e=0;	delta_name taskname;	int taskpri=10;	delta_id taskid;	/*初始化串口,设置波特率为38400*/  	UartInit( 38400, UART2);  	    #if (!CONFIGURE_USE_BURN_MODE)    	mount_osaware();   #endif	  	/* 设置输出设备,设置底层的字符输出函数,实现输出重定向   	* 在使用pintf,putc,puts等I/O输出函数前必须先用该函数设置字符输出函数,否则输出无效。	*/    	set_outputfunc((outputfunc_ptr)uart_putchar_to_uart2);	#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('U','D','P','S');	e = delta_task_create (	taskname,							taskpri++,							1024 * 10,							DELTA_DEFAULT_MODES,							DELTA_DEFAULT_ATTRIBUTES,							&taskid							);			if (e != DELTA_SUCCESSFUL)	{		printf("ucp server task  create failed:%d\n\r",e);		delta_task_delete( DELTA_SELF );	}	e = delta_task_start ( taskid,(delta_task_entry)Us_Task, 0 );\	if (e != DELTA_SUCCESSFUL)	{		printf("udp 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 + -