www.pudn.com > vxWorks_Lab.rar > conClient.c


/* conClient - Sends requests periodically to conServer */ 
 
/* Copyright 1984-1994 Wind River Systems, Inc. */ 
 
/* 
modification history 
-------------------- 
01b,05dec94,bss  cleaned up.  applied WRS coding conventions 
01a,???????,???  written. 
*/ 
 
/* 
DESCRIPTION 
This module implements a client which periodically 
sends a request to the slave spawned by a concurrent 
server.  This code illustrates how many clients 
to be handled in parallel if the server has a 
concurrent architecture. 
*/ 
 
/* includes */ 
 
#include  
#include  
#include  
#include  
#include "advTCPLab.h" 
 
/* defines */ 
 
#define MAX_MSG_SIZE 80 
 
#define LOCAL static 		/* for WRS coding conventions */ 
 
/* typedefs */ 
 
typedef int SOCK_FD; 
 
/* forward declarations */ 
LOCAL void error (); 
 
/******************************************************************************* 
 * 
 * main - entry point to client 
 * 
 * This function requests service from a master server. 
 * Subsequently, this task periodically sends a request 
 * to the slave server, spawned to handle a particular client. 
 */ 
 
main (argc, argv) 
	int argc; 
	char ** argv; 
 
	{ 
	u_long srvInet; 
	SOCK_FD sockFd; 
	struct sockaddr_in srvAddr; 
	char * request = "This is a request"; 
	int ix; 
 
	/* Get the server inet address */ 
	if (argc != 2) 
		{ 
		fprintf (stderr, "Usage: %s inetAddr\n", argv[0]); 
		exit (1); 
		} 
 
	/* Create a socket */ 
	if ( (sockFd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 ) 
		error ("Socket failed"); 
 
	/* Convert the server's IP address from ASCII dot notation to */ 
	/* a network byte-ordered integer */ 
	srvInet = inet_addr (argv[1]); 
 
	/* Initialize servers address */ 
	bzero ((char *)&srvAddr, sizeof(srvAddr)); 
	srvAddr.sin_family		= AF_INET;	 
	srvAddr.sin_port		= htons (SRV_PORT); 
	srvAddr.sin_addr.s_addr = srvInet; 
 
	/* Connect to server */ 
	if (connect(sockFd, &srvAddr, sizeof(srvAddr)) < 0) 
		{ 
		close (sockFd); 
		error ("conect failed"); 
		} 
 
	for (ix=1; ix<20; ix++) 
		{ 
		if (write (sockFd, request, strlen(request) + 1) < 
			strlen (request) + 1) 
			{ 
			close (sockFd); 
			error ("write failed"); 
			} 
		sleep (5); 
		} 
 
	close (sockFd); 
	} 
 
/************************************************************************ 
 * 
 * error - print an error message and exit. 
 * 
 */ 
LOCAL void error (pStr) 
	char * pStr; 
	{ 
	perror (pStr); 
	exit (1); 
	}