发送文件

//服务器端
#include<iostream>
#include<fstream>
#include<WinSock2.h>
#include<fstream>
#define ONE_PAGE 1024*4
using namespace std;

#pragma comment(lib,"ws2_32.lib")


class FileHeader
{
public :
	void SetFileName(char *FileName)
	{
		strcpy_s(this->FileName,MAX_PATH,FileName);
	}
	char * GetFileName()
	{
		return this->FileName;
	}
	void SetFileSize(int FileSize)
	{
		this->FileSize=FileSize;
	}
	int GetFileSize()
	{
		return this->FileSize;
	}
private:
	char FileName[MAX_PATH];
	int FileSize;
};




int main()
{
		//加载库
		WORD wVersionRequested;
		WSADATA wsaData;
		int err;
		wVersionRequested = MAKEWORD( 2, 2 );
		err = WSAStartup( wVersionRequested, &wsaData );
		if ( err != 0 )
		{
			return 1;
		}
		if ( LOBYTE( wsaData.wVersion ) != 2 ||HIBYTE( wsaData.wVersion ) != 2 ) 
		{
			WSACleanup( );
			return 1; 
		}
		//创建socket
		SOCKET SocketListen;
		SocketListen =socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
		if(SocketListen==INVALID_SOCKET)
		{
			cout<<"socket at error. \n"<<endl;
			closesocket(SocketListen);
			WSACleanup();
			return 0;
		}
		//bind
		sockaddr_in Service; 
		Service.sin_family = AF_INET;
		Service.sin_addr.s_addr = inet_addr( "127.0.0.1" );
		Service.sin_port = htons( 1234 );
		// bind to server.
		if ( bind( SocketListen, (SOCKADDR*) &Service, sizeof(sockaddr_in) ) == SOCKET_ERROR)
		{
			printf( "Failed to connect.\n" );
			closesocket(SocketListen);
			WSACleanup();
			return 0;
		}
		//监听listen
		if (listen( SocketListen, 10 ) == SOCKET_ERROR)
		{
			closesocket(SocketListen);
			WSACleanup();
			printf("Error listening on socket.\n");
		}
		//接收accept
		SOCKET SocketAccept;
		SocketAccept=accept(SocketListen,NULL,NULL);
		if(SocketAccept==INVALID_SOCKET)
		{
			WSACleanup();
			closesocket(SocketListen);
			return 0;
		}
		//接收文件的名字和大小
		FileHeader File;
		if(SOCKET_ERROR==recv(SocketAccept,(char *)&File,sizeof(FileHeader),0))
		{
			cout<<"recv at error"<<endl;
			closesocket(SocketListen);
			closesocket(SocketAccept);
			WSACleanup();
			return 0;
		}
		//创建文件
		fstream fs;
		fs.open("G:\\计算机网络(第五版)谢希仁编著_课后习题答案.doc",fstream::binary|fstream::out|fstream::trunc);
		if(!fs)
		{
			cout<<"打开文件失败"<<endl;
			closesocket(SocketListen);
			closesocket(SocketAccept);
			WSACleanup();
			return 0;
		}
		char szbuf[ONE_PAGE]={0};
		int FileSize=File.GetFileSize();//文件的大小
		while(FileSize>0)
		{
			//读取一页到szbuf
			int nSize=recv(SocketAccept,szbuf,ONE_PAGE,0);
			if(nSize>0)
			{
				FileSize-=nSize;
				fs.write(szbuf,nSize);//szbuf写入到文件fs
			}
			//else
			//{
			//	break;
			//}
		}
		fs.close();
		closesocket(SocketListen);
		closesocket(SocketAccept);
		WSACleanup();
		return 0;
}
//客户端
#include<iostream>
#include<fstream>
#include<WinSock2.h>

#define ONE_PAGE 1024*4
using namespace std;

#pragma comment(lib,"ws2_32.lib")


class FileHeader//文件类
{
public :
	void SetFileName(char *FileName)
	{
		strcpy_s(this->FileName,MAX_PATH,FileName);
	}
	char * GetFileName()
	{
		return this->FileName;
	}
	void SetFileSize(int FileSize)
	{
		this->FileSize=FileSize;
	}
	int GetFileSize()
	{
		return this->FileSize;
	}
private:
	char FileName[MAX_PATH];//文件名字
	int FileSize;//文件大小
};




int main()
{
		//加载库
		WORD wVersionRequested;
		WSADATA wsaData;
		int err;
		wVersionRequested = MAKEWORD( 2, 2 );
		err = WSAStartup( wVersionRequested, &wsaData );
		if ( err != 0 ) 
		{     
			return 0;
		}
		if ( LOBYTE( wsaData.wVersion ) != 2 ||HIBYTE( wsaData.wVersion ) != 2 ) 
		{
			WSACleanup( );
			return 0; 
		}
		//创建socket
		SOCKET ConnectSocket;
		ConnectSocket =socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
		if(ConnectSocket==INVALID_SOCKET)
		{
			cout<<"socket at error. \n"<<endl;
			WSACleanup();
			return 1;
		}
		//Connect
		sockaddr_in clientService; 
		clientService.sin_family = AF_INET;
		clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
		clientService.sin_port = htons( 1234 );
		// Connect to server.
		if ( connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) 
		{
			cout<<"Failed to connect.\n";
			closesocket(ConnectSocket);
			WSACleanup();
			return 1;
		}
		fstream fs;
		//获得文件的名称和位置
		char *FileName="C:\\Users\\Administrator\\Desktop\\计算机网络(第五版)谢希仁编著_课后习题答案.doc";
		//打开文件
		fs.open(FileName,fstream::binary|fstream::in);
		if(!fs)
		{
			cout<<"打开失败"<<endl;
			return 0;
		}
		//移动文件获得文件大小
		fs.seekg(0,fstream::end);
		int FileSize=fs.tellg();
		fs.seekg(0,fstream::beg);
		//定义文件对象
		FileHeader File;
		File.SetFileName(FileName);
		File.SetFileSize(FileSize);
		//见文件的名字大小发送到服务器
		if(SOCKET_ERROR==send(ConnectSocket,(const char *)&File,sizeof(FileHeader),0))
		{
			cout<<"发送失败"<<endl;
			return 0;
		}
		//读取文件内容
		char szbuf[ONE_PAGE]={0};
		while(1)
		{
			fs.read(szbuf,ONE_PAGE);
			int nSize=fs.gcount();//读取的实际大小
			if(nSize>0)
			{
				if(SOCKET_ERROR==send(ConnectSocket,szbuf,nSize,0))
				{
					continue;
				}
			}
			else
			{
				break;
			}
		}
		fs.close();
		closesocket(ConnectSocket);
		WSACleanup();
		return 0;
}

你可能感兴趣的:(发送文件)