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.

78 lines
2.2 KiB

  1. /*
  2. * --- SDE-COPYRIGHT-NOTE-BEGIN ---
  3. * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. *
  5. * Filename: lib/misc/tcp-client.c
  6. * Copyright (C) 2008 The OpenSDE Project
  7. * Copyright (C) 2004 - 2006 The T2 SDE Project
  8. * Copyright (C) 1998 - 2003 Clifford Wolf
  9. *
  10. * More information can be found in the files COPYING and README.
  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; version 2 of the License. A copy of the
  15. * GNU General Public License can be found in the file COPYING.
  16. * --- SDE-COPYRIGHT-NOTE-END ---
  17. */
  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>
  28. #define BUFSIZE 1024
  29. int main(int argc, char ** argv) {
  30. struct sockaddr_in servaddr;
  31. struct termio tbuf,tbufsav;
  32. struct timeval tv;
  33. char buf[BUFSIZE];
  34. int sockfd,rc,c;
  35. fd_set rfds;
  36. if (argc != 3) {
  37. printf("Usage: %s <IP-Address> <TCP-Port>\n",argv[0]);
  38. return 1;
  39. }
  40. if ( (sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0 )
  41. { perror("socket"); return 1; }
  42. bzero(&servaddr,sizeof(servaddr));
  43. servaddr.sin_family = AF_INET;
  44. servaddr.sin_port = htons(atoi(argv[2]));
  45. if ( inet_pton(AF_INET,argv[1],&servaddr.sin_addr) <= 0 )
  46. { printf("Not an IP address: %s\n",argv[1]); return 1; }
  47. if ( connect(sockfd,&servaddr,sizeof(servaddr)) < 0 )
  48. { perror("connect"); return 1; }
  49. if (ioctl(0,TCGETA, &tbuf) == -1) { perror("ioctl1"); return 1; }
  50. tbufsav=tbuf; tbuf.c_lflag &= ~(ICANON|ECHO);
  51. if (ioctl(0,TCSETAF, &tbuf) == -1) { perror("ioctl2"); return 1; }
  52. do {
  53. FD_ZERO(&rfds); FD_SET(sockfd,&rfds); FD_SET(0,&rfds);
  54. tv.tv_sec=1; tv.tv_usec=0;
  55. rc=select(sockfd+1, &rfds, NULL, NULL, NULL);
  56. if (rc == -1) { perror("select"); return 1; }
  57. if (FD_ISSET(sockfd,&rfds)) {
  58. rc=read(sockfd,buf,BUFSIZE);
  59. for (c=0; c<rc; c++)
  60. if (buf[c]!='\r') write(1,buf+c,1);
  61. }
  62. if (FD_ISSET(0,&rfds)) {
  63. rc=read(0,buf,BUFSIZE);
  64. for (c=0; c<rc; c+=write(sockfd,buf,rc)) ;
  65. }
  66. } while (rc > 0);
  67. if (ioctl(0,TCSETAF, &tbufsav) == -1) { perror("ioctl3"); return 1; }
  68. return 0;
  69. }