Page 1 of 1

Check if the Network Connection still exists?

Posted: Fri Dec 15, 2006 5:41 am
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.

Posted: Fri Dec 15, 2006 1:19 pm
by AND51
Try to send any String to the server, when SendnetworkData() fails (returning -1), the connection might be interrupted.

Posted: Fri Dec 15, 2006 1:31 pm
by Inf0Byt3
If you want to achieve this without having to send anything, you can try make a timeout function... (or an inactivity timer).

Posted: Fri Dec 15, 2006 1:38 pm
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.

Posted: Sat Dec 16, 2006 2:26 am
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

Posted: Sat Dec 16, 2006 4:18 pm
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.