[Balug-talk] getsockname ?
Vinay S Kulkarni
vinay_sk at yahoo.com
Wed Aug 1 09:08:27 PDT 2001
Hi Urs,
Thanks for the response. My confusion was from
another program I ran where I did print out the
sin_port after applying ntohs() to it by in the
unsigned short port variable in my class I saved the
port without ntohs(). Silly as ut may sound, i did
overlook that for quite some time.
Best REgards,
Vinay
--- Urs Thuermann <urs at isnogud.escape.de> wrote:
> > sin_size = sizeof(my_addr);
> > if (getsockname(sockfd, (struct sockaddr
> *)&my_addr, &sin_size) == -1) {
> > perror("getsockname");
> > exit(1);
> > }
> > printf("My TCP port = %d\n",my_addr.sin_port);
>
>
> > The run looks like this:
> >
> > [root at vin /root]# /home/vinay/server
> > My TCP port = 62337
> >
> >
> >
> > And lsof gave me this:
> >
> > [root at vin /root]# lsof | grep "server.*TCP"
> > server 11037 vinkulkarni 3u IPv4 47013
>
> > TCP *:33267 (LISTEN)
> > [root at altair /root]#
> >
> >
> > If we look at the numbers then we see that the
> digits in both from
> > what the server prints and what lsof tells are the
> same except they
> > are jumbled up. This is purely coincidental. I got
> it only in this
> > run.
>
> It is coincidental that 62337 and 33267 have the the
> decimal digits
> just jumbled up. But look at the hexadecimal
> represantations of these
> numbers:
>
> 62337 = 0xF381
> 33267 = 0x81F3
>
> And this is *not* coincidental. The correct port
> number is 33267 = 0x81F3.
> When using system calls involving a struct
> sockaddr_in, the port number
> is always be stored in memory in network byte order,
> which is big
> endian. That means the most significant byte is
> stored at the lower
> memory address, followed the the less significant
> bytes. That is in
> memory the port number 0x81F3 is stored as
>
> 0x81 0xF3
>
> Intel x86 however are little endian, placing the
> least significant
> byte at the lower memory address. When the CPU
> reads the 2-byte value
> from memory, it uses 0x81 as the least significant
> byte and 0xF3 as
> the most significant byte, yielding 0xF381 = 62337.
> To get the
> correct port number, you have to swap the two bytes
> by using ntohs().
> Change your printf() as follows:
>
> printf("My TCP port = %d\n",
> ntohs(my_addr.sin_port));
>
> > The same code compiled on the ultra-10 that I work
> on runs just
> > fine.
>
> This is because Sparcs are big endian. That means
> that network byte
> order and host byte order are the same. The
> routines ntohs(),
> htons(), ntohl(), and htonl() are NOPs, i.e. the do
> nothing, while on
> Intel x86, which is little endian, they swap the
> byte order.
>
> To make your program portable, so that it works on
> litte endian and
> big endian machines you should always use ntohs(),
> etc.
>
>
> urs
=====
Vinay Kulkarni
The past is history. The future is a mystery. But this moment is a gift. That's why it's called the 'present'.
__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/
More information about the Balug-talk-balug.org
mailing list