How to capture last octect of an IP address in C++?

S

sonali.bhavsar

I want to parse the IP address data to capture the last octect. Are
there any socket calls which can be directly used?
 
J

Josh Mcfarlane

I want to parse the IP address data to capture the last octect. Are
there any socket calls which can be directly used?

C++ doesn't deal with sockets. You will have to refer to the newsgroup
for the operating system you are using.
 
V

Victor Bazarov

I want to parse the IP address data to capture the last octect. Are
there any socket calls which can be directly used?

Sockets, IP address, are off-topic here. You might want to post this
to a newsgroup for your platform, where network programming questions
usually belong.

If you just want to parse a string in the form "X.X.X.X" where 'X' is
an integer number, then then simplest way is to find the last '.' and
extract the number. Use 'strrchr' and 'strtol'.

V
 
M

Maxim Yegorushkin

I want to parse the IP address data to capture the last octect. Are
there any socket calls which can be directly used?

unsigned char last_ip4_octet(char const* addr)
{
unsigned char t[4];
if(4 != sscanf(addr, "%hhu.%hhu.%hhu.%hhu", t + 0, t + 1, t + 2, t
+ 3))
throw std::invalid_argument("bad ip address format");
return t[3];
}
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top