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

📄 conclient.c

📁 Tornado train workshop的配套实验教材
💻 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 <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#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);
	}

⌨️ 快捷键说明

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