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

📄 seqserver.c

📁 Vxworks的培训教程,大家分享下啊,
💻 C
字号:
/* seqServer - sequentially print a message to service client requests *//* Copyright 1984-1993 Wind River Systems, Inc. *//*modification history--------------------01b,06dec94,bss  cleaned up.01a,???????,???  written.*//*DESCRIPTIONThis server is a simple example of a TCP server.  Aftersetting up a TCP socket, seqServer goes into an infinite loop,processing client requests sequentially.*//* includes */#include "vxWorks.h"#include "advTCPLab.h"#include "errno.h"#include "inetLib.h"#include "ioLib.h"#include "netinet/in.h"#include "sockLib.h"#include "stdio.h"#include "string.h"#include "sys/socket.h"#include "sys/types.h"#include "taskLib.h"/* defines */#define MAX_MSG_LEN 80#define CON_SLAVE_PRIORITY	  100#define CON_SLAVE_OPTIONS	   0#define CON_SLAVE_STACK_SIZE	6000/* typedefs */typedef int SOCK_FD;/* forward declarations */LOCAL void doRequest(SOCK_FD sock, struct sockaddr_in *pClientAddr);LOCAL void error (char * pStr);/******************************************************************************* * * vxServer - accepts client requests and processes them * */int vxServer(void)	{    int 				clientAddrLength;	SOCK_FD             sockFd;     /* used to accept clients' requests */	SOCK_FD             newSockFd;  /* connected to new client */	struct sockaddr_in  clientAddr;	struct sockaddr_in  srvAddr;	clientAddrLength = sizeof (clientAddr);	/* Create a socket */	if ( (sockFd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 )		error ("Socket failed");	/* 	 * Bind to a well known address. INADDR_ANY says any network	 * interface will do. hton?() routines put things in network	 * byte order 	 */	bzero ((char *)&srvAddr, sizeof(srvAddr));	srvAddr.sin_family		= AF_INET;	srvAddr.sin_port		= htons (SRV_PORT);	srvAddr.sin_addr.s_addr = INADDR_ANY;	if (bind (sockFd, (struct sockaddr *) &srvAddr, sizeof(srvAddr)) < 0)		{		close (sockFd);		error ("Bind failed");		}	/* Queue uo to five requests (max allowed by TCP) */	if (listen (sockFd, 5) < 0)		{		close (sockFd);		error ("Listen failed");		}	/* Service requests */	FOREVER 		{		newSockFd = accept (sockFd, (struct sockaddr *) &clientAddr,							&clientAddrLength);		if (newSockFd < 0)			{			close (sockFd);			error ("Accept failed");			}		doRequest (newSockFd, &clientAddr);		}	}/************************************************************************ * * doRequest - read the clients requests from the socket and honor them. * * In a more typical application, this routine would fork of anouther * process to handle the request. * */void doRequest 	(	SOCK_FD sock,	struct sockaddr_in *pClientAddr	)	{	char 	buf [MAX_MSG_LEN];	int 	msgSize;	char 	clientInet [INET_ADDR_LEN];	u_short clientPort;	inet_ntoa_b (pClientAddr->sin_addr, clientInet);	clientPort = ntohs (pClientAddr->sin_port);	FOREVER		{		msgSize = read (sock, buf, MAX_MSG_LEN);		/* Read error ? */		if (msgSize < 0)			{			close (sock);			error ("Read failed");			}		/* Connection closed ? */		else if (msgSize == 0)			{			close (sock);			break;			}		/* Print client message and send reply */		else			{			printf ("Task %s servicing client from inet %s, port %d\n\n",				taskName(0), clientInet, clientPort);			}		}	}/******************************************************************************* * * error - aborts a task on an error  * * This funcion displays the reason a task crashed  * and terminates that task. */LOCAL void error (char * pStr)	{	perror (pStr);	exit (1);	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -