I used the bottom code to transfer file over socket between 2 clients. When client A wants to send a file to client B, it sends that file to server, after the server receives that file, it sends that file to client B.
My code is used to read then send file:
CFile f;
BOOL p=f.Open(fname,CFile::modeRead);
char buff[1024];
int y;
int x;
if(!p)
//print error msg
while(true)
{
y=f.Read(buff,1024);
x=send(s,buff,y,0);
if(y<1024)
{
f.Close();
break;
}
}
send(s, "SENT|",6,0);
My code is used to receive then write file:
f.Open(fname,CFile::modeCreate | CFile::modeWrite);
while(true)
{
z=recv(sRecSocket,buff,512,0);
if(z==SOCKET_ERROR)
{
break;
}
if(strncmp(buff,"SENT|",5)==0)
break;
f.Write(buff,z);
}
f.Close();
send(sRecSocket,"RECV|",6,0);
But when i run my project, sometimes it works well, the file is sent completely to client B, but sometimes does not, the file is sent to client B but the size is 0 byte or smaller than the real size of file. Can somebody tell me the reason? (I'm not good at English, thanks for trying to understand what I'm saying)