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)
Server disconnect event
- tinman
- PureBasic Expert
- Posts: 1102
- Joined: Sat Apr 26, 2003 4:56 pm
- Location: Level 5 of Robot Hell
- Contact:
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.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.
Cheers.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
(WinXPhSP3 PB5.20b14)
- Fangbeast
- PureBasic Protozoa
- Posts: 4789
- Joined: Fri Apr 25, 2003 3:08 pm
- Location: Not Sydney!!! (Bad water, no goats)
Server disconnect:
Gidday tin dude
Pupil; told me how to do this..
In the main body of the code, put this
the line that's important is the If WSAA etc etc
Now, in a callback to check the event..
Hope it helps some. Regards

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
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
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
- tinman
- PureBasic Expert
- Posts: 1102
- Joined: Sat Apr 26, 2003 4:56 pm
- Location: Level 5 of Robot Hell
- Contact:
Re: Server disconnect:
Thanks, I'll check it out tonight.Fangbeast wrote:Gidday tin dude :) Pupil; told me how to do this..
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
(WinXPhSP3 PB5.20b14)
Re: Server disconnect event
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