单缓冲区模式文件传输模式相比于数组的好处是,文件IO操作和网络recv同时进行,这样提高效率
下面来看看代码:
服务器代码如下:
#include <Winsock2.h>
#include <iostream>
#include <thread>
#include<fstream>
#pragma comment(lib, "Ws2_32.lib")
using namespace std;
/*协议头*/
class FileHeader
{
public:
void Setname(wchar_t* szfillname)
{
wcscpy(_szfillname, szfillname);
}
const wchar_t* Getname(void)const
{
return _szfillname;
}
void Setsize(unsigned long long ullsize)
{
_ullfilesize = ullsize;
}
unsigned long long Getsize(void)
{
return _ullfilesize;
}
private:
wchar_t _szfillname[200];//用于装名字
unsigned long long _ullfilesize;//用于装文件大小的
};
int wmain(int ac,wchar_t* av[])
{
/* 初始化winsock2库 */
//Step 0. Initialize winsock2 library...
WSADATA wsaData;
wcout << av[1] << endl;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
return 1;
}
// Step 1. Ask system for a socket.this socket is waiter
SOCKET sockServer = ::socket(PF_INET, SOCK_STREAM, 0);
if (sockServer == INVALID_SOCKET)
{
return 1;
}
//Step 2. bind the socket to a local address and port.接待套接字
sockaddr_in addrServer = { 0 };
addrServer.sin_family = AF_INET;
addrServer.sin_addr.S_un.S_addr = inet_addr("88.88.106.242");
//host to network short.小端转大端
addrServer.sin_port = htons(30001);
if (::bind(sockServer, reinterpret_cast<const sockaddr*>(&addrServer),
sizeof addrServer) == SOCKET_ERROR)
{
return 1;
}
// Step 3. listen. 监听队列的设置 监听五个套接字的连接
if (listen(sockServer, 5) == SOCKET_ERROR)
{
return 1;
}
// Step 4. accept.
sockaddr_in addrClient = { 0 }; //将对放的信息保存在这个结构体里
int iLength = sizeof addrClient;
SOCKET sockClient = INVALID_SOCKET;
while (true)
{
//该返回的套接字才是用于收发的套接字-这里的逻辑是 一个连接开一个线程
sockClient = ::accept(sockServer, reinterpret_cast<sockaddr*>(&addrClient), &iLength);
if (sockClient == INVALID_SOCKET)
{
return 1;
}
thread t1([sockClient,av]()->void {
//open a file and retrieve the information.
fstream fs;
fs.open(av[1], fstream::in|fstream::binary);
if (!fs)
{
return;
}
wcout << av[1] << endl;
unsigned long long ullSize = 0ull;
fs.seekg(0, fstream::end); //文件指针移到末尾
ullSize = fs.tellg(); //获取文件的大小
fs.seekg(0, fstream::beg); //再将文件指针移到起始位置
//send file header
FileHeader header;
header.Setsize(ullSize);
header.Setname(av[1]);
unsigned long long iResult = ::send(sockClient, reinterpret_cast<const char*>(&header), sizeof(header), 0);
if (iResult<=0)
{
return;
}
//send body of file 多次发送
char readingBuf[4096];
while(!fs.eof())
{
fs.read(readingBuf, 4096); //读进buf
iResult = fs.gcount() ; //这里需要注意的是tellg()函数是累计的 上次实际读了多少应该用gcount()函数
::send(sockClient, reinterpret_cast<const char*>(readingBuf), iResult, 0);
//cout << "shijisend:" << shijie<<"tellg:" << iResult<<"last"<< lastReadSize <<endl;
}
fs.close();
closesocket(sockClient);
});
t1.detach();
}
closesocket(sockServer);
WSACleanup();
return 0;
}
客户端代码如下:
/*一个缓冲区,这样相比于数组的好处是,文件IO操作和网络recv同时进行,这样能提高效率*/
#include <Winsock2.h>
// Need to link with Ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")
#include <iostream>
#include "future" //asyc
#include <fstream>
#include "thread"
#define BUFFSIZE 1024*1024
using namespace std;
class FileHeader
{
public:
void Setname(wchar_t* szfillname)
{
wcscpy(_szfillname, szfillname);
}
const wchar_t* Getname(void)const
{
return _szfillname;
}
void Setsize(unsigned long long ullsize)
{
_ullfilesize = ullsize;
}
unsigned long long Getsize(void)
{
return _ullfilesize;
}
private:
wchar_t _szfillname[200];//用于装名字
unsigned long long _ullfilesize;//用于装文件大小的
};
int wmain(int ac, wchar_t* av[])
{
/* 初始化winsock2库 */
// Step 0. Initialize winsock2 library...
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
return 1;
}
//1.创建套接字
SOCKET socksever = ::socket(PF_INET, SOCK_STREAM, 0);
if (socksever == INVALID_SOCKET)
{
return 1;
}
//2.connect
sockaddr_in addrsocksever = { 0 };
addrsocksever.sin_family = AF_INET;
addrsocksever.sin_addr.S_un.S_addr = inet_addr("192.168.0.44");
addrsocksever.sin_port = htons(30001);
if (::connect(socksever, reinterpret_cast<const sockaddr*>
(&addrsocksever), sizeof addrsocksever) == SOCKET_ERROR)
{
return 1;
}
//3.receive header of file
FileHeader fileheader;
int ineedtorecieve = sizeof fileheader;
int irecv = 0; //实际接多少
int itotalrecv = 0;
do
{
irecv = ::recv(socksever, reinterpret_cast<char*>(&fileheader) + itotalrecv, ineedtorecieve, 0);
itotalrecv += irecv;
} while ((ineedtorecieve -= irecv) > 0);
//4. open a file and write to..
fstream fs;
fs.open(av[1], iostream::out | iostream::binary | iostream::trunc);
if (!fs)
{
return 1;
}
//5. recieve body of file ..
char* readingbuf = new char[BUFFSIZE];
unsigned long long ullremindersize = fileheader.Getsize();
itotalrecv = 0;
while (ullremindersize > 0)
{
itotalrecv = 0;//这个地方很重要,每缓冲区塞满了后,这个必须清零
ineedtorecieve = BUFFSIZE > ullremindersize ? ullremindersize : BUFFSIZE;
//直到接收到缓冲区大小的数据
while (ineedtorecieve > 0)
{
irecv = ::recv(socksever, readingbuf + itotalrecv, ineedtorecieve, 0);
ullremindersize -= irecv;
itotalrecv += irecv;
ineedtorecieve -= irecv;
}
//缓冲区收满往磁盘里写
fs.write(readingbuf, itotalrecv);
}
fs.close();
closesocket(socksever);
if (readingbuf)
{
delete[]readingbuf;
readingbuf = nullptr;
}
return 0;
}
另外传输文件的路径名称需要给命令行作为程序的输入av[1],接收文件的客户端也需要设置命令行参数,具体设置如下: