📄 port.cpp
字号:
#include <dos.h>
#include "stdlib.h"
#include "stdio.h"
#define PORT 0
/*函数声明*/
void SendFile(char *fname); /* 发送文件*/
void Send(int s); /*发送一个字节*/
void SendFileName(char *fname); /*发送文件名*/
void ReceiveFile(); /*接收文件*/
void GetFileName(char *f); /*接收文件名*/
void InitPort(int port,unsigned char para); /*初始化端口*/
void SendPort(int port,char c); /*端口发送*/
int ReadPort(int port); /*读端口字节*/
int CheckState(int port); /*检查端口状态*/
int Receive(int port,int * G); /*接收一个字节*/
/*主函数*/
void main(int argc ,char *argv[] )
{
if(argc<2){
printf("Please input R(receive) or S(sent) parametre:");
exit(1);
}
InitPort(PORT,231);/* 初始化连接端口的参数*/
if(argv[1]=="S") /*检查选择的有效性*/
SendFile(argv[2]);/*通过建立的接口发送文件*/
else if(argv[1]=="R")
ReceiveFile();/*接收端负责文件的接收*/
else{
printf("Error parament.Please input again.");
exit(1);
}
}
void SendFile(char *fname )
{
FILE *fp;
int ch,s;
if(!(fp=fopen(fname,"rb"))){/*打开要发送的文件*/
printf("Can't open the file.\n");
exit(1);
}
SendFileName(fname); /*发送将要进行传输的文件的文件名*/
do{/*逐字节发送*/
ch=(int)getc(fp);/*得到发送文件的一个字节*/
if(ferror(fp)){
printf("Error reading file.\n");
break;
}
s=ch%16; /*取文件中一个字节的低4位*/
Send(s);/*发送一个字节的低4位*/
s=ch/16; /*取文件中一个字节的高4位*/
Send(s); /*发送一个字节的高4位*/
}while(!feof(fp));/*发送结束*/
s=46; /*发送文件结束信息*/
Send(s);
Send(s);
fclose(fp);
}
void Send(int s )
{
int G;
SendPort(PORT,s);/*发送将要建立连接的端口号*/
G=ReadPort(PORT); /*等待握手信号*/
if(s!=G)
s=s+16;
do{
SendPort(PORT,s); /*发送将要建立连接的端口号*/
G=ReadPort(PORT); /*等待握手信号*/
}while(s!=G);
}
void SendFileName(char *fname )
{
int s,ch;
printf("Now transmit the file.Please wait...");
while(*fname){/*逐字节传送要发送文件的文件名*/
ch=(int)fname++;
s=ch%16; /*取文件名中一个字节的低4位*/
Send(s);
s=ch/16;
Send(s); /*取文件名中一个字节的低4位*/
}
s=32; /*发送文件名结束标志*/
Send(s);
Send(s);
}
void ReceiveFile(){
FILE *fp;
char ch;
int G1,G2,G3;
char fname[15];
GetFileName(fname);/* 获得发送文件的文件名*/
printf("Receiving file %s.\n",fname);
remove(fname);
if(!(fp=fopen(fname,"wb"))){/*打开文件准备接受数据*/
printf("Can't open output file.\n");
exit(1);
}
/*循环为检测每次接受的数据是否为新数据,如果不是,则用此次接收的数据覆盖上次接收的数据*/
G1=ReadPort(PORT); /*读取连接端口的信息*/
G2=Receive(PORT,&G1);/* 建立连接后,接收端接收一个字节*/
do{
G3=Receive(PORT,&G2);/* 建立连接后,接收端接收一个字节*/
ch=(char)(G1%16+G2*16); /*恢复分开的数据,组合高4位和低4位*/
putc(ch,fp);/*写入文件一个字节*/
if(ferror(fp)){
printf("\nError writing file.");
exit(1);
}
G2=Receive(PORT,&G3);
G1=G3;
}while(G1/16!=48);
printf("\nTransmit finished.");
fclose(fp);/*关闭文件*/
}
int Receive(int port,int *G)
{
int GM;
SendPort(port,*G);/* 发送将要建立连接的端口号*/
GM=ReadPort(port); /*读取连接端口的信息*/
if(GM/16==0)
return GM;
else if(GM/16==1){
do{
*G=GM;
SendPort(port,GM);
GM=ReadPort(port);
}while(GM/16==1);
}
return GM;
}
void GetFileName(char * f)
{
int G1,G2,G3;
char ch;
G1=ReadPort(PORT);
G2=ReadPort(PORT);
do{
G3=Receive(PORT,&G3);
ch=(char)(G1%16+G2/16);
*f=ch;
*f++;
G2=Receive(PORT,&G3);
G1=G3;
}while(G1/16!=32);
printf("File name transmit finished.\n");
}
void InitPort(int port,unsigned char para)
{
union REGS reg;
reg.x.dx=port;
reg.h.ah=0;
reg.h.al=para;
int86(0x14,®,®);
}
void SendPort(int port,char c)
{
union REGS reg;
reg.x.dx=port;
reg.h.al=c;
reg.h.ah=1;
int86(0x14,®,®);
if(reg.h.ah&128){
printf("\nSend mistakes!");
exit(1);
}
}
int ReadPort(int port )
{
union REGS reg;
while(!(CheckState(port)&256)){/*检查端口状态*/
if(kbhit()){ /*如端口长期无数据可人为终止等待*/
printf("Press any key to exit.");
getch();
exit(1);
}
}
reg.x.dx=port;
reg.h.ah=2;
int86(0x14,®,®);
if(reg.h.ah&128){
printf("\nRead mistake!");
exit(1);
}
return reg.h.al;
}
int CheckState(int port )
{
union REGS reg;
reg.x.dx=port;
reg.h.ah=3;
int86(0x14,®,®);
return reg.x.ax;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -