计算机网络课程设计Word下载.docx
《计算机网络课程设计Word下载.docx》由会员分享,可在线阅读,更多相关《计算机网络课程设计Word下载.docx(19页珍藏版)》请在冰豆网上搜索。
![计算机网络课程设计Word下载.docx](https://file1.bdocx.com/fileroot1/2022-12/14/ed9d7da4-ccb0-421a-96ad-161b6d642513/ed9d7da4-ccb0-421a-96ad-161b6d6425131.gif)
5.实习内容
5.1设计分析
计算机网络是建立在TCP/IP网络体系结构之上,计算机除了安装TCP/IP软件外。
还安装了TCP/IP开发系统。
本实验要求设计和实现一个简单的文件传输协议,需用Socket编程接口写两个程序,分别为客户端(Client.cs)和服务端(Server.cs)。
客户端程序为文件接收端、服务器端为文件发送端。
接收端能将收到的文件保存储到机器上。
从而实现文件传输能功。
当设计程序使客户连接的时候,服务器将会向客户发回一条消息告知它的IP地址,请求添加一个用户。
然关闭连接并继续接收端口的连接。
建立各个命令功能对应的函数,发送请求,等待服务器的服务。
服务器端初始化WinSock.创建SOCKET,获取主机信息,并对客户端进行会话,发送回复讯息给客户端。
响应完毕后关闭连接,释放winSock.从而实现文件传输。
5.2设计流程图
5.3程序实现代码
客户端程序:
#include<
Winsock2.h>
stdio.h>
iostream>
usingnamespacestd;
#pragmacomment(lib,"
wsock32.lib"
)
#definePORT4523
charbuf_send[1024];
charbuf_rec[1024];
charIP[20];
charfilename[20];
SOCKETsockClient;
SOCKADDR_INaddrServer;
DWORDCreateSock()//创建套接字
{
WSADATAWSAData;
chara[20];
memset(a,0,20);
if(WSAStartup(MAKEWORD(2,2),&
WSAData)!
=0)
{printf("
sockinitfail!
\n"
);
return(-1);
}
if(strncmp(IP,a,20)==0){
printf("
pleaseinputtheIPofhost:
"
scanf("
%s"
&
IP);
}
addrServer.sin_family=AF_INET;
addrServer.sin_addr.s_addr=inet_addr(IP);
addrServer.sin_port=htons(PORT);
return1;
DWORDConnectServer()//连接服务器
sockClient=socket(AF_INET,SOCK_STREAM,0);
if(sockClient==SOCKET_ERROR)
{
sockcreatefail!
\n"
WSACleanup();
return(-1);
if(connect(sockClient,(structsockaddr*)&
addrServer,sizeof(addrServer))==SOCKET_ERROR)
Connectfail\n"
memset(IP,0,20);
return
(1);
voidhelp()
charhelp[5];
cout<
<
type?
orhelplistthecommand:
;
cin>
>
help;
@*@*@*@*@*@*@*@*@*@*@CommandList@*@*@*@*@*@*@*@*@*@*@"
endl
<
'
?
'
or'
help'
……………………commandlist"
endl
dir'
…………………………directorylist"
cd'
……………………………changecurrentdirectory"
pwd'
……………………………listcurrentdirectory"
get'
……………………………downloadfiles"
put'
……………………………uploadfiles"
quit'
……………………………exit!
@*@*@*@*@*@*@*@*@*@*@@*@*@*@*@*@*@*@*@*@*@@*@*@*@*@*@*@"
endl;
voidlist(SOCKETsock)
intnRead;
while(true)
nRead=recv(sock,buf_rec,1024,0);
if(nRead==SOCKET_ERROR)
{
printf("
readresponseerror!
exit
(1);
}
if(nRead==0)//数据读取结束
break;
//显示数据
buf_rec[nRead]='
\0'
buf_rec);
intSendFile(SOCKETdatatcps,FILE*file)
printf("
sendingfiledata.."
for(;
//从文件中循环读取数据并发送客户端
intr=fread(buf_send,1,1024,file);
if(send(datatcps,buf_send,r,0)==SOCKET_ERROR)
lostthrconnectiontoclient!
closesocket(datatcps);
return0;
if(r<
1024)//文件传送结束
closesocket(datatcps);
done\n"
intProcessCmd()
intcount;
charorder[10];
charparam[20];
charcommand[30];
FILE*fd;
FILE*fd2;
command[0]='
CreateSock();
ConnectServer();
//returnmain;
//cout<
connectOK"
pleaseinputorder:
memset(order,0,10);
memset(param,0,20);
memset(command,0,30);
memset(buf_rec,0,1024);
memset(buf_send,0,1024);
order;
strcat(command,order);
if(strncmp(order,"
get"
3)==0||strncmp(order,"
put"
cd"
2)==0)
cin>
param;
strcat(command,"
"
strcat(command,param);
command<
strcpy(buf_send,command);
if(send(sockClient,buf_send,sizeof(buf_send),0)<
0)
cout<
senddataerror!
closesocket(sockClient);
return0;
if(strncmp(command,"
quit"
4)==0)
}//quit
recv(sockClient,buf_rec,1024,0);
buf_rec<
if(strncmp(buf_rec,"
dir"
3)==0)
list(sockClient);
}//dir
2)==0){}//cd
pwd"
3)==0){}//pwd
{
fd=fopen(param,"
wb"
//使用二进制方式
if(fd==NULL)
{
printf("
openfile%sforweitefailed!
param);
return0;
}
while((count=recv(sockClient,buf_rec,1024,0))>
0){
fwrite(buf_rec,sizeof(char),count,fd);
fclose(fd);
}//get
if(strncmp(buf_rec,"
{
strcpy(filename,buf_rec+9);
fd2=fopen(filename,"
rb"
if(fd2)
if(!
SendFile(sockClient,fd2))
{
printf("
sendfailed!
return0;
}
fclose(fd2);
else//打开文件失败
strcpy(buf_send,"
can'
topenfile!
if(send(sockClient,buf_send,1024,0))
}
}//put
closesocket(sockClient);
WSACleanup();
returnProcessCmd();
intmain()
help();
ProcessCmd();
return0;
服务器端:
SOCKETsockSrv;
//socket初始化
DWORDCreateSocket()
socketinitializefailed!
sockSrv=socket(AF_INET,SOCK_STREAM,0);
if(sockSrv==SOCKET_ERROR)
socketcreatefailed!
SOCKADDR_INaddrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_port=htons(PORT);
addrSrv.sin_family=AF_INET;
//绑定端口
if(bind(sockSrv,(structsockaddrFAR*)&
addrSrv,sizeof(addrSrv))==SOCKET_ERROR)
BindError"
return
(1);
intSendFileRecord(SOCKETdatatcps,WIN32_FIND_DATA*pfd)
//usedtosendresponsetoclient
charfilerecord[MAX_PATH+32];
FILETIMEft;
FileTimeToLocalFileTime(&
pfd->
ftLastWriteTime,&
ft);
SYSTEMTIMElastwtime;
FileTimeToSystemTime(&
ft,&
lastwtime);
char*dir=pfd->
dwFileAttributes&
FILE_ATTRIBUTE_DIRECTORY?
DIR>
:
sprintf(filerecord,"
%04d-%02d-%02d%02d:
%02d%5s%10d%-20s\n"
lastwtime.wYear,
lastwtime.wMonth,
lastwtime.wDay,
lastwtime.wHour,
lastwtime.wMinute,
dir,
pfd->
nFileSizeLow,
cFileName);
if(send(datatcps,filerecord,strlen(filerecord),0)==SOCKET_ERROR)
Erroroccurswhensendingfilelist!
intSendFileList(SOCKETdatatcps)
HANDLEhff;
WIN32_FIND_DATAfd;
//搜索文件
hff=FindFirstFile("
*"
fd);
if(hff==INVALID_HANDLE_VALUE)//发生错误
constchar*errstr="
tlistfiles!
listfileerror!
if(send(datatcps,errstr,strlen(errstr),0)==SOCKET_ERROR)
erroroccurswhensendingfilelist!
closesocket(datatcps);
BOOLfMoreFiles=TRUE;
while(fMoreFiles)
//发送此项文件信息
if(!
SendFileRecord(datatcps,&
fd))
//搜索下一个文件
fMoreFiles=FindNextFile(hff,&
losttheconnectiontoclient!
DWORDProcessCmd()
if(listen(sockSrv,5)<
Listenerror!
Listeningfortherequest……\n"
SOCKADDR_INaddrclient;
intlen=(sizeof(SOCKADDR_IN));
while
(1)
SOCKETsockconn=accept(sockSrv,(SOCKADDR*)&
addrclient,&
len);
if(sockconn==INVALID_SOCKET)
continue;
elseprintf("
connectingfromclient\n"
while(true)
{
charfilename[20];
memset(buf_rec,0,1024);
memset(buf_send,0,1024);
if(recv(sockconn,buf_rec,1024,0)<
break;
}
cout<
endl<
thecommandis:
3)!
=0&
&
strncmp(buf_rec,"
2)!
continue;
strcpy(buf_send,"
dir-ing\n"
send(sockconn,buf_send,1024,0);
SendFileList(sockconn);
}//dir
strcpy(filename,buf_rec+3);
cd-ing\n"
SetCurrentDirectory(filename);
}//cd
//获取当前目录,并放至回复报文中
if(!
GetCurrentDirectory(1024,buf_send))
pwd--can'
tgetthecurrentdirectory!
send(sockconn,buf_send,1024,0))
senderror!
}
if(strncmp(buf_rec,"
strcpy(filename,buf_rec+4);
printf(filename);
FILE*file;
//处理下载文件请求
file=fopen(filename,"
//打开下载的文件
if(file)
{
sprintf(buf_send,"
getfile%s\n"
filename);
{fclose(file);
else
{//创建额外数据连接传送数据
if(!
SendFile(sockconn,file))
return0;
fclose(file);
}//file
strc