📄 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 conventions01a,???????,??? written.*//*DESCRIPTIONThis module implements a client which periodicallysends a request to the slave spawned by a concurrentserver. This code illustrates how many clientsto be handled in parallel if the server has aconcurrent 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 + -