Check if the Network Connection still exists?

Just starting out? Need help? Post your questions and find answers here.
CadeX
Enthusiast
Enthusiast
Posts: 124
Joined: Mon Oct 02, 2006 2:56 pm
Location: Australia
Contact:

Check if the Network Connection still exists?

Post by CadeX »

I've always been curious of this, i know the server can just tell when a client disconnects, but is it possible for the client to tell if it has lost connection from the server without having to send any packets?

Thanks, Cad.
Pro-Gamer, Programmer, Pro-Grammer
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Try to send any String to the server, when SendnetworkData() fails (returning -1), the connection might be interrupted.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

If you want to achieve this without having to send anything, you can try make a timeout function... (or an inactivity timer).
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

You can also imitate the FTP potocol, sending a "NOOP" every x minutes/seconds. if the server does not react, the connection is interrupted.

Info: The NOOP-command is for keeping alive a FTP connecetion. A client can send it to the server, but some servers ignore NOOP and close the conenction after X minutes of inactivity.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

@Cadex:
maybe following routine i wrote years ago could help you, if i understand you right...

Code: Select all

; English forum: 
; Author: MrVainSCL
; Date: 19. January 2003


;Here is a normal version:


; ------------------------------------------------------------
;
; PureBasic Win32 API - CheckInternetConnection - Example v1.0
;
; by MrVainSCL! aka Thorsten   19/Jan/2003    PB v3.51+
;
; ------------------------------------------------------------
;
    If InternetGetConnectedState_(0, 0) 
      result$ = "Online"
    Else
      result$ = "Offline"
    EndIf
    ;
    MessageRequester("Are we connected to the Internet?","We are "+result$,0)
End
;
; ------------------------------------------------------------



;Here is a Procedure() version:


; ------------------------------------------------------------
;
; PureBasic Win32 API - CheckInternetConnection - Example v1.0
;
; by MrVainSCL! aka Thorsten   19/Jan/2003    PB v3.51+
;
; ------------------------------------------------------------
;
    Procedure MyCheckInternetConnection()
        If InternetGetConnectedState_(0, 0) 
          result = 1
        Else
          result = 0
        EndIf
        ;
        ProcedureReturn result
    EndProcedure
    ;
    ; -------- Check if user is connected to internet -------- 
    ;
    If MyCheckInternetConnection() = 1
      state$ = "Online"
    Else
      state$ = "Offline"
    EndIf
    ;
    MessageRequester("Are we connected to the Internet?","We are "+state$,0)
End
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

@Vain:

Your Procedures shows the current internet connection state. He wants to know if his client is connected to the server.

I think the only way is to include a "ping" routine, which is the savest one. This is the way many other applications do. Just send a short message to the server and wait for answer. If no answer comes in, then time out your connection or try to reconnect.
Tranquil
Post Reply