Please help!

  • Thread starter =?iso-8859-1?q?Fredrik_H=E5kansson?=
  • Start date
?

=?iso-8859-1?q?Fredrik_H=E5kansson?=

Hello!

I'm a beginner and are struggling to learn.

I'm on Linux and i know that i have an IP address in the member
variable RealHostAddr.sin.sin_addr.s_addr in HEX format. However i'm not
sure if this is a char[], long or what it is.

I have tried to have the contents of RealHostAddr.sin.sin_addr.s_addr to
be put into syslog.

I have tried the following:
syslog(LOG_DEBUG,"%s",(char *)RealHostAddr.sin.sin_addr.s_addr);
and
syslog(LOG_DEBUG,"%s",(char *)RealHostAddr.sin.sin_addr.s_addr, 10);
and
syslog(LOG_DEBUG,"%d",(int)RealHostAddr.sin.sin_addr.s_addr, 10);
and
syslog(LOG_DEBUG,"%d",(long)RealHostAddr.sin.sin_addr.s_addr, 10);

And many other but nothing seems to work. I have reasons to believe that
the IP is stored backwards so that 192.168.20.50 is 50.20.168.192. So i
also later want to use ntohl().

Fredrik
 
J

Jens.Toerring

Fredrik Håkansson said:
I'm on Linux and i know that i have an IP address in the member
variable RealHostAddr.sin.sin_addr.s_addr in HEX format. However i'm not
sure if this is a char[], long or what it is.
I have tried to have the contents of RealHostAddr.sin.sin_addr.s_addr to
be put into syslog.

Sorry, but you're rather off-topic here since your problem is not
really one about the language C but some details of platform spe-
cific networking functions etc. which aren't even mentioned in the
C standard. So you will have a lot more success with getting answers
to your question when you would post them to e.g. comp.unix.programmer
or comp.os.linux.development.apps, where they would be completely
on-topic.
Regards, Jens
 
?

=?iso-8859-1?q?Fredrik_H=E5kansson?=

Fredrik Håkansson said:
I'm on Linux and i know that i have an IP address in the member
variable RealHostAddr.sin.sin_addr.s_addr in HEX format. However i'm not
sure if this is a char[], long or what it is.
I have tried to have the contents of RealHostAddr.sin.sin_addr.s_addr to
be put into syslog.

Sorry, but you're rather off-topic here since your problem is not
really one about the language C but some details of platform spe-
cific networking functions etc. which aren't even mentioned in the
C standard. So you will have a lot more success with getting answers
to your question when you would post them to e.g. comp.unix.programmer
or comp.os.linux.development.apps, where they would be completely
on-topic.
Regards, Jens

OK!

Thanks!
 
A

Alan Balmer

Hello!

I'm a beginner and are struggling to learn.

I'm on Linux and i know that i have an IP address in the member
variable RealHostAddr.sin.sin_addr.s_addr in HEX format. However i'm not
sure if this is a char[], long or what it is.

You've been told elsethread that this question is offtopic, but there
is a C question hidden here. The proper answer to your question is
that you don't _want_ to know what type the s_addr is. You should
consider it an opaque structure, subject to change. Knowing what it
"really" is can only get you in trouble. The API you're using provides
functions to access the contents.

<OT> Do you have access to W. Richard Stevens "Unix Network
Programming" Volume 1? It's a bit pricey, but it's the bible for this
 
S

SM Ryan

# I'm on Linux and i know that i have an IP address in the member
# variable RealHostAddr.sin.sin_addr.s_addr in HEX format. However i'm not
# sure if this is a char[], long or what it is.

# And many other but nothing seems to work. I have reasons to believe that
# the IP is stored backwards so that 192.168.20.50 is 50.20.168.192. So i
# also later want to use ntohl().

Different machines stores bytes in different orders. If you have 4 byte
longs and a union like
union {long i; char c[4];} u;
and then do
u.i = 0x61626364;
printf("%.4s\n",u.c);
sometimes you'll get ABCD, sometimes DCBA, or, if you still have PDP-11
around, BADC.

u.i = ntohl(0x61626364);
puts the bytes in the same order on every machine so that
printf("%.4s\n",u.c);
is ABCD. htonl is the inverse function of ntohl.
 
?

=?iso-8859-1?q?Fredrik_H=E5kansson?=

# I'm on Linux and i know that i have an IP address in the member
# variable RealHostAddr.sin.sin_addr.s_addr in HEX format. However i'm not
# sure if this is a char[], long or what it is.

# And many other but nothing seems to work. I have reasons to believe that
# the IP is stored backwards so that 192.168.20.50 is 50.20.168.192. So i
# also later want to use ntohl().

Different machines stores bytes in different orders. If you have 4 byte
longs and a union like
union {long i; char c[4];} u;
and then do
u.i = 0x61626364;
printf("%.4s\n",u.c);
sometimes you'll get ABCD, sometimes DCBA, or, if you still have PDP-11
around, BADC.

u.i = ntohl(0x61626364);
puts the bytes in the same order on every machine so that
printf("%.4s\n",u.c);
is ABCD. htonl is the inverse function of ntohl.

Thanks your explanation was great it helped me to understand and to
complete my code.

Fredrik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top