* updated kpat (21.12.1 -> 21.12.2), untested
[t2-trunk.git] / misc / archive / tcp-client.c
blobdce863088bb7aa2198483338ce9732b5b38f19b7
1 /*
2 * --- T2-COPYRIGHT-NOTE-BEGIN ---
3 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4 *
5 * T2 SDE: misc/archive/tcp-client.c
6 * Copyright (C) 2004 - 2005 The T2 SDE Project
7 * Copyright (C) 1998 - 2003 ROCK Linux Project
8 *
9 * More information can be found in the files COPYING and README.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License. A copy of the
14 * GNU General Public License can be found in the file COPYING.
15 * --- T2-COPYRIGHT-NOTE-END ---
18 #include <arpa/inet.h>
19 #include <netinet/in.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <sys/socket.h>
23 #include <sys/time.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <termio.h>
27 #include <stdlib.h>
29 #define BUFSIZE 1024
31 int main(int argc, char ** argv) {
32 struct sockaddr_in servaddr;
33 struct termio tbuf,tbufsav;
34 struct timeval tv;
35 char buf[BUFSIZE];
36 int sockfd,rc,c;
37 fd_set rfds;
39 if (argc != 3) {
40 printf("Usage: %s <IP-Address> <TCP-Port>\n",argv[0]);
41 return 1;
44 if ( (sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0 )
45 { perror("socket"); return 1; }
47 bzero(&servaddr,sizeof(servaddr));
48 servaddr.sin_family = AF_INET;
49 servaddr.sin_port = htons(atoi(argv[2]));
50 if ( inet_pton(AF_INET,argv[1],&servaddr.sin_addr) <= 0 )
51 { printf("Not an IP address: %s\n",argv[1]); return 1; }
52 if ( connect(sockfd,&servaddr,sizeof(servaddr)) < 0 )
53 { perror("connect"); return 1; }
55 if (ioctl(0,TCGETA, &tbuf) == -1) { perror("ioctl1"); return 1; }
56 tbufsav=tbuf; tbuf.c_lflag &= ~(ICANON|ECHO);
57 if (ioctl(0,TCSETAF, &tbuf) == -1) { perror("ioctl2"); return 1; }
59 do {
60 FD_ZERO(&rfds); FD_SET(sockfd,&rfds); FD_SET(0,&rfds);
61 tv.tv_sec=1; tv.tv_usec=0;
62 rc=select(sockfd+1, &rfds, NULL, NULL, NULL);
63 if (rc == -1) { perror("select"); return 1; }
64 if (FD_ISSET(sockfd,&rfds)) {
65 rc=read(sockfd,buf,BUFSIZE);
66 for (c=0; c<rc; c++)
67 if (buf[c]!='\r') write(1,buf+c,1);
69 if (FD_ISSET(0,&rfds)) {
70 rc=read(0,buf,BUFSIZE);
71 for (c=0; c<rc; c+=write(sockfd,buf,rc)) ;
73 } while (rc > 0);
75 if (ioctl(0,TCSETAF, &tbufsav) == -1) { perror("ioctl3"); return 1; }
76 return 0;