///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////以下代碼已經解決了fpt文件傳送過程中,數據丟失的問題(特別是大文件),爲V2.0版源碼
////////////////////////////////////////////////客戶端程序
#include<iostream>
#include<winsock2.h>
#include<windows.h>
using namespace std;
static USHORT ser_port=10000;//服務器端口
static char ser_addr_str[256];//進行鏈接的服務器IP
static u_short host_port=6000;//本地服務器端口
static char buf[2048];
static char fname[256];
DWORD WINAPI Clienter(LPVOID param)
{
int err;
WSADATA wsaData;
err=WSAStartup(MAKEWORD(1,1),&wsaData);//開啓網絡服務
if(err!=0)//啓動失敗
{
cerr<<"客戶端啓動失敗!"<<endl;
exit(0);
}
/*
循環取得不同文件
*/
get_file:
SOCKET client=socket(AF_INET,SOCK_STREAM,0);//建立SOCKET
/*
設定服務器的相關信息
*/
SOCKADDR_IN ser_addr;//服務器信息結構體
ser_addr.sin_addr.S_un.S_addr=inet_addr(ser_addr_str);//192.168.179.1 要訪問的服務器地址
ser_addr.sin_family=AF_INET;
ser_addr.sin_port=htons(ser_port);//目標主機端口
/*
與服務器鏈接
*/
err=connect(client,(SOCKADDR*)& ser_addr,sizeof(SOCKADDR));
if(err==-1)
{
cout<<"服務器鏈接失敗!"<<endl;
goto end;
}
cout<<"請你輸入請求的文件名:"<<endl;
memset(buf,0,sizeof(buf));
cin>>buf;
memcpy(fname,buf,256);
/*
發送的是文件名
*/
loop:
send(client,buf,sizeof(buf),0);
recv(client,buf,sizeof(buf),0);
if(buf[0]==-2)
goto loop;
if(buf[0]==-1)
{
cerr<<"ftp server do not have the file:"<<fname<<endl;
goto end;
}
else
{
long long flen;
int i=0,nlen;
memcpy(&flen,buf,sizeof(long long));
memcpy(&i,buf+sizeof(long long),4);
memcpy(&nlen,buf+sizeof(int)+sizeof(long long),4);
FILE *file=fopen(fname,"wb");
if(file==NULL)
{
cerr<<fname<<" create error!"<<endl;
goto end;
}
for(;i*1024+nlen!=flen;)
{
/*
將講到的內容寫到文件中;
*/
fwrite(buf+8+sizeof(long long),1,nlen,file);
memset(buf,0,sizeof(buf));
/*
接收成功標識,以便繼續傳送
*/
send(client,buf,64,0);
/*
再次接收
*/
int flag=recv(client,buf,sizeof(buf),0);
memcpy(&i,buf+sizeof(long long),4);
memcpy(&nlen,buf+4+sizeof(long long),4);
if(nlen==0)
{
break;
}
}
fwrite(buf+sizeof(int)*2+sizeof(long long),1,nlen,file);
fclose(file);
}
end:
closesocket(client);
/*
命令行提示信息
*/
char cmd;
cout<<"是否繼續(Y/N)?"<<endl;
getchar();//上次輸入的回車
cmd=getchar();
if(!(cmd=='n'||cmd=='N'))
goto get_file;
WSACleanup();
exit(0);
return 1;
}
int main()
{
cout<<"請你輸入對方的主機IP:";
cin>>ser_addr_str;
cout<<"對方主機服務端端口:";
char buf[256];
cin>>buf;
ser_port=atoi(buf);
if(ser_port<=1024)//如果輸入端口小於1024表明維持使用6000端口,當對話的另一方不使用同一主機時
ser_port=10000;
cout<<"本機服務端端口:";
cin>>buf;
host_port=atoi(buf);
if(host_port<=1024)//同上
host_port=6000;
CreateThread(0,0,Clienter,0,0,0);
while(1)
Sleep(10000);
return 1;
}
///////////////////////////////////////////////////////////////服務端程序
#include<iostream>
#include<WinSock2.h>
#include<windows.h>
using namespace std;
#pragma comment (lib,"ws2_32.lib")
USHORT host_port=10000;
SOCKET client;
SOCKET server;
sockaddr_in client_addr;
int addr_len;
char buf[2048];
char fname[256];
DWORD WINAPI ftp_server(LPVOID argv)//
{
start:
addr_len=sizeof(sockaddr);
client = accept(server,(sockaddr*)&client_addr,&addr_len);
cout<<inet_ntoa(client_addr.sin_addr)<<endl;
int b_len=0;
while(TRUE)
{
memset(buf,0,sizeof(buf));
int flag=
recv(client,buf,sizeof(buf),0);
if(strlen(buf)!=0)
{
cout<<"file_name:"<<buf<<endl;
break;
}
cout<<strerror(flag)<<endl;
cout<<buf<<endl;
memset(buf,0,sizeof(buf));
buf[0]=-2;
send(client,buf,1,0);//重複請求
}
memcpy(fname,buf,strlen(buf)>256?255:strlen(buf));
fname[255]=0;
FILE* file=fopen(fname,"rb");
if(file==NULL)
{
cerr<<"open error!"<<endl;
memset(buf,0,sizeof(buf));
buf[0]=-1;
send(client,buf,1,0);
goto end;
}
else
{
fseek(file,0,2);
long long flen=ftell(file);//得到文件最大長度;
int begin,nlen;
fseek(file,0,0);//回到文件頭部
if(flen==0)
{
cerr<<"文件長度爲零!"<<endl;
fclose(file);
goto end;
}
/*
對文件進行分塊傳送
*/
for(int i=0;;)
{
memset(buf,0,sizeof(buf));
memcpy(buf,&flen,sizeof(long long));
memcpy(buf+sizeof(long long),&i,4);
begin=ftell(file);
fread(buf+sizeof(long long)+8,1,1024,file);
nlen=ftell(file)-begin;/*文件讀取的內容長度*/
if(nlen==0)/*文件內容讀取完全*/
{
break;
}
memcpy(buf+sizeof(long long)+4,&nlen,4);
send(client,buf,sizeof(buf),0);
/*
先等待,讓客戶端接收完畢
*/
recv(client,buf,64,0);
}
fclose(file);
}
end:
closesocket(client);
goto start;
WSACleanup();
exit(0);
return 0;
}
int main(char argv[],int argc)
{
int flag;
WSADATA wd;
flag=WSAStartup(MAKEWORD(1,1),&wd);
if(flag!=0)
{
cerr<<"version error!"<<endl;
return 0;
}
/*
創建一個服務器socket
*/
server=socket(AF_INET,SOCK_STREAM,0);
/*
設定服務器socket的端口、使用的協議族
*/
SOCKADDR_IN ser_addr;
ser_addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
ser_addr.sin_port=htons(host_port);
ser_addr.sin_family=AF_INET;
/*
與對應的端口相綁定
*/
bind(server,(sockaddr*)&ser_addr,sizeof(sockaddr));//x
/*
將socket server設爲監聽模式
*/
listen(server,50);
/*
開始對請求進行監聽
*/
CreateThread(0,0,ftp_server,0,0,0);
while(1)
Sleep(10000);
return 1;
}