detecting connection

This is a discussion on detecting connection within the Linux Programming forums, part of the Platform Specific Boards category; First, I want to apologise about my english, itīs realy bad. I hope that it will be clear enought for ...

  1. #1
    zly
    zly is offline
    Registered User
    Join Date
    Feb 2005
    Posts
    2

    Question detecting connection

    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.

  2. #2
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    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?
    Monday - what a way to spend a seventh of your life

  3. #3
    zly
    zly is offline
    Registered User
    Join Date
    Feb 2005
    Posts
    2
    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.
    Thanks anyway.

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    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.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple forks in one client connection (ftpclient)
    By Dynamo in forum Networking/Device Communication
    Replies: 5
    Last Post: 01-16-2011, 11:41 AM
  2. Replies: 6
    Last Post: 12-19-2008, 04:49 PM
  3. passing a connection to another process.
    By Kinasz in forum Networking/Device Communication
    Replies: 3
    Last Post: 10-09-2004, 05:34 PM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. Detecting (internet) connection status
    By Coder in forum Windows Programming
    Replies: 9
    Last Post: 03-16-2002, 04:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21