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