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

📄 conserver.c

📁 Vxworks的培训教程,大家分享下啊,
💻 C
字号:
/* conServer.c - demo concurrent TCP server */

/* Copyright 1984-1997 Wind River Systems, Inc. */

/*
modification history
--------------------
01c,09oct97,dlk  make port an argument
01b,05dec94,bss  cleaned up.  applied WRS coding conventions
01a,???????,???  written.
*/

/*
DESCRIPTION
This module implements an example of a concurrent server.
The master server sits in an infinite loop accepting
client requests.  When a request for service occurs, the
master server spawns a slave server to process the request(s)
sent by the client.  This construction allows many clients
to be handled in parallel.
*/

/* includes */

#include "vxWorks.h"
#include "sockLib.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include "inetLib.h"
#include "stdio.h"
#include "stdlib.h"
#include "ioLib.h"
#include "taskLib.h"
#include "string.h"
#include "conServer.h"
#include "advTCPLab.h"

/* defines */

#define MAX_MSG_LEN 80

/* typedefs */

typedef int SOCK_FD;

/* forward declarations */

LOCAL void doRequest(SOCK_FD sock, struct sockaddr_in * pClientAddr);
LOCAL void error (char * str);

/******************************************************************************
*
* vxServer - accepts client requests and spawns slave servers
*
* vxServer sits in an infinite loop accepting client's requests
* for service.  When a request occurs, the vxServer spawns off
* a slave server to handle the client's requests.  Then vxServer
* loops back around and will block until the next request for
* service occurs.
*/

void vxServer
	(
	u_short port
	)
	{
	int clientAddrLength;
	SOCK_FD sockFd;
	SOCK_FD newSockFd;
	struct sockaddr_in * pClientAddr;
	struct sockaddr_in srvAddr;

	clientAddrLength = sizeof (struct sockaddr_in);

	if (port == 0)
		port = SRV_PORT;

	/* 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. htonX()
	 * routines put things in network byte order
	 */

	bzero ((char *)&srvAddr, sizeof(srvAddr));
	srvAddr.sin_family = AF_INET;
	srvAddr.sin_port = htons(port);
	srvAddr.sin_addr.s_addr = INADDR_ANY;

	if ( bind (sockFd, (struct sockaddr *) &srvAddr, sizeof(srvAddr)) < 0 )
		{
		close (sockFd);
		error ("Bind failed");
		}


	/* Queue up to 8 requests (max allowed by TCP) */
	if ( listen (sockFd, 5) < 0 )
		{
		close (sockFd);
		error ("Listen failed");
		}

	/* Service requests */
	FOREVER
		{
		pClientAddr = malloc (sizeof(struct sockaddr));

		newSockFd = accept (sockFd, (struct sockaddr *)pClientAddr,
						&clientAddrLength);
		if (newSockFd < 0)
	 		{
			close (sockFd);
	 		error ("Accept failed");
	 		}

		taskSpawn ("tSlaveServer", SLAVE_PRIORITY, 0,
				SLAVE_STACK_SIZE, (FUNCPTR) doRequest,
				newSockFd,(int) pClientAddr, 0,0,0,0,0,0,0,0);
		}

 	}


/************************************************** 
*
* doRequest - read the client's requests from the
* socket and honor them.
*
*/

void doRequest
    (
    SOCK_FD sock,
    struct sockaddr_in * pClientAddr
    )

	{
	char buf[MAX_MSG_LEN];
	int msgSize;
	char clientInet [INET_ADDR_LEN];
	u_short port = ntohs (pClientAddr->sin_port);

	inet_ntoa_b (pClientAddr->sin_addr, clientInet);
	free (pClientAddr);
 
 	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;
			}

		/* Get client message and service it*/
		else
			{
			printf ("Task %s servicing client from"
					" inet %s, port %d\n\n",taskName(0),
					clientInet, port);
			}
		}
	}


void error 
    (
    char * str
    )
	{
	perror (str);
	exit (1);
	}

⌨️ 快捷键说明

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