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

📄 tcpclnt.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   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	TCPCLNT_ECHOPORT		5000
#define	TCPCLNT_DESTHOST		"192.168.0.200"			/*"wlj.coretek.com.cn"*/
static char tcOutBuf[MAXDATALEN];
static char tcInBuf[MAXDATALEN];
/***********************************************************
 * MODULE: Tc_Task()
 *
 * PURPOSE:
 *    main body of tcp client task.
 *
 * PARAMETERS:
 *    Input:   none
 *    Output:  none
 *    Return value: none
 *
 ***********************************************************/
static void Tc_Task()
{
	unsigned long  servAddr;
	int e,len,recvLen,i,sendTimes=0;
	struct sockaddr_in sa;
	SOCKET sock = INVALID_SOCKET;
	int	 totalTestTimes=0;
	fd_set recvSet;
	struct timeval waitTime; 

	for(i=0;i<MAXDATALEN;i++)
		tcOutBuf[i] = 0x30+ i % 10;
	while(1)
	{
		sock = socket(AF_INET, SOCK_STREAM, 0);
		if(sock == INVALID_SOCKET)
		{
			printf("tcp client -- socket create failed\r\n");
			goto error;
		}
		servAddr = inet_addr(TCPCLNT_DESTHOST);
		if(!servAddr)
		{
			printf("tcp client -- specify valid IP host, or use default active host\r\n");
			goto error;
		}
		sa.sin_family = AF_INET;
		sa.sin_addr.s_addr = servAddr;
		sa.sin_port = htons(TCPCLNT_ECHOPORT);
		DEMO_DEBUG("tcp client -- connect...\r\n");
		e = connect(sock, (struct sockaddr*)&sa,sizeof(struct sockaddr));
		if(e != 0)
		{
			e = Dnet_ErrnoGet(sock);
			printf("tcp client -- socket connect failed: %d\n", e);
			goto error;
		}
		/* Put in non-block mode */
		totalTestTimes++;
		printf("tcp client -- connect established : %d times\r\n",totalTestTimes);
		sendTimes=0;
		while(sendTimes<MAXSENDTIMES)
		{
			len = send(sock, tcOutBuf, MAXDATALEN, 0);
			if(len != MAXDATALEN)
			{
				e = Dnet_ErrnoGet(sock);
				printf("tcp client -- send failed: %d \r\n", e);
				goto error;
			}
			DEMO_DEBUG("tcp client -- send data length = %d\r\n",len);
			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 = recv(sock, tcInBuf , MAXDATALEN, 0);
				if ( len > 0 )
				{
					recvLen += len;
					DEMO_DEBUG("tcp client -- have been receive data length = %d\r\n",recvLen);
					if (recvLen > MAXDATALEN)
					{
						printf("tcp client -- received packet too long\r\n");
						goto error;
					}
					if (recvLen == MAXDATALEN)
					{
						DEMO_DEBUG("tcp client -- receive  completed \r\n");
						break;
					}
				}/*while ((len = Dnet_recv..*/
				if(len == 0)
				{
					printf("tcp client -- connection have terminated\r\n");
					goto error;
				}
				else
				if (len < 0)
				{
					e = Dnet_ErrnoGet(sock);
					printf("tcp client -- receive TCP echo error %d \r\n", e);
					goto error;
				}
			}
			if ( e == 0 )
			{
				printf("tcp client -- select timeout\r\n");
				goto error;
			}
			if (e == SOCKET_ERROR)
			{
				DEMO_DEBUG("tcp client -- bad fd_set \r\n");
				goto error;
			}
			sendTimes++;
		}/*while(sendTimes<MAXSENDTIMES)*/
error:
		if(sock != INVALID_SOCKET)
		{
	        DEMO_TASKSLEEP(5000);
			closesocket(sock);
			sock= INVALID_SOCKET;
			printf("tcp client -- Close socket,try again.\r\n\n");
	        DEMO_TASKSLEEP(5000);
		}
	}
}
/***********************************************************
 * 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;

//  	mount_osaware();

	/*初始化串口,设置波特率为38400*/
  	UartInit( 38400, 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','C');
	e = delta_task_create (	taskname,
							taskpri++,
							1024 * 10,
							DELTA_DEFAULT_MODES,
							DELTA_DEFAULT_ATTRIBUTES,
							&taskid
							);
		
	if (e != DELTA_SUCCESSFUL)
	{
		printf("tcp client task  create failed:%d\n\r",e);
		delta_task_delete( DELTA_SELF );
	}
	e = delta_task_start ( taskid,(delta_task_entry)Tc_Task, 0 );\
	if (e != DELTA_SUCCESSFUL)
	{
		printf("tcp 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 + -