Home | History | Annotate | Download | only in clisrv
      1 /*
      2  *
      3  *   Copyright (c) International Business Machines  Corp., 2001
      4  *
      5  *   This program is free software;  you can redistribute it and/or modify
      6  *   it under the terms of the GNU General Public License as published by
      7  *   the Free Software Foundation; either version 2 of the License, or
      8  *   (at your option) any later version.
      9  *
     10  *   This program is distributed in the hope that it will be useful,
     11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
     12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     13  *   the GNU General Public License for more details.
     14  *
     15  *   You should have received a copy of the GNU General Public License
     16  *   along with this program;  if not, write to the Free Software
     17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  */
     19 
     20 /******************************************************************************
     21  *
     22  *   pthcli.c
     23  *
     24  *
     25  *   (C) COPYRIGHT International Business Machines Corp. 1993
     26  *   All Rights Reserved
     27  *   Licensed Materials - Property of IBM
     28  *   US Government Users Restricted Rights - Use, duplication or
     29  *   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
     30  *
     31  *****************************************************************************/
     32 
     33 /******************************************************************************/
     34 /* File:        pthcli.c                                                      */
     35 /*                                                                            */
     36 /* Description: Read contents of data file. Write each line to socket, then   */
     37 /*              ead line back from socket and write to standard output.       */
     38 /*                                                                            */
     39 /*                                                                            */
     40 /* Usage:       pthcli [port number]                                          */
     41 /*                                                                            */
     42 /******************************************************************************/
     43 
     44 /* client using TCP */
     45 
     46 #include <stdio.h>
     47 #include <string.h>
     48 #include <unistd.h>
     49 #include "inet.h"
     50 #include <errno.h>
     51 #include <stdlib.h>
     52 #define MAXLINE 1024
     53 
     54 void noprintf(char *string, ...)
     55 {
     56 }
     57 
     58 /* Read contents of FILE *fp. Write each line to socket, then
     59    read line back from socket and write to standard output.
     60    Return to caller when done */
     61 
     62 void str_cli(fp, sockfd)
     63 register FILE *fp;
     64 register int sockfd;
     65 {
     66 	int n;
     67 	char sendline[MAXLINE], recvline[MAXLINE + 1];
     68 	prtln();
     69 	while (fgets(sendline, MAXLINE, fp) != NULL) {
     70 		n = strlen(sendline);
     71 
     72 		dprt("%s: str_cli(): sendline = %s", __FILE__, sendline);
     73 
     74 		if (writen(sockfd, sendline, n) != n)
     75 			perror("str_cli: writen error on socket");
     76 		/*
     77 		 * read a line from socket and write it to standard output
     78 		 */
     79 
     80 		prtln();
     81 		n = readline(sockfd, recvline, MAXLINE);
     82 		prtln();
     83 		/*
     84 		   printf("strcli: recvline = %s", recvline);
     85 		 */
     86 		if (n < 0)
     87 			perror("str_cli: readline error on socket");
     88 		recvline[n] = 0;
     89 		fputs(recvline, stdout);
     90 		prtln();
     91 	}
     92 
     93 	prtln();
     94 	if (ferror(fp))
     95 		perror("str_cli: error reading file");
     96 }
     97 
     98 int main(int argc, char *argv[])
     99 {
    100 	FILE *input;
    101 	int sockfd;
    102 	struct sockaddr_in serv_addr;
    103 
    104 	pname = argv[0];
    105 	if (argc < 3) {
    106 		printf("\nusage: %s ip data\n", pname);
    107 		exit(1);
    108 	}
    109 
    110 	/* Fill in the structure */
    111 	memset((char *)&serv_addr, 0x00, sizeof(serv_addr));
    112 	serv_addr.sin_family = AF_INET;
    113 	serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
    114 	serv_addr.sin_port = htons(SERV_TCP_PORT);
    115 	prtln();
    116 	dprt("%s: main(): Binding local address for client to use\n"
    117 	     "serv_addr.sin_family = %d\n serv_addr.sin_addr.s_addr = %#x\n"
    118 	     "serv_addr.sin_port = %d\n", __FILE__, serv_addr.sin_family,
    119 	     serv_addr.sin_addr.s_addr, serv_addr.sin_port);
    120 
    121 	/* Open Internet stream socket */
    122 	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    123 		printf("client: socket open failure, no = %d\n", errno);
    124 		return (errno);
    125 		exit(1);
    126 	}
    127 	prtln();
    128 	dprt("%s: main(): Open Internet stream socket, socfd = %d\n", __FILE__,
    129 	     sockfd);
    130 	/* Connect to the server */
    131 	if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) <
    132 	    0) {
    133 		prtln();
    134 		printf("client: connect failure, no = %d\n", errno);
    135 		return (errno);
    136 		exit(1);
    137 	}
    138 #ifdef _LINUX
    139 	if ((input = fopen(argv[2], "r")) == NULL) {
    140 		perror("fopen");
    141 		return (errno);
    142 	}
    143 	str_cli(input, sockfd);	/* call the routines that do the work */
    144 	prtln();
    145 #else
    146 	prtln();
    147 	str_cli(stdin, sockfd);	/* call the routines that do the work */
    148 #endif
    149 	prtln();
    150 	close(sockfd);
    151 	exit(0);
    152 }
    153