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

📄 udpclnt.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 UDPCLNT_ECHOPORT		5000#define	UDPCLNT_DESTHOST		"192.168.0.200"		static char ucOutBuf[MAXDATALEN];static char ucInBuf[MAXDATALEN];/*********************************************************** * MODULE: Uc_Task * * PURPOSE: *			main body of udp client task. * * PARAMETERS *    Input:     none *    Output:    none *    Return value: int * ***********************************************************/void Uc_Task(){	unsigned long  servAddr;	int e,len,recvLen,i,totalTestTimes;	struct sockaddr_in me;	struct sockaddr_in him;	int addrLen = sizeof(struct sockaddr_in);	SOCKET  sock = INVALID_SOCKET;	fd_set recvSet;	struct timeval waitTime; 		for(i=0;i<MAXDATALEN;i++)		ucOutBuf[i] = 0x30+ i % 10;	while(1)	{		sock = socket(AF_INET, SOCK_DGRAM, 0);		if(sock == INVALID_SOCKET)		{			DEMO_DEBUG("udp client -- socket create failed: %d\r\n", sock);			goto error;		}		me.sin_family = AF_INET;		me.sin_addr.s_addr = INADDR_ANY;		me.sin_port = 0;  /* let UDP pick a client port */		/* make client socket a connected socket */		e = bind(sock, (struct sockaddr*)&me,addrLen);		if(e != 0)		{			e = Dnet_ErrnoGet(sock);			DEMO_DEBUG("udp client -- bind failed: %d\r\n", e);			goto error;		}		servAddr = inet_addr(UDPCLNT_DESTHOST);		if(!servAddr)		{			DEMO_DEBUG("udp client -- specify valid IP host, or use default active host\r\n");			goto error;		}		him.sin_family = AF_INET;		him.sin_addr.s_addr =servAddr ;		him.sin_port = htons(UDPCLNT_ECHOPORT);		totalTestTimes=0;		while(totalTestTimes<MAXSENDTIMES)		{			len = sendto(sock, ucOutBuf, MAXDATALEN, 0,(struct sockaddr*)&him,addrLen);			if(len != MAXDATALEN)			{				if(len < 0)				{					e = Dnet_ErrnoGet(sock);					printf("udp client -- send failed: %d \r\n", e);					goto error;				}				else				{					printf("udp client -- could only send %d bytes\r\n", len);					goto error;				}			}			DEMO_DEBUG("udp client -- send data\r\n");			len = 0;			recvLen=0;			waitTime.tv_sec = 10;			waitTime.tv_usec = 0;			FD_ZERO(&recvSet);			FD_SET(sock,&recvSet);			while ( ( e = select(0,&recvSet ,0, 0,&waitTime) ) > 0)			{				len = recvfrom(sock, ucInBuf, MAXDATALEN, 0, (struct sockaddr *)&him,(int *)&addrLen);				if ( len > 0 )				{					recvLen += len;					DEMO_DEBUG("udp client -- have been receive data length = %d\r\n",recvLen);					if (recvLen > MAXDATALEN)					{						printf("udp client -- received packet too long\r\n");						goto error;					}					if (recvLen == MAXDATALEN)					{						DEMO_DEBUG("udp client -- receive  completed \r\n");						break;					}				}				if(len < 0)				{						e = Dnet_ErrnoGet(sock);						if(e != WSAEWOULDBLOCK)							printf("udp client -- receive error %d \r\n", e);						goto error;				}				else if(len == 0)				{					printf("udp client -- receive over\r\n");					goto error;				}			}			if ( e == 0 )			{				printf("udp client -- timeout\r\n");				goto error;			}			if (e == SOCKET_ERROR)			{				DEMO_DEBUG("udp client -- bad fd_set \r\n");				goto error;			}			totalTestTimes++;		}/*while(totalTestTimes<MAXSENDTIMES)*/error:		if(sock != INVALID_SOCKET)		{			DEMO_TASKSLEEP(2000);			closesocket(sock);			sock= INVALID_SOCKET;			DEMO_DEBUG("udp client -- Close socket,try again.\r\n");	        DEMO_TASKSLEEP(10000);		}		continue;		}}/*********************************************************** * 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','C');	e = delta_task_create (	taskname,							taskpri++,							1024 * 10,							DELTA_DEFAULT_MODES,							DELTA_DEFAULT_ATTRIBUTES,							&taskid							);			if (e != DELTA_SUCCESSFUL)	{		printf("ucp client task  create failed:%d\n\r",e);		delta_task_delete( DELTA_SELF );	}	e = delta_task_start ( taskid,(delta_task_entry)Uc_Task, 0 );\	if (e != DELTA_SUCCESSFUL)	{		printf("ucp client 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 + -