mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.6 KiB

  1. /*
  2. * --- ROCK-COPYRIGHT-NOTE-BEGIN ---
  3. *
  4. * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  5. * Please add additional copyright information _after_ the line containing
  6. * the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
  7. * the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
  8. *
  9. * ROCK Linux: rock-src/misc/archive/tcp-client.c
  10. * ROCK Linux is Copyright (C) 1998 - 2005 Clifford Wolf
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version. A copy of the GNU General Public
  16. * License can be found at Documentation/COPYING.
  17. *
  18. * Many people helped and are helping developing ROCK Linux. Please
  19. * have a look at http://www.rocklinux.org/ and the Documentation/TEAM
  20. * file for details.
  21. *
  22. * --- ROCK-COPYRIGHT-NOTE-END ---
  23. */
  24. #include <arpa/inet.h>
  25. #include <netinet/in.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <sys/socket.h>
  29. #include <sys/time.h>
  30. #include <sys/types.h>
  31. #include <unistd.h>
  32. #include <termio.h>
  33. #include <stdlib.h>
  34. #define BUFSIZE 1024
  35. int main(int argc, char ** argv) {
  36. struct sockaddr_in servaddr;
  37. struct termio tbuf,tbufsav;
  38. struct timeval tv;
  39. char buf[BUFSIZE];
  40. int sockfd,rc,c;
  41. fd_set rfds;
  42. if (argc != 3) {
  43. printf("Usage: %s <IP-Address> <TCP-Port>\n",argv[0]);
  44. return 1;
  45. }
  46. if ( (sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0 )
  47. { perror("socket"); return 1; }
  48. bzero(&servaddr,sizeof(servaddr));
  49. servaddr.sin_family = AF_INET;
  50. servaddr.sin_port = htons(atoi(argv[2]));
  51. if ( inet_pton(AF_INET,argv[1],&servaddr.sin_addr) <= 0 )
  52. { printf("Not an IP address: %s\n",argv[1]); return 1; }
  53. if ( connect(sockfd,&servaddr,sizeof(servaddr)) < 0 )
  54. { 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; }
  58. do {
  59. FD_ZERO(&rfds); FD_SET(sockfd,&rfds); FD_SET(0,&rfds);
  60. tv.tv_sec=1; tv.tv_usec=0;
  61. rc=select(sockfd+1, &rfds, NULL, NULL, NULL);
  62. if (rc == -1) { perror("select"); return 1; }
  63. if (FD_ISSET(sockfd,&rfds)) {
  64. rc=read(sockfd,buf,BUFSIZE);
  65. for (c=0; c<rc; c++)
  66. if (buf[c]!='\r') write(1,buf+c,1);
  67. }
  68. if (FD_ISSET(0,&rfds)) {
  69. rc=read(0,buf,BUFSIZE);
  70. for (c=0; c<rc; c+=write(sockfd,buf,rc)) ;
  71. }
  72. } while (rc > 0);
  73. if (ioctl(0,TCSETAF, &tbufsav) == -1) { perror("ioctl3"); return 1; }
  74. return 0;
  75. }