First, I want to apologise about my english, itīs realy bad. I hope that it will be clear enought for understending essence of my problem. Iīm a new in Linux programming and Iīm trying to improve that. My little program has to detect whether is system connected on the interner or not. I supose that there is simple solution (in Win there is RAS library, but in Lin I donīt know where to look). I would appreciate if someone could answer me. Thanks.
You could ping the system you want to connect to. Why do you want the application to connect to the internet? Will it connect to a custom server/service or a known one?
It wonīt connect to any server. It just has to perform some stuff while user is on net. I tried this line:
if(!system("ping -c 1 www.google.com")) ... //sistem is online
But itīs quite unelegant (server could be crushed...) and detection is too slow. If someone know for some Linux API function or something similar, that would be great.
Use sockets to try and make a connec()tion to an outside server. You could I guess check to see if they have everything set up for internet, but that doesn't mean it is going to work, so you will have to see if you can reach outside of the system.
Use the gethostbyname() function, and try to resolve a common domain (Like yahoo.com). If it fails, then you can conclude one of the following:
1) yahoo.com is down (unlikely)
2) Internet connection is down
3) Internet connection is up, but DNS server is down