关于VC++ SOCKET文件传输问题
发布网友
发布时间:2022-11-13 19:03
我来回答
共3个回答
热心网友
时间:2024-08-19 02:32
你先用readfile把文件一次性读到缓冲区中。这样下面的操作就和传送字符一样了。在将文件的长度通知客户端,让他分配好内存,准备接受文件。下面就将缓冲区的东西发送到客户端就行了。这个方法进行小文件传输还凑合,要传输大文件需要先把文件分块传送。
char *pBuffer = new char[1024];
DWORD dwReadSize = 0;
::ReadFile(hFile,pBuffer,1024,&dwReadSize,NULL);
send(socket,reinterpret_cast<char *>(pBuffer),dwReadSize,NULL);
当然,文件大小需要你用GetFileSize来获得
最后记得closehandle
热心网友
时间:2024-08-19 02:32
BOOL bo_File::SendFile(CString strIP, UINT uPort, char *chSrc, BOOL bSend)
{
BOOL bRet = FALSE;
CSocket sckClient;
CFile file;
do
{
if(bSend)
bRet = file.Open(chSrc, CFile::modeRead);
else
bRet = file.Open(chSrc, CFile::modeCreate | CFile::modeWrite);
if(!bRet) break ;
bRet = sckClient.Create();
if(!bRet) break ;
bRet = sckClient.Connect(strIP, uPort);
if(!bRet) break ;
BYTE *chFileData = new BYTE[ 1024 + 1 ];
// char chData[10];
DWORD nSize = 0;
Message tagSendMsg;
memset(&tagSendMsg,0,sizeof(tagSendMsg));
if(bSend)
{
nSize = file.GetLength();
tagSendMsg.dwFileSize=nSize;
DWORD nRead = nSize;
// ZeroMemory(chData, 10);
// memcpy(chData, &nSize, sizeof(int));
int nRet = sckClient.Send(&tagSendMsg,sizeof(Message));
if(nRet <= 0)
{
bRet = FALSE;
break ;
}
while(nRead > 0)
{
int n1 = min(nRead, 1024);
int n2 = file.Read(chFileData, n1);
nRead -= n2;
nRet = sckClient.Send(chFileData, n2);
if(nRet <= 0)
{
bRet = FALSE;
break ;
}
bRet = TRUE;
}
}
else
{
char chData[10];
ZeroMemory(chData, 10);
nSize = 0;
int nRet = sckClient.Receive(chData, 10);//读文件大小
if(nRet <= 0)
{
bRet = FALSE;
break ;
}
nSize = *((int*)(chData));
if(nSize <= 0)
{
bRet = FALSE;
break ;
}
BYTE *chFileData = new BYTE[1024+1];
int nWrite = 0;
while(nWrite < nSize)
{
nRet = sckClient.Receive(chFileData, 1024);
if(nRet <= 0)
{
bRet = FALSE;
break ;
}
file.Write(chFileData, nRet);
nWrite += nRet;
bRet = TRUE;
}
}
delete []chFileData;
if(!bRet) // 送文件数据出错.
{
break ;
}
}while(0);
if(sckClient.m_hSocket != INVALID_SOCKET)
{
sckClient.Close();
}
if(file.m_hFile != CFile::hFileNull)
{
file.Close();
}
return bRet;
}
////////////////////////////
//////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
BOOL CParseMessage::ReceiveFile(UINT uPort, char *chDes, BOOL bSend)
{
BOOL bRet = FALSE;
CFile file;
CSocket sckServer;
CSocket sckClient;
AfxMessageBox("1");
do
{
if(!bSend)
{
bRet = file.Open(chDes, CFile::modeCreate | CFile::modeWrite);
}
else
{
bRet = file.Open(chDes, CFile::modeRead);
}
if(!bRet)
{
break ;
}
bRet = sckServer.Create(uPort);
if(!bRet)
{
break ;
}
bRet = sckServer.Listen();
if(!bRet)
{
break ;
}
bRet = sckServer.Accept(sckClient);
if(!bRet)
{
break ;
}
char chData[10];
BYTE *chFileData = new BYTE[1024+1];
int nSize = 0;
if(!bSend)
{
ZeroMemory(chData, 10);
int nRet = sckClient.Receive(chData, 10);//读文件大小
// CString s;
//s.Format("%d",nRet);
if(nRet <= 0)
{
bRet = FALSE;
break ;
}
nSize = *((int*)(chData));
if(nSize <= 0)
{
bRet = FALSE;
break ;
}
int nWrite = 0;
while(nWrite < nSize)
{
nRet = sckClient.Receive(chFileData, 1024);
if(nRet <= 0)
{
bRet = FALSE;
break ;
}
file.Write(chFileData, nRet);
nWrite += nRet;
bRet = TRUE;
}
}
else
{
nSize = file.GetLength();
int nRead = nSize;
ZeroMemory(chData, 10);
memcpy(chData, &nSize, sizeof(int));
int nRet = sckClient.Send(chData, sizeof(int));
if(nRet <= 0)
{
bRet = FALSE;
break ;
}
while(nRead > 0)
{
int n1 = min(nRead, 1024);
int n2 = file.Read(chFileData, n1);
nRead -= n2;
nRet = sckClient.Send(chFileData, n2);
if(nRet <= 0)
{
bRet = FALSE;
break ;
}
bRet = TRUE;
}
}
delete []chFileData;
}while(0);
if(sckClient.m_hSocket != INVALID_SOCKET)
{
sckClient.Close();
}
if(sckServer.m_hSocket != INVALID_SOCKET)
{
sckServer.Close();
}
if(file.m_hFile != CFile::hFileNull)
{
file.Close();
}
return bRet;
}
热心网友
时间:2024-08-19 02:32
I/O方法实现
#include<fstream>
using namespace std;
ifstream infile(strPath,ios::in|ios::binary);//strPath,你的文件路径
char buf[1024];
int len;
while(!infile.eof()) //判断是否读取结束
{
memset(buf,0,1024);
infile.read(buf,1024); //读文件
len=infile.gcount();//获取读入的长度
send(Sock,buf,len,0);//发送出去,你可以在buf前面添加一些头用来标识
}