Page 1 of 1

Posted: Sun Mar 30, 2003 10:58 pm
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)

Posted: Tue May 06, 2003 4:41 pm
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

Posted: Tue May 06, 2003 6:24 pm
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.

Server disconnect:

Posted: Wed May 07, 2003 8:01 am
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

Re: Server disconnect:

Posted: Wed May 07, 2003 8:48 am
by tinman
Fangbeast wrote:Gidday tin dude :) Pupil; told me how to do this..
Thanks, I'll check it out tonight.

Re: Server disconnect event

Posted: Fri Feb 21, 2014 7:04 pm
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