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
noprintf(char * string,...)54 void noprintf(char *string, ...)
55 {
56 (void) string;
57 }
58
59 /* Read contents of FILE *fp. Write each line to socket, then
60 read line back from socket and write to standard output.
61 Return to caller when done */
62
str_cli(FILE * fp,int sockfd)63 void str_cli(FILE *fp, int sockfd)
64 {
65 int n;
66 char sendline[MAXLINE], recvline[MAXLINE + 1];
67 prtln();
68 while (fgets(sendline, MAXLINE, fp) != NULL) {
69 n = strlen(sendline);
70
71 dprt("%s: str_cli(): sendline = %s", __FILE__, sendline);
72
73 if (writen(sockfd, sendline, n) != n)
74 perror("str_cli: writen error on socket");
75 /*
76 * read a line from socket and write it to standard output
77 */
78
79 prtln();
80 n = readline(sockfd, recvline, MAXLINE);
81 prtln();
82 /*
83 printf("strcli: recvline = %s", recvline);
84 */
85 if (n < 0)
86 perror("str_cli: readline error on socket");
87 recvline[n] = 0;
88 fputs(recvline, stdout);
89 prtln();
90 }
91
92 prtln();
93 if (ferror(fp))
94 perror("str_cli: error reading file");
95 }
96
main(int argc,char * argv[])97 int main(int argc, char *argv[])
98 {
99 FILE *input;
100 int sockfd;
101 struct sockaddr_in serv_addr;
102
103 pname = argv[0];
104 if (argc < 3) {
105 printf("\nusage: %s ip data\n", pname);
106 exit(1);
107 }
108
109 /* Fill in the structure */
110 memset((char *)&serv_addr, 0x00, sizeof(serv_addr));
111 serv_addr.sin_family = AF_INET;
112 serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
113 serv_addr.sin_port = htons(SERV_TCP_PORT);
114 prtln();
115 dprt("%s: main(): Binding local address for client to use\n"
116 "serv_addr.sin_family = %d\n serv_addr.sin_addr.s_addr = %#x\n"
117 "serv_addr.sin_port = %d\n", __FILE__, serv_addr.sin_family,
118 serv_addr.sin_addr.s_addr, serv_addr.sin_port);
119
120 /* Open Internet stream socket */
121 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
122 printf("client: socket open failure, no = %d\n", errno);
123 return (errno);
124 exit(1);
125 }
126 prtln();
127 dprt("%s: main(): Open Internet stream socket, socfd = %d\n", __FILE__,
128 sockfd);
129 /* Connect to the server */
130 if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) <
131 0) {
132 prtln();
133 printf("client: connect failure, no = %d\n", errno);
134 return (errno);
135 exit(1);
136 }
137 #ifdef _LINUX
138 if ((input = fopen(argv[2], "r")) == NULL) {
139 perror("fopen");
140 return (errno);
141 }
142 str_cli(input, sockfd); /* call the routines that do the work */
143 prtln();
144 #else
145 prtln();
146 str_cli(stdin, sockfd); /* call the routines that do the work */
147 #endif
148 prtln();
149 close(sockfd);
150 exit(0);
151 }
152