关于VC++ SOCKET文件传输问题
我现在只会传输字符我想问下如何传输文件那个给我个思路或者给我点思路只写个简单的功能就是可以穿个文件到可户机我的思路是在服务端打开文件然后获取文件和内容在放到缓冲区在REA...
我现在只会传输字符我想问下如何传输文件那个给我个思路或者给我点思路只写个简单的功能就是可以穿个文件到可户机我的思路是在服务端打开文件然后获取文件和内容在放到缓冲区在READ发送可户端以接受的参数用CFILE类创建我觉得思路错了请那个给我点简单的代码我这问题想了好久都不会
展开
3个回答
展开全部
你先用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
char *pBuffer = new char[1024];
DWORD dwReadSize = 0;
::ReadFile(hFile,pBuffer,1024,&dwReadSize,NULL);
send(socket,reinterpret_cast<char *>(pBuffer),dwReadSize,NULL);
当然,文件大小需要你用GetFileSize来获得
最后记得closehandle
展开全部
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;
}
{
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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前面添加一些头用来标识
}
#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前面添加一些头用来标识
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询