2 * --- T2-COPYRIGHT-NOTE-BEGIN ---
3 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
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
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>
22 #include <sys/socket.h>
24 #include <sys/types.h>
31 int main(int argc
, char ** argv
) {
32 struct sockaddr_in servaddr
;
33 struct termio tbuf
,tbufsav
;
40 printf("Usage: %s <IP-Address> <TCP-Port>\n",argv
[0]);
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; }
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
);
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
)) ;
75 if (ioctl(0,TCSETAF
, &tbufsav
) == -1) { perror("ioctl3"); return 1; }