OpenSDE Framework (without history before r20070)
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.

77 lines
2.2 KiB

  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 - 2006 The T2 SDE Project
  7. * Copyright (C) 1998 - 2003 Clifford Wolf
  8. *
  9. * More information can be found in the files COPYING and README.
  10. *
  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 ---
  16. */
  17. #include <arpa/inet.h>
  18. #include <netinet/in.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <sys/socket.h>
  22. #include <sys/time.h>
  23. #include <sys/types.h>
  24. #include <unistd.h>
  25. #include <termio.h>
  26. #include <stdlib.h>
  27. #define BUFSIZE 1024
  28. int main(int argc, char ** argv) {
  29. struct sockaddr_in servaddr;
  30. struct termio tbuf,tbufsav;
  31. struct timeval tv;
  32. char buf[BUFSIZE];
  33. int sockfd,rc,c;
  34. fd_set rfds;
  35. if (argc != 3) {
  36. printf("Usage: %s <IP-Address> <TCP-Port>\n",argv[0]);
  37. return 1;
  38. }
  39. if ( (sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0 )
  40. { perror("socket"); return 1; }
  41. bzero(&servaddr,sizeof(servaddr));
  42. servaddr.sin_family = AF_INET;
  43. servaddr.sin_port = htons(atoi(argv[2]));
  44. if ( inet_pton(AF_INET,argv[1],&servaddr.sin_addr) <= 0 )
  45. { printf("Not an IP address: %s\n",argv[1]); return 1; }
  46. if ( connect(sockfd,&servaddr,sizeof(servaddr)) < 0 )
  47. { perror("connect"); return 1; }
  48. if (ioctl(0,TCGETA, &tbuf) == -1) { perror("ioctl1"); return 1; }
  49. tbufsav=tbuf; tbuf.c_lflag &= ~(ICANON|ECHO);
  50. if (ioctl(0,TCSETAF, &tbuf) == -1) { perror("ioctl2"); return 1; }
  51. do {
  52. FD_ZERO(&rfds); FD_SET(sockfd,&rfds); FD_SET(0,&rfds);
  53. tv.tv_sec=1; tv.tv_usec=0;
  54. rc=select(sockfd+1, &rfds, NULL, NULL, NULL);
  55. if (rc == -1) { perror("select"); return 1; }
  56. if (FD_ISSET(sockfd,&rfds)) {
  57. rc=read(sockfd,buf,BUFSIZE);
  58. for (c=0; c<rc; c++)
  59. if (buf[c]!='\r') write(1,buf+c,1);
  60. }
  61. if (FD_ISSET(0,&rfds)) {
  62. rc=read(0,buf,BUFSIZE);
  63. for (c=0; c<rc; c+=write(sockfd,buf,rc)) ;
  64. }
  65. } while (rc > 0);
  66. if (ioctl(0,TCSETAF, &tbufsav) == -1) { perror("ioctl3"); return 1; }
  67. return 0;
  68. }