home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
lan
/
poor_lan
/
poor_lan.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-10-19
|
6KB
|
255 lines
/* Poor man's LAN file server. Port settings:
9600 baud,
no parity,
eight data bits,
two stop bits.
*/
#define PORT 0
#include <dos.h>
#include <stdio.h>
unsigned int filesize(FILE *fp);
void sport(int port, char c); void send_file(int port);
void rec_file(int port); void send_file_name(char *f, int port);
void get_file_name(char *f, int port); void port_init(int port);
void wait(int port); char rport(int port);
main () {
puts("File server in operation.");
puts("To abort, press any key.");
port_init(PORT); /* initialize the serial port */
do {
/* wait until a request is received */
if(check_stat(PORT) & 256) {
switch(rport(PORT)) {
case 's': send_file(PORT);
break;
case 'r': rec_file(PORT);
break;
}
}
/**********************************
Add additional workstations by checking
more ports as shown here.
if(check_stat(PORT1) & 256) {
switch(rport(PORT1)) {
case 's': send_file(PORT1);
break;
CASE 'r': rec_file(PORT1);
break;
}
}
.
.
.
if(check_stat(PORTn) & 256) {
switch(rport(PORTn)) {
case 's': send_file(PORTn);
break;
case 'r': rec_file(PORTn);
break;
}
}
***********************************/
} while(!kbhit());
}
/* Send the specified file through specified port. */
void send_file (int port) {
FILE *fp;
char ch, fname[14];
union {
char c[2];
unsigned int count;
} cnt;
sport(port, '.'); /* acknowledge */
get_file_name(fname, PORT);
if(!(fp = fopen(fname, "rb"))) {
puts("cannot open input file");
exit(1);
}
if(rport(port) != '.') {
puts("remote file failure.");
exit(1);
}
printf("sending file %s\n", fname);
/* find out the size of the file */
cnt.count = filesize(fp);
/* send size */
sport(port, cnt.c[0]);
wait(port);
sport(port, cnt.c[1]);
do {
ch = getc(fp);
if(ferror(fp)) {
puts("error reading input file");
break;
}
/* wait until receiver is ready */
if(!feof(fp)) {
wait(port);
sport(port, ch);
}
} while (!feof(fp));
wait(port); /* read the last period from port */
fclose(fp);
}
/* Receive a file through the specified port */
void rec_file (int port) {
FILE *fp;
char ch, fname[14];
union {
char c[2];
unsigned int count;
} cnt;
sport(port, '.'); /* acknowledge */
get_file_name(fname, PORT);
printf("receiving file %s\n", fname);
remove(fname);
if(!(fp = fopen(fname, "wb"))) {
puts("cannot open output file");
exit(1);
}
/* get file length */
sport(port, '.');
cnt.c[0] = rport(port);
sport(port, '.');
cnt.c[1] = rport(port);
sport(port, '.');
for( ; cnt.count; cnt.count--) {
ch = rport(port);
putc(ch, fp);
if(ferror(fp)) {
puts("error reading file");
exit(1);
}
sport(port, '.');
}
fclose(fp);
}
/* Return the length, in bytes, of a file */
unsigned int filesize(FILE *fp) {
unsigned long int i;
i = 0;
do {
getc(fp);
i++;
} while (!feof(fp));
rewind(fp);
return i - 1; /* don't count the EOF char */
}
/* Send the file name */
void send_file_name(char *f, int port) {
do {
sport(port, '?');
} while (!kbhit() && !(check_stat(port) & 256));
if(kbhit()) {
getch();
exit(1);
}
wait(port);
while(*f) {
sport(port, *f++);
wait(port);
}
sport(port, 0);
}
/* Receive the file name */
void get_file_name(char *f, int port) {
while(rport(port) != '?') printf(".");
sport(port, '.');
while((*f = rport(port))) {
if(*f != '?') {
f++;
sport(port, '.');
}
}
sport(port, '.');
}
/* Send a character out the serial port */
void sport(int port, char c) {
union REGS r;
r.x.dx = port; /* serial port */
r.h.al = c; /* byte to send */
r.h.ah = 1; /* send character function */
int86(0x14, &r, &r);
if(r.h.ah & 128) {
printf("send error detected in serial port %d", r.h.ah);
exit(1);
}
}
/* Read a character from a port */
char rport(int port) {
union REGS r;
/* wait for a character */
while(!(check_stat(port) & 256))
if(kbhit()) {
getch();
exit(1);
}
r.x.dx = port; /* serial port */
r.h.ah = 2; /* read character function */
int86(0x14, &r, &r);
if(r.h.ah & 128)
printf("read error detected in serial port");
return r.h.al;
}
/* Check the status of the serial port */
int check_stat(int port) {
union REGS r;
r.x.dx = port; /* serial port */
r.h.ah = 3; /* read status */
int86(0x14, &r, &r);
return r.x.ax;
}
/* Initialize port to 9600 baud, two stop bits,
no parity, 8 data bits.
*/
void port_init(int port) {
union REGS r;
r.x.dx = port; /* serial port */
r.h.ah = 0; /* initialize port function */
r.h.al = 231; /* initialization code */
int86(0x14, &r, &r);
}
/* Wait for a response */
void wait(int port) {
if(rport(port) != '.') {
puts("communication error");
exit(1);
}
}