home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lan / poor_lan / poor_lan.c < prev    next >
C/C++ Source or Header  |  1989-10-19  |  6KB  |  255 lines

  1. /* Poor man's LAN file server.   Port settings:
  2.  
  3.         9600 baud,
  4.         no parity,
  5.         eight data bits,
  6.         two stop bits.
  7.  
  8. */
  9.  
  10. #define PORT 0
  11.  
  12. #include <dos.h>
  13. #include <stdio.h>
  14.  
  15. unsigned int filesize(FILE *fp);
  16. void sport(int port, char c); void send_file(int port);
  17. void rec_file(int port); void send_file_name(char *f, int port);
  18. void get_file_name(char *f, int port); void port_init(int port);
  19. void wait(int port); char rport(int port);
  20.  
  21. main () {
  22.     puts("File server in operation.");
  23.     puts("To abort, press any key.");
  24.  
  25.     port_init(PORT); /* initialize the serial port */
  26.  
  27.     do {
  28.         /* wait until a request is received */
  29.         if(check_stat(PORT) & 256) {
  30.             switch(rport(PORT)) {
  31.                 case 's': send_file(PORT);
  32.                     break;
  33.                 case 'r': rec_file(PORT);
  34.                     break;
  35.             }
  36.         }
  37.  
  38.         /**********************************
  39.         Add additional workstations by checking
  40.         more ports as shown here.
  41.  
  42.         if(check_stat(PORT1) & 256) {
  43.             switch(rport(PORT1)) {
  44.                 case 's': send_file(PORT1);
  45.                     break;
  46.                 CASE 'r': rec_file(PORT1);
  47.                     break;
  48.             }
  49.         }
  50.         .
  51.         .
  52.         .
  53.         if(check_stat(PORTn) & 256) {
  54.             switch(rport(PORTn)) {
  55.                 case 's': send_file(PORTn);
  56.                     break;
  57.                 case 'r': rec_file(PORTn);
  58.                     break;
  59.             }
  60.         }
  61.         ***********************************/
  62.     } while(!kbhit());
  63. }
  64.  
  65. /* Send the specified file through specified port. */
  66. void send_file (int port) {
  67.     FILE *fp;
  68.     char ch, fname[14];
  69.     union {
  70.         char c[2];
  71.         unsigned int count;
  72.     } cnt;
  73.  
  74.     sport(port, '.'); /* acknowledge */
  75.  
  76.     get_file_name(fname, PORT);
  77.     if(!(fp = fopen(fname, "rb"))) {
  78.         puts("cannot open input file");
  79.         exit(1);
  80.     }
  81.  
  82.     if(rport(port) != '.') {
  83.         puts("remote file failure.");
  84.         exit(1);
  85.     }
  86.  
  87.     printf("sending file %s\n", fname);
  88.     /* find out the size of the file */
  89.     cnt.count = filesize(fp);
  90.     /* send size */
  91.     sport(port, cnt.c[0]);
  92.     wait(port);
  93.  
  94.     sport(port, cnt.c[1]);
  95.     do {
  96.         ch = getc(fp);
  97.         if(ferror(fp)) {
  98.             puts("error reading input file");
  99.             break;
  100.         }
  101.         /* wait until receiver is ready */
  102.         if(!feof(fp)) {
  103.             wait(port);
  104.             sport(port, ch);
  105.         }
  106.     } while (!feof(fp));
  107.     wait(port); /* read the last period from port */
  108.     fclose(fp);
  109. }
  110.  
  111. /* Receive a file through the specified port */
  112. void rec_file (int port) {
  113.     FILE *fp;
  114.     char ch, fname[14];
  115.     union {
  116.         char c[2];
  117.         unsigned int count;
  118.     } cnt;
  119.  
  120.     sport(port, '.'); /* acknowledge */
  121.  
  122.     get_file_name(fname, PORT);
  123.  
  124.     printf("receiving file %s\n", fname);
  125.     remove(fname);
  126.     if(!(fp = fopen(fname, "wb"))) {
  127.         puts("cannot open output file");
  128.         exit(1);
  129.     }
  130.  
  131.     /* get file length */
  132.     sport(port, '.');
  133.     cnt.c[0] = rport(port);
  134.     sport(port, '.');
  135.     cnt.c[1] = rport(port);
  136.     sport(port, '.');
  137.  
  138.     for( ; cnt.count; cnt.count--) {
  139.         ch = rport(port);
  140.         putc(ch, fp);
  141.         if(ferror(fp)) {
  142.             puts("error reading file");
  143.             exit(1);
  144.         }
  145.         sport(port, '.');
  146.     }
  147.     fclose(fp);
  148. }
  149.  
  150. /* Return the length, in bytes, of a file */
  151. unsigned int filesize(FILE *fp) {
  152.     unsigned long int i;
  153.  
  154.     i = 0;
  155.     do {
  156.         getc(fp);
  157.         i++;
  158.     } while (!feof(fp));
  159.     rewind(fp);
  160.     return i - 1; /* don't count the EOF char */
  161. }
  162.  
  163. /* Send the file name */
  164. void send_file_name(char *f, int port) {
  165.     do {
  166.         sport(port, '?');
  167.     } while (!kbhit() && !(check_stat(port) & 256));
  168.     if(kbhit()) {
  169.         getch();
  170.         exit(1);
  171.     }
  172.     wait(port);
  173.  
  174.     while(*f) {
  175.         sport(port, *f++);
  176.         wait(port);
  177.     }
  178.     sport(port, 0);
  179. }
  180.  
  181. /* Receive the file name */
  182. void get_file_name(char *f, int port) {
  183.     while(rport(port) != '?') printf(".");
  184.     sport(port, '.');
  185.     while((*f = rport(port))) {
  186.         if(*f != '?') {
  187.             f++;
  188.             sport(port, '.');
  189.         }
  190.     }
  191.     sport(port, '.');
  192. }
  193.  
  194. /* Send a character out the serial port */
  195. void sport(int port, char c) {
  196.     union REGS r;
  197.  
  198.     r.x.dx = port;      /* serial port */
  199.     r.h.al = c;         /* byte to send */
  200.     r.h.ah = 1;         /* send character function */
  201.     int86(0x14, &r, &r);
  202.     if(r.h.ah & 128) {
  203.         printf("send error detected in serial port %d", r.h.ah);
  204.         exit(1);
  205.     }
  206. }
  207.  
  208. /* Read a character from a port */
  209. char rport(int port) {
  210.     union REGS r;
  211.  
  212.     /* wait for a character */
  213.     while(!(check_stat(port) & 256))
  214.         if(kbhit()) {
  215.             getch();
  216.             exit(1);
  217.         }
  218.     r.x.dx = port;      /* serial port */
  219.     r.h.ah = 2;         /* read character function */
  220.     int86(0x14, &r, &r);
  221.     if(r.h.ah & 128)
  222.         printf("read error detected in serial port");
  223.     return r.h.al;
  224. }
  225.  
  226. /* Check the status of the serial port */
  227. int check_stat(int port) {
  228.     union REGS r;
  229.  
  230.     r.x.dx = port;      /* serial port */
  231.     r.h.ah = 3;         /* read status */
  232.     int86(0x14, &r, &r);
  233.     return r.x.ax;
  234. }
  235.  
  236. /* Initialize port to 9600 baud, two stop bits,
  237.    no parity, 8 data bits.
  238. */
  239. void port_init(int port) {
  240.     union REGS r;
  241.  
  242.     r.x.dx = port;      /* serial port */
  243.     r.h.ah = 0;         /* initialize port function */
  244.     r.h.al = 231;       /* initialization code */
  245.     int86(0x14, &r, &r);
  246. }
  247.  
  248. /* Wait for a response */
  249. void wait(int port) {
  250.     if(rport(port) != '.') {
  251.         puts("communication error");
  252.         exit(1);
  253.     }
  254. }
  255.