When i send a text file everything is fine.
When i try to send a .gif it gives me the correct size but goes into a loop and sending the same 11 bytes.
I open the files in binary mode so it cant be the problem.
I got a hunch that it should meet an EOF character or such but i dont check for that so i dont know what could be the problem.
Client sending:
Code:
void Send_File()
{
//***************************************
//Read file and send size
//***************************************
ifstream file("1.gif", ios::binary);
char cLength[256] = {0};
unsigned int nBytesSent = 0, fileSize = 0, r = 0;
// get length of file:
file.seekg (0, ios::end); fileSize = file.tellg(); file.seekg (0, ios::beg);
// allocate memory:
char * buffer = new char [fileSize];
// read data as a block:
file.read (buffer,fileSize);
file.close(); itoa(fileSize, cLength,10);
send(inetsock, cLength, strlen(cLength), 0);
//******************************************
//Send data
//******************************************
char *buf = new char[256];
cout << "fileSize " << fileSize << endl; system("pause");
while(nBytesSent<fileSize)
{
string str(buffer); //char* to string
strcpy(buf, str.substr(nBytesSent, 255).c_str());
r = send(inetsock, buf, strlen(buf), 0);
if (r==SOCKET_ERROR)
{
cout << "Connection unexpectedly closed \n"; closesocket(inetsock);
}
nBytesSent+=r;
cout << "nBytesSent " << nBytesSent << endl;
}
delete[] buffer; delete[] buf;
}
Server recieving:
Code:
void Recieve_File()
{
//Recieve size
char recvBuff[256]={0}; string fileBuff="";
unsigned int fileSize = 0, nBytesRecieved = 0, r = 0;
recv(AcceptSock, recvBuff, sizeof(recvBuff), 0);
fileSize=atof(recvBuff);
//cout << fileSize << endl; system("pause");
//******************************************
//Recieve data
//******************************************
char *buf = new char[256];
cout << "fileSize " << fileSize << endl;
while(nBytesRecieved<fileSize)
{
memset(buf, 0, 256);
r=recv(AcceptSock, buf, sizeof(buf), 0);
fileBuff.append(buf);
nBytesRecieved+=r+1;
cout << "nBytesRecieved " << nBytesRecieved << endl;
}
//cout << fileBuff << endl; system("pause");
ofstream TheCopy("TheCopy.txt",ios::binary | ios::app);
TheCopy.write (fileBuff.c_str(),fileSize);
TheCopy.close();
delete[] buf;
}