Server disconnect event

Windows specific forum
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.

Hi,

Does anyone have some API code that allows a PB client program to detect when the server disconnects? Kind of like event #4 of the NetworkServerEvent() command, but for the client.

I searched the forums here but couldn't find anything.

Yours hopefully,
--
I used to be a nihilist but I don't believe in that any more.
(Win98first ed. + all updates, PB3.62, external editor)
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

Just write your own "Ping"-Routine, if the time for the server answer is over you can be nearly sure that the server connection is lost.

Cheers
Mike
Tranquil
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

Tranquil wrote:Just write your own "Ping"-Routine, if the time for the server answer is over you can be nearly sure that the server connection is lost.
Thanks, that's kind of what I use already (have a timeout which is reset on communications recieved). However, I am downloading a lot of small files on a pretty variable speed connection and would rather know immediately when the server disconnects because at the moment I have to set the timeout high (crappy line) so it makes for very slow progress.

Cheers.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Server disconnect:

Post by Fangbeast »

Gidday tin dude :) Pupil; told me how to do this..

In the main body of the code, put this

Code: Select all

ConnectionID = OpenNetworkConnection(ServerIPAddress, ServerPort)  ; Try to make the connection now
  
If ConnectionID ; If connect okay, Do these jobs
If WSAAsyncSelect_(ConnectionID, WindowID(#Window_chatbox), $9000, #FD_CLOSE) = #SOCKET_ERROR ; If this happened we'll now know when an server unexpectedly die on us
err.l = WSAGetLastError_() ; This line should be here even if we don't act on the result.
EndIf
the line that's important is the If WSAA etc etc

Now, in a callback to check the event..

Code: Select all

Procedure MyWindowCallback(WindowID, Message, wParam, lParam)
  Result = #PB_ProcessPureBasicEvents 
  If WindowID = WindowID(#yourwindowid)
    Select message
      Case $9000
       ;However you want to handle the event, put it here
Hope it helps some. Regards
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Re: Server disconnect:

Post by tinman »

Fangbeast wrote:Gidday tin dude :) Pupil; told me how to do this..
Thanks, I'll check it out tonight.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
amine_t
New User
New User
Posts: 2
Joined: Fri Feb 21, 2014 6:58 pm

Re: Server disconnect event

Post by amine_t »

Solution :

Code: Select all

InitNetwork()

Declare winproc(h,m,w,u)

If OpenWindow(0,0,0,100,100,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  SetWindowCallback(@winproc())
  con=OpenNetworkConnection("127.0.01",#Server_Port)
  If con
    Debug "Connected to server"
   ;since PB doesn't automatically recognize WSAAsyncSelect, we have to call it explicitly
    socket=OpenLibrary(#PB_Any,"Ws2_32.dll")
    ;Be careful to use ConnectionID(con) and not only "con"
    If CallFunction(socket,"WSAAsyncSelect",ConnectionID(con),WindowID(0),99999,$20)=-1  ;we send event 99999 to window
      err.l = WSAGetLastError_()
    EndIf
  EndIf
  CloseLibrary(socket)
EndIf
  
Repeat
  event=WaitWindowEvent()
Until event=#PB_Event_CloseWindow

Procedure winproc(handle,msg,wparam,uparam)
  If msg=99999
    MessageRequester("","Server disconnected")
  EndIf
  ProcedureReturn   #PB_ProcessPureBasicEvents
EndProcedure

Post Reply