Calculating subnet wiht netmask...

S

sam

Hi,

This is a C programming specific with TCP/IP relation.
Supposed I have two string (char *) variables with the following assignment:
ip_str = "10.1.2.3"
netmask_str = "255.255.0.0"

How can I use C programming technique to get the subnet 10.1.0.0?

I tried to use "struct in_addr", but not sure how to start writing a
simple code for it?

Is there any simple C code that I can follow in this regard?

Thanks
Sam
 
V

Victor Bazarov

sam said:
This is a C programming specific with TCP/IP relation.

This is a C++ newsgroup, and TCP/IP is off-topic.

However, if you have 2 strings, where one is an encoded number
and the other is an encoded mask, you need to

(a) convert the number into an unsigned long
(b) convert the mask into an unsigned long
(c) perform bitwise AND on them
(d) convert the result back into a string

Use 'sscanf' to convert numbers from a string, and 'sprintf' to
convert back.

V
 

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
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top