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