#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#include<process.h>
#include<iostream>
#include<string>
#include<windows.h>
#include<conio.h>
using namespace std;
typedef SOCKET *PSOCKET;
struct fileInfo
{
char filename[20];
unsigned int flen;
};
//初始化winsocket API 库
int initWinSocket ()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD (1,1);
err = WSAStartup (wVersionRequested,&wsaData);
if (err!=0) return 0;
if (LOBYTE (wsaData.wVersion)!= 1 || HIBYTE (wsaData.wVersion) !=1)
{
WSACleanup ();
return 0;
}
return 1;
}
//请求服务模式1聊天
//int Accept (PSOCKET pSocket)
//{
// int count;
// char strBuf[50];
// count = recv(*pSocket,strBuf,50,0);
// strBuf[count] = '/0';
// cout<<strBuf<<endl;
// cout<<"请输入:"<<endl;
// cin>>strBuf;
// send (*pSocket,strBuf,strlen(strBuf),0);
// return 1;
//}
char Accept (PSOCKET pSocket)
{
char sst;
int err;
int len;
char strBuf[10];
err = recv (*pSocket,strBuf,10,0);
if (strcmp(strBuf,"123456")!=0)
{
send (*pSocket,"refus",5,0);
return ('0');
}
len = strlen(strBuf);
sst = strBuf[len+1];
send (*pSocket,"ready",5,0);
return sst;
}
fileInfo getInfo (PSOCKET pSocket,char st)
{
int count;
int flen;
char *pc;
char strBuf[30];
char filename[20];
FILE *fp;
fileInfo sInfoFile;
count = recv (*pSocket,strBuf,30,0);
strBuf[count] = '/0';
//cout<<"count"<<count<<" strBuf="<<strBuf<<endl;//检测
//getch();
if (st == '1')
{
pc = strBuf;
flen = *((int*)(pc));
//pc = &strBuf[4];
for (int i=1;i<=count;i++)
sInfoFile.filename[i-1] = pc[3+i];
sInfoFile.filename [count-4] = '/0';
sInfoFile.flen = flen;
//cout<<"sInfoFile:"<<sInfoFile.filename<<" "<<sInfoFile.flen<<endl; //检测
//getch();
}
else
if (st == '2')
{
pc = &strBuf[0];
for (int i=0;i<count;i++)
sInfoFile.filename[i] = pc[i];
sInfoFile.filename [count] = '/0';
fp = fopen (sInfoFile.filename,"rb");
fseek (fp,0,2);
flen = ftell (fp);
fclose (fp);
sInfoFile.flen = flen;
pc = (char*)(&flen);
for (int i=0;i<4;i++)
strBuf[i] = pc[i];
send (*pSocket,strBuf,4,0);
}
return sInfoFile;
}
//服务器端发送文件
int sendFile (PSOCKET pSocket, fileInfo sInfoFile)
{
FILE *fp;
int count;
//char filename[20];
char strBuf[4098];
int flen;
//char filename[20];
//cout<<"请输入要发送的文件名:";
//cin>>filename;
cout<<"服务器端等待客户端的准备完毕相应"<<endl;
count = recv (*pSocket,strBuf,5,0);
strBuf[count] = '/0';
if (strcmp (strBuf,"ready") != 0)
{
cout<<"错误!按任意键继续!"<<endl;
getch ();
}
fp = fopen (sInfoFile.filename,"rb");
//fp = fopen (filename,"rb");
do
{
count = fread (strBuf,1,4096,fp);
send (*pSocket,strBuf,count,0);
}while(!feof(fp));
if( SOCKET_ERROR==send (*pSocket,"/0",1,0) )
{
cout<<"传送结束标志失败!按任意键退出!"<<endl;
}
else
cout<<"文件传输完毕!按任意键继续!"<<endl;
getch ();
return 1;
}
//服务器端接收文件
int receiveFile (PSOCKET pSocket,fileInfo sInfoFile)
{
FILE *fp;
int count;
char strBuf[4098];
char filename[20];
cout<<"接收到的文件民:大小为:"<<sInfoFile.filename<<endl<<sInfoFile.flen<<endl;//检测
cout<<"文件另存为:";
cin>>filename;
fp = fopen (filename,"ab");
// fp = fopen (sInfoFile.filename,"ab");
send (*pSocket,"ready",5,0);
while (1)
{
count = recv (*pSocket,strBuf,4096,0);
if (count == 1 && strBuf[0]=='/0')
{
cout<<"传输完毕"<<endl;
break;
}
strBuf[count] = '/0';
fwrite (strBuf,1,count,fp);
//cout<<"count="<<count<<endl;
}
fclose (fp);
cout<<"请按任意键继续!"<<endl;
getch ();
return 1;
}
int server ()
{
int err;
unsigned int port;
int count;
char sst;
int len = sizeof (SOCKADDR);
char strBuf[1026];
fileInfo sInfoFile;
SOCKET socketSrv = socket (AF_INET,SOCK_STREAM,0);
if (socketSrv == SOCKET_ERROR)
{
cout<<"创建套接字失败!"<<endl;
return 0;
}
SOCKADDR_IN addrSrv;
SOCKADDR_IN addrClient;
addrSrv.sin_addr.S_un.S_addr = htonl (INADDR_ANY);
addrSrv.sin_family = AF_INET;
cout<<"请选择监听端口,按 1 为默认设置!";
cin>>port;
if (port == 1) port = 6000;
addrSrv.sin_port = htons (port);
bind (socketSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
listen (socketSrv,5);
SOCKET sockCon = accept (socketSrv,(SOCKADDR*)&addrClient,&len);
if (sockCon==INVALID_SOCKET)
{
cout<<"接受连接失败!"<<endl;
return 0;
}
sst = Accept (&sockCon);
if ('0'==sst)
{
//拒绝处理
cout<<"拒绝服务!"<<endl;
getch();
}
sInfoFile = getInfo (&sockCon,sst);
if (sst=='1')
receiveFile (&sockCon,sInfoFile);
else
if (sst == '2')
sendFile (&sockCon,sInfoFile);
//while(1)
//{
//count = recv (sockCon,strBuf,1024,0);
//if (count==SOCKET_ERROR)
//{
// cout<<"发送失败!"<<endl;
// return 0;
//}
//strBuf[count] = '/0';
//cout<<strBuf<<endl;
//cout<<"请输入回复:";
//cin>>strBuf;
//err = send (sockCon,strBuf,strlen(strBuf),0);
//if (err==SOCKET_ERROR)
//{
// cout<<"发送失败!"<<endl;
// return 0;
//}
//}
//while(1)
//{
//Accept (&sockCon);
//}
return 1;
}
void main ()
{
int color = 2;
HANDLE hCon = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleTextAttribute (hCon,color);
if (!initWinSocket ())
{
cout<<"初始化动态链接库失败!按任意键退出!"<<endl;
system ("PAUSE");
return;
}
server ();
getch();
}