I am working on a socket library in windows. I am able to send a file but there are 2 problems I am encountering.
Issue 1:
IF the file is small, say 120 bytes When i send the file from server to client the files does not send UNLESS I put in a sleep(1) after my send (server).
Issue 2:
The Client seems to be revieving 1 extra recieve and adding BUFF_SIZE amount of data extra to every single binary file that is sent. I have also noticed that at time the client does not recieve _BUFFSIZE bytes, the odd time (very odd) the number is less the _BUFFSIZE bytes recieved.
NOTES:
_BUFFSIZE = 100 for these example, I have played around w/ larger and smaller buffer sizes and I have same result.
I am atempting to send various files all w. same results (.mp3, .wma, .txt, .doc, .avi)
Here is my Send and Reveive from my socket Lib... I have made 100% certain my program only calls these 1 time each.
There is some trivial code (counters) in here for my debuging purposes.
Server
Code:
int ServerFileTransfer::sendFile( std::string const& strFileName )
{
ifstream transferFile(strFileName.c_str(), ios::binary);
int bytes = -1;
if ( transferFile.is_open() )
{
ULONG filelength;
transferFile.seekg(0, ios::end);
filelength = transferFile.tellg();
// put back to start of file for reading
transferFile.seekg(0, ios::beg);
TCHAR sendBuff[ _BUFFSIZE ] = {0};
int count = 0;
ULONG bytesSent = 0;
for( bytesSent; bytesSent < filelength && !transferFile.eof(); bytesSent += _BUFFSIZE)
{
transferFile.read( sendBuff, _BUFFSIZE );
bytes = send( _cliSocketData, sendBuff, _BUFFSIZE * sizeof( TCHAR ), 0 );
++count;
if ( !isSocketOK( bytes ) )
return 0;
}
bytesSent -= _BUFFSIZE;
if ( filelength > bytesSent )
{
ULONG bytesRem = filelength - bytesSent;
transferFile.read( sendBuff, bytesRem);
bytes = send( _cliSocketData, sendBuff, bytesRem * sizeof( TCHAR ), 0 );
send
++count;
if ( !isSocketOK( bytes ) )
return 0;
}
transferFile.close();
}
return true;
}
Client
Code:
int ClientFileTransfer::receiveFile( std::string& strFileName )
{
TCHAR recvBuff[ _BUFFSIZE ];
int buffLen = _BUFFSIZE * sizeof( TCHAR );
int bytes = 0;
bool isOK = true;
ofstream outFile(strFileName.c_str(), ios::binary);
static ULONG bytesRecv = 0;
int count = 0;
while ((bytes = recv( _dataSock, recvBuff, buffLen, 0 )) > 0)
{
if ( bytes != _BUFFSIZE )
int x=0;
bytesRecv += bytes;
if (bytes != 100)
int x = 0;
TransmitFile()
outFile.write(recvBuff, bytes);
if( !( isOK = isSocketOK( bytes ) ))
break;
count++;
}
outFile.close();
return isOK;
}
Feel free to make any suggestions, I will try them all and give feedback on how I make out.
Thanks,