Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / February 2005

Tip: Looking for answers? Try searching our database.

Getting File size from network byte order

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Manu - 22 Feb 2005 11:14 GMT
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.
adebaene@club-internet.fr - 22 Feb 2005 12:48 GMT
> Hi,
>
[quoted text clipped - 3 lines]
>
> *((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)
> {
[quoted text clipped - 3 lines]
> My question is, how to get the size of the file (from the received packet)
> on the server?
ntohl.

Arnaud
MVP - VC
adebaene@club-internet.fr - 22 Feb 2005 12:48 GMT
> Hi,
>
[quoted text clipped - 3 lines]
>
> *((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)
> {
[quoted text clipped - 3 lines]
> My question is, how to get the size of the file (from the received packet)
> on the server?
ntohl.

Arnaud
MVP - VC
Manu - 22 Feb 2005 13:29 GMT
But there are 4 byes of data. How do I supply the netlong parameter to ntohl
from 4 bytes of data?

Manu.

>> Hi,
>>
[quoted text clipped - 26 lines]
> Arnaud
> MVP - VC
Manu - 22 Feb 2005 13:55 GMT
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.

> But there are 4 byes of data. How do I supply the netlong parameter to
> ntohl from 4 bytes of data?
[quoted text clipped - 31 lines]
>> Arnaud
>> MVP - VC
Manu - 22 Feb 2005 13:59 GMT
Aww... those ULONGs should be long :)

> OK, I got it.
>
[quoted text clipped - 44 lines]
>>> Arnaud
>>> MVP - VC
Carl Daniel [VC++ MVP] - 22 Feb 2005 14:54 GMT
> 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

adebaene@club-internet.fr - 22 Feb 2005 15:18 GMT
> OK, I got it.
>
[quoted text clipped - 4 lines]
>
> 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
Tom Widmer - 22 Feb 2005 16:40 GMT
>>OK, I got it.
>>
[quoted text clipped - 10 lines]
> 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
Manu - 23 Feb 2005 05:08 GMT
Oh well... Thanks a lot guys. That is a lot of information I got.

Manu.

>>>OK, I got it.
>>>
[quoted text clipped - 19 lines]
>
> Tom

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.