I also showed how to get public IP address of your home router in C++ with a library, libcurl.
Now I am going to combine them.
This article shows you how to make an email client that send the public IP address of your Router to your email account in C++.
The source code above shows you three files, stdafx.h, config.hpp and send_publicip.cpp.
I couldn't show the "#include" line in the one screen shot but it shouldn't be hard to figure out for anybody.
I am not going to repeat the source code of classes: Socket, MyCURL, MyOpenSSL and MySMTP. You can see them from my previous articles linked above.
As a fan of C++ namespace, the file, config.hpp, has a little strange looking namespace structure. When variable names conflict, I will have to type-in namespace name as well but most of time this trick improves readability of source code.
If you understood previous articles, this source code shouldn't be difficult to understand.
This main code does:
- initialize global setup for libraries of OpenSSL and cUrl.
- it sets URL, User ID and User Password of my home router.
- once it got the HTML body, it looks for the IP address number.
- sometimes the function, "RequestGet", returns not valid body data so I need to carefully check each steps of searching IP address.
- once I got the IP address, it is printed out to the screen and
- it sends an email with the retrieved Public IP address of my home router.
The Makefile above shows that it is using three libraries: boost, curl and openssl.
I am still not sure why the first access is always rejected. I think it has something to do with time out or something. But as a workaround, if I call "curl.RequestGet()" twice, it seems to be working better.
ReplyDelete