home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / mtl / general / 481 < prev    next >
Encoding:
Text File  |  1992-11-21  |  6.1 KB  |  246 lines

  1. Xref: sparky mtl.general:481 can.general:5430
  2. Newsgroups: mtl.general,can.general
  3. Path: sparky!uunet!newsflash.concordia.ca!garrot.DMI.USherb.CA!turgd00
  4. From: turgd00@DMI.USherb.CA (DAVID TURGEON)
  5. Subject: Small C program to CHAT with
  6. Message-ID: <By03o6.LGv@DMI.USherb.CA>
  7. Sender: usenet@DMI.USherb.CA (Pour courrier Usenet)
  8. Nntp-Posting-Host: tohi
  9. Organization: Universite de Sherbrooke -- Dept. d'Informatique
  10. Date: Fri, 20 Nov 1992 06:07:16 GMT
  11. Lines: 233
  12.  
  13. Here's a small program a friend made to monitor a port that enables 
  14. Chat with anyone that can Telnet. (this is usefull when two people
  15. are on different systems with no chat compability.
  16.  
  17.  
  18. ====== CUT HERE and Compile using 'cc' or something ============
  19.  
  20. /***********************************************************************
  21. ** cht.c -- quick and dirty internet chat program
  22. **
  23. *************************************************************************
  24. ** cht is a poorly written program that allows a person to telnet to the
  25. ** cht server,
  26. ** and send messages to the person running cht.
  27. **
  28. ** The program waits for a TCP/IP connection to the specified port number
  29. ** (default is defined as PORTNUM), and when it gets a connection,
  30. ** it spawns 2 children. One child listens for data on the connection,
  31. ** and when it receives anything, it writes it to stdout, and the second
  32. ** child waits for input from stdin, and when it gets any, it writes it
  33. ** to the network.
  34. **
  35. ** Currently, two commands (not case sensitive) are recognized (from the
  36. **    network side only).
  37. **   /bye closes the connection
  38. **   /beep sends a beep to the terminal running cht, and prints "beep"
  39. **
  40. **
  41. ** As I said, this is a kludgey program, and can certainly be written more
  42. ** efficiently. Also, most of the network stuff was borrowed from
  43. ** another program, who's original purpose was to do remote tape
  44. ** tape copies from  a VMS system.
  45. **
  46. ************************************************************************
  47. ** Also, feel free to modify, expand on, improve, or anything else you want
  48. **    to do to this code.
  49. **
  50. ** I take no responsibility for any use, or abuse of this software, and
  51. **    I certainly do not guarantee it's usability -- if you don't
  52. **    like it, don't use it.
  53. **
  54. *************************************************************************/
  55. #include <stdio.h>
  56. #include <errno.h>
  57. #include <ctype.h>
  58. #include <sys/types.h>
  59. #include <sys/ioctl.h>
  60. #include <sys/mtio.h>
  61. #include <sys/socket.h>
  62. #include <netinet/in.h>
  63. #include <netdb.h>
  64.  
  65. #define PORTNUM 1227
  66. #define MAXHOSTNAME 255
  67. #define INBUFSIZ 1024
  68.  
  69.  
  70. main(argc, argv)
  71.     int     argc;
  72.     char   *argv[];
  73. {
  74.     int     s, t, dummy = -1;
  75.     int     port;
  76.  
  77.     if (argc == 2) {
  78.         if (sscanf(argv[1], "%d", &port) != 1) {
  79.             printf("Error - bad port number specified\n");
  80.             exit(1);
  81.         }
  82.     } else
  83.         port = PORTNUM;
  84.     printf("Port = %d\n", port);
  85.  
  86.     if ((s = establish(port)) < 0) {    /* establish port */
  87.         perror("establish");
  88.         printf("s = %d\n", s);
  89.         exit(1);
  90.     }
  91.  
  92.     if ((t = get_connection(s)) == -1) {        /* wait for connection */
  93.         perror("accept");
  94.         exit(1);
  95.     }
  96.     printf("get_connection was completed!\n");
  97.     got_connection(t);
  98.     clean_up(t);
  99. }
  100.  
  101. clean_up(t)
  102.     int     t;
  103. {
  104.     int     rc;
  105.     printf("Closing network\n");
  106.     close(t);
  107. }
  108.  
  109. getlines(t)
  110.     int     t;
  111. {
  112.     int     siz = 0;
  113.     int     i;
  114.     char    buf[255];
  115.     char    cmd[5];
  116.  
  117.     while(1) {
  118.         siz = read_line(t, buf, sizeof(buf));
  119.         for(i = 0; i < sizeof(cmd); i++)
  120.             cmd[i] = toupper(buf[i]);
  121.         if(!memcmp(cmd, "/BYE", 4))
  122.             return(0);
  123.         if(!memcmp(cmd, "/BEEP", 5)) {
  124.             write(1, "\007\n-> *** Beep ***\n", 18);
  125.             continue;
  126.         }
  127.         write(1, "\n-> ", 4);
  128.         write(1, buf, siz);
  129.     }
  130. };
  131.  
  132. putlines(t)
  133.     int     t;
  134. {
  135.     int     siz = 0;
  136.     char    buf[255];
  137.     char    user[255];
  138.     int     user_l;
  139.  
  140.     sprintf(user, "%s> ", cuserid(NULL));
  141.     user_l = strlen(user);
  142.  
  143.     while(1) {
  144.         siz = read_line(0, buf, sizeof(buf));
  145.         write(t, user, user_l);
  146.         write(t, buf, siz);
  147.     }
  148. };
  149.  
  150. got_connection(t)
  151.     int     t;
  152. {
  153.     int pid;
  154.     char msg[255];
  155.  
  156.     sprintf(msg, "\015Welcome to the Cha Internet Message program!!\015\n");
  157.  
  158.     write(t, msg, strlen(msg));
  159.  
  160.     write(1, "\007", 1);
  161.  
  162.     if(fork() == 0) {   /* fork first child to read from net */
  163.         getlines(t);    /*   (and write to local terminal) */
  164.         clean_up(t);
  165.     }
  166.     else if(fork() == 0) { /* and the second to write to net */
  167.         putlines(t);       /*   (and read from local terminal) */
  168.     }
  169.     else {
  170.         wait();
  171.     }
  172.  
  173. };
  174.  
  175. int read_line(s, buf, n)  /* reads until newline encountered */
  176.     int s;
  177.     char *buf;
  178.     int n;
  179. {
  180.     char ch;
  181.     int bcount = 0;
  182.  
  183.     while(bcount < n) {
  184.         if(read(s, &ch, 1) != 1) {
  185.             perror("read");
  186.             abort();
  187.         }
  188.         buf[bcount++] = ch;
  189.         if(ch == '\n') {
  190.             buf[bcount++] = '\015';
  191.             break;
  192.         }
  193.     }
  194.     return(bcount);
  195. }
  196.  
  197. int     establish(portnum)
  198.     unsigned short portnum;
  199. {
  200.     char    myname[MAXHOSTNAME + 1];
  201.     int     s;
  202.     struct sockaddr_in sa;
  203.     struct hostent *hp;
  204.  
  205.     bzero(&sa, sizeof(struct sockaddr_in));
  206.     gethostname(myname, MAXHOSTNAME);
  207.     printf("%s\n", myname);
  208.     /* gethostbyname() doesn't work on WIN/TCP on the VAX */
  209.  
  210. /*
  211.     hp = gethostbyname(myname);
  212.     if(hp == NULL)
  213.         return(-1);
  214.     sa.sin_family = hp->h_addrtype;
  215. */
  216.     sa.sin_family = AF_INET;
  217.     sa.sin_port = htons(portnum);
  218.     if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  219.         perror("socket ");
  220.         printf("socket failed - s = %d\n", s);
  221.         return (-1);
  222.     }
  223.     printf("s = %d\n", s);
  224.     if (bind(s, &sa, sizeof(sa)) < 0) {
  225.         perror("bind ");
  226.         close(s);
  227.         return (-1);
  228.     }
  229.     printf("s = %d\n", s);
  230.     listen(s, 3);
  231.     printf("s = %d\n", s);
  232.     return (s);
  233. }
  234.  
  235. int     get_connection(s)
  236.     int     s;
  237. {
  238.     struct sockaddr_in isa;
  239.     int     i = sizeof(isa);
  240.     int     t;
  241.  
  242.     if ((t = accept(s, &isa, &i)) < 0)
  243.         return (-1);
  244.     return (t);
  245. };
  246.