Click to See Complete Forum and Search --> : Lan vs one PC


MasterDucky
August 10th, 2009, 12:31 PM
I got a little server-client TCP-IP application for file transfer.
Its working perfectly on one machine (meaning server and client on the same machine with 192.168.1.2).

When i connect them on a Lan (2 computers) it sends the size of the file first which the server recieves fine.
But then it just hangs there like it would waiting for something and doesnt wanna go to the second recv() for recieving the data.
The data gets sent (at least after the cout<< i placed right after send())

Let me remind you that all works perfectly well on one machine.

This all doesnt make sense to me.
So am i right to think that it would rather be a problem with OS, router etc. then the program itself?

henky@nok.co.id
August 10th, 2009, 08:59 PM
I remind you that all works perfectly well on one machine.

This all doesnt make sense to me.
So am i right to think that it would rather be a problem with OS, router etc. then the program itself?
Not always. It depends on how your program handles unexpected
recv() result. For example, your program expects to receive 1KB
data and it works fine in one machine but might not be in LAN.

MasterDucky
August 11th, 2009, 06:14 AM
Thank you Henky!

Here are the sending and the recieving parts if somebody would wanna take a look at it.

The sending side


//***************************************
//Read file and send size
//***************************************
ifstream file("1.jpg", ios::binary);
char cLength[1024] = {0};
size_t 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 * buf = new char [fileSize];
// read data as a block:
file.read (buf,fileSize);
file.close(); itoa(fileSize, cLength,10);
send(sock, cLength, strlen(cLength)+1, 0);
//******************************************
//Send data
//******************************************
while ( fileSize )
{
int s = send(sock, buf, fileSize, 0);

if ( s > 0 )
{
fileSize -= s;
buf += s;
}
}
delete[] buf;

The recieving side
//Recieve size
char recvBuff[256]={0}; size_t fileSize = 0, r = 0;
recv(AcceptSock, recvBuff, sizeof(recvBuff), 0);
fileSize=atof(recvBuff);

//******************************************
//Recieve data
//******************************************
char *buf = new char[fileSize];
ofstream TheCopy("TheCopy.txt",ios::binary | ios::app);
while( fileSize )
{
memset(buf, 0, fileSize);
r=recv(AcceptSock, buf, sizeof(buf), 0);
TheCopy.write(buf, sizeof(buf));
if ( r > 0 )
{
fileSize -= r;
buf += r;
}
}
TheCopy.close();
delete[] buf;

henky@nok.co.id
August 13th, 2009, 01:26 AM
Just quick reply as I am not at my desk:
1. Try to send and receive length of file as long as integer size.
Use hton and ntoh for conversion of network-host byte order.
There is no guarantee TCP will not combine string of length and contents of file.
i.e. if you send "100" as length and "xxxaaaxx" as contents of file, your program may
receive "100xxa" and "aaxx".

2. Don't move your pointer inside while loop in receiving program such as
buf += r;
there may be memory access violation if loop more than once
at memset() and/or recv().

Rgds,

MasterDucky
August 14th, 2009, 06:56 AM
Thank you very much Henky for your help!

I will try to understand what you are saying.

>>1. Try to send and receive length of file as long as integer size.

I dont see how would that be possible? By adding a third send() and recv()?
But then i had to add a forth and a fifth...

>>There is no guarantee TCP will not combine string of length and contents of file.

This has already been told to me but it still doesnt make sense.

Because it has to stop recieve one time, otherwise it would hang there forever.

Im saying this because i managed to fix the code by adding Sleep() so it means it just stops recieving after something.

I guess its something like the send() function knows how many bytes it has to send
(because we have to precise it) and it gets transmitted to the recv function and when it recieved the number of bytes it stops recieving.