By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,838 Members | 1,668 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,838 IT Pros & Developers. It's quick & easy.

Getting File size from network byte order

P: n/a
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.

Nov 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

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

Nov 17 '05 #2

P: n/a

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

Nov 17 '05 #3

P: n/a
But there are 4 byes of data. How do I supply the netlong parameter to ntohl
from 4 bytes of data?

Manu.

<ad******@club-internet.fr> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

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

Nov 17 '05 #4

P: n/a
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" <jo*****@knowledgepointsolutions.com> wrote in message
news:uI**************@TK2MSFTNGP09.phx.gbl...
But there are 4 byes of data. How do I supply the netlong parameter to
ntohl from 4 bytes of data?

Manu.

<ad******@club-internet.fr> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

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


Nov 17 '05 #5

P: n/a
Aww... those ULONGs should be long :)

"Manu" <jo*****@knowledgepointsolutions.com> wrote in message
news:um**************@TK2MSFTNGP12.phx.gbl...
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" <jo*****@knowledgepointsolutions.com> wrote in message
news:uI**************@TK2MSFTNGP09.phx.gbl...
But there are 4 byes of data. How do I supply the netlong parameter to
ntohl from 4 bytes of data?

Manu.

<ad******@club-internet.fr> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

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



Nov 17 '05 #6

P: n/a
Manu wrote:
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

Nov 17 '05 #7

P: n/a

Manu wrote:
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

Nov 17 '05 #8

P: n/a
ad******@club-internet.fr wrote:
Manu wrote:
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
Nov 17 '05 #9

P: n/a
Oh well... Thanks a lot guys. That is a lot of information I got.

Manu.

"Tom Widmer" <to********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
ad******@club-internet.fr wrote:
Manu wrote:
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

Nov 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.