Getting File size from network byte order

  • Thread starter Thread starter Manu
  • Start date Start date
M

Manu

Hi,

Following code shows how a packet is sent from a client to a server's
socket. The file size (fileSizeTemp) is converted to network byte order
before sending to the server.

*((unsigned short *)&dataBuff[6 + filenameLen + 1]) = htons((unsigned
short)(fileSizeTemp >> 16));
*((unsigned short *)&dataBuff[6 + filenameLen + 3]) = htons((unsigned
short)((fileSizeTemp << 16) >> 16));

if (send(clientSock, ..., ...) == SOCKET_ERROR)
{
//...
}

My question is, how to get the size of the file (from the received packet)
on the server?

Thanks

Manu.
 
Manu said:
Hi,

Following code shows how a packet is sent from a client to a server's
socket. The file size (fileSizeTemp) is converted to network byte order
before sending to the server.

*((unsigned short *)&dataBuff[6 + filenameLen + 1]) = htons((unsigned
short)(fileSizeTemp >> 16));
*((unsigned short *)&dataBuff[6 + filenameLen + 3]) = htons((unsigned
short)((fileSizeTemp << 16) >> 16));

You could do it more simply with htonl :
*((unsigned long *)&dataBuff[6 + filenameLen + 1]) =
htonl(fileSizeTemp)
if (send(clientSock, ..., ...) == SOCKET_ERROR)
{
//...
}

My question is, how to get the size of the file (from the received packet)
on the server?
ntohl.

Arnaud
MVP - VC
 
Manu said:
Hi,

Following code shows how a packet is sent from a client to a server's
socket. The file size (fileSizeTemp) is converted to network byte order
before sending to the server.

*((unsigned short *)&dataBuff[6 + filenameLen + 1]) = htons((unsigned
short)(fileSizeTemp >> 16));
*((unsigned short *)&dataBuff[6 + filenameLen + 3]) = htons((unsigned
short)((fileSizeTemp << 16) >> 16));

You could do it more simply with htonl :
*((unsigned long *)&dataBuff[6 + filenameLen + 1]) =
htonl(fileSizeTemp)
if (send(clientSock, ..., ...) == SOCKET_ERROR)
{
//...
}

My question is, how to get the size of the file (from the received packet)
on the server?
ntohl.

Arnaud
MVP - VC
 
But there are 4 byes of data. How do I supply the netlong parameter to ntohl
from 4 bytes of data?

Manu.

Hi,

Following code shows how a packet is sent from a client to a server's
socket. The file size (fileSizeTemp) is converted to network byte order
before sending to the server.

*((unsigned short *)&dataBuff[6 + filenameLen + 1]) = htons((unsigned
short)(fileSizeTemp >> 16));
*((unsigned short *)&dataBuff[6 + filenameLen + 3]) = htons((unsigned
short)((fileSizeTemp << 16) >> 16));

You could do it more simply with htonl :
*((unsigned long *)&dataBuff[6 + filenameLen + 1]) =
htonl(fileSizeTemp)
if (send(clientSock, ..., ...) == SOCKET_ERROR)
{
//...
}

My question is, how to get the size of the file (from the received packet)
on the server?
ntohl.

Arnaud
MVP - VC
 
OK, I got it.

long fileSize = ntohl(((ULONG)dataBuff[6 + filenameLen + 4] << 24)
+ ((ULONG)dataBuff[6 + filenameLen + 3] << 16)
+ ((ULONG)dataBuff[6 + filenameLen + 2] << 8)
+ ((ULONG)dataBuff[6 + filenameLen + 1]));

That is all that is required.

Thanks Arnaud :-)

Manu.

Manu said:
But there are 4 byes of data. How do I supply the netlong parameter to
ntohl from 4 bytes of data?

Manu.

Hi,

Following code shows how a packet is sent from a client to a server's
socket. The file size (fileSizeTemp) is converted to network byte order
before sending to the server.

*((unsigned short *)&dataBuff[6 + filenameLen + 1]) = htons((unsigned
short)(fileSizeTemp >> 16));
*((unsigned short *)&dataBuff[6 + filenameLen + 3]) = htons((unsigned
short)((fileSizeTemp << 16) >> 16));

You could do it more simply with htonl :
*((unsigned long *)&dataBuff[6 + filenameLen + 1]) =
htonl(fileSizeTemp)
if (send(clientSock, ..., ...) == SOCKET_ERROR)
{
//...
}

My question is, how to get the size of the file (from the received packet)
on the server?
ntohl.

Arnaud
MVP - VC
 
Aww... those ULONGs should be long :)

Manu said:
OK, I got it.

long fileSize = ntohl(((ULONG)dataBuff[6 + filenameLen + 4] << 24)
+ ((ULONG)dataBuff[6 + filenameLen + 3] << 16)
+ ((ULONG)dataBuff[6 + filenameLen + 2] << 8)
+ ((ULONG)dataBuff[6 + filenameLen + 1]));

That is all that is required.

Thanks Arnaud :-)

Manu.

Manu said:
But there are 4 byes of data. How do I supply the netlong parameter to
ntohl from 4 bytes of data?

Manu.

Manu wrote:
Hi,

Following code shows how a packet is sent from a client to a server's
socket. The file size (fileSizeTemp) is converted to network byte
order
before sending to the server.

*((unsigned short *)&dataBuff[6 + filenameLen + 1]) = htons((unsigned

short)(fileSizeTemp >> 16));
*((unsigned short *)&dataBuff[6 + filenameLen + 3]) = htons((unsigned

short)((fileSizeTemp << 16) >> 16));

You could do it more simply with htonl :
*((unsigned long *)&dataBuff[6 + filenameLen + 1]) =
htonl(fileSizeTemp)


if (send(clientSock, ..., ...) == SOCKET_ERROR)
{
//...
}

My question is, how to get the size of the file (from the received
packet)
on the server?
ntohl.

Arnaud
MVP - VC
 
Manu said:
OK, I got it.

long fileSize = ntohl(((ULONG)dataBuff[6 + filenameLen + 4] << 24)
+ ((ULONG)dataBuff[6 + filenameLen + 3] << 16)
+ ((ULONG)dataBuff[6 + filenameLen + 2] << 8)
+ ((ULONG)dataBuff[6 + filenameLen + 1]));

long fileSize = ntohl(*(long*)(dataBuff+6+filenameLen+1));

As you've written it above you'll get the wrong answer if you compile the
code on a big-endian machine.

Similarly, on the server end you should be using htonl as Arnaud pointed
out.

-cd
 
Manu said:
OK, I got it.

long fileSize = ntohl(((ULONG)dataBuff[6 + filenameLen + 4] << 24)
+ ((ULONG)dataBuff[6 + filenameLen + 3] << 16)
+ ((ULONG)dataBuff[6 + filenameLen + 2] << 8)
+ ((ULONG)dataBuff[6 + filenameLen + 1]));

That is all that is required.

DO NOT mess with byte shifting and ordering when ntohl is doing exactly
that for you (and do it right!)

long* ptr=reinterpret_cast<long*>(dataBuff[6 + filenameLen + 1]);
long fileSize=ntohl(*ptr);

Arnaud
MVP - VC
 
Manu said:
OK, I got it.

long fileSize = ntohl(((ULONG)dataBuff[6 + filenameLen + 4] << 24)
+ ((ULONG)dataBuff[6 + filenameLen + 3] << 16)
+ ((ULONG)dataBuff[6 + filenameLen + 2] << 8)
+ ((ULONG)dataBuff[6 + filenameLen + 1]));

That is all that is required.


DO NOT mess with byte shifting and ordering when ntohl is doing exactly
that for you (and do it right!)

long* ptr=reinterpret_cast<long*>(dataBuff[6 + filenameLen + 1]);
long fileSize=ntohl(*ptr);

And note that the reinterpret_cast above is potential undefined
behaviour due to alignment issues, but is fortunately perfect well
defined for x86, which is unfussy about alignment (although you lose
atomicity and performance). Beware if you are using a non-x86 platform
which may be fussier about alignment, in which case you should use
memcpy to copy the 4 bytes into a ULONG before calling ntohl.

Tom
 
Oh well... Thanks a lot guys. That is a lot of information I got.

Manu.

Tom Widmer said:
Manu said:
OK, I got it.

long fileSize = ntohl(((ULONG)dataBuff[6 + filenameLen + 4] << 24)
+ ((ULONG)dataBuff[6 + filenameLen + 3] << 16)
+ ((ULONG)dataBuff[6 + filenameLen + 2] << 8)
+ ((ULONG)dataBuff[6 + filenameLen + 1]));

That is all that is required.


DO NOT mess with byte shifting and ordering when ntohl is doing exactly
that for you (and do it right!)

long* ptr=reinterpret_cast<long*>(dataBuff[6 + filenameLen + 1]);
long fileSize=ntohl(*ptr);

And note that the reinterpret_cast above is potential undefined behaviour
due to alignment issues, but is fortunately perfect well defined for x86,
which is unfussy about alignment (although you lose atomicity and
performance). Beware if you are using a non-x86 platform which may be
fussier about alignment, in which case you should use memcpy to copy the 4
bytes into a ULONG before calling ntohl.

Tom
 
Back
Top