Restored from previous forum. Originally posted by freak.
Hi All,
When programming Network Stuff, you have to call the Network Event Functions permanently to get data. So you can't use WaitWindowEvent(), and therefore put a Delay(1) in your Loop.
Now I always disliked this Solution, and now, i've finally found a better one:
There is an API-call, that makes windows Send a Message to the WindowCallback Procedure each time a Network Event occurs.
The Trick now is, to send a #WM_NULL Message, which will be compleetly ignored, but which will still cause WaitWindowEvent() to
return a Null Value. And the best thing is, for this you don't even need a Callback Procedure. It can all be done with WaitWindowEvent().
This way, you can still use WaitWindowEvent() for the GUI Stuff, and Put your Network Event Stuff in the same Loop. So no Delay(1) is needed.
Here's a short example, that explains, how it's done:
Code: Select all
; ++++++++++++++++++++++++ Network Server Example +++++++++++++++++++++++++++++++
OpenWindow(0,0,0,300,80, "Network Server", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
StringGadget(0, 5, 5, 290, 25, "")
ButtonGadget(1, 5, 40, 80, 25, "Send")
DisableGadget(1,1)
If InitNetwork() = 0: End: EndIf ; Initialize Network Stuff
Socket = CreateNetworkServer(0,6000)
If Socket = 0: End: EndIf
; The following call will activate this Trick
;
; Socket : Socket (or Connection ID) is returned by CreateNetworkServer()
; WindowID() : ID of a Window, to send the Events to.
; #WM_NULL : The Message to be Send, if a Network Event occurs.
; #WM_NULL will be ignored, but still causes the WaitWindowEvent() to return,
; and then the NetworkEvent to be called.
; #FD_ALL : Event to be send to Callback Procedure, we request them all.
#FD_ALL = #FD_READ|#FD_WRITE|#FD_OOB|#FD_ACCEPT|#FD_CONNECT|#FD_CLOSE
WSAAsyncSelect_(Socket, WindowID(0), #WM_NULL, #FD_ALL)
;Main Loop
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
End
Case #PB_Event_Gadget
If EventGadget() = 1
text.s = GetGadgetText(0)
SendNetworkData(EventClient() , @text, Len(text)+1)
EndIf
EndSelect
Select NetworkServerEvent()
Case 1
DisableGadget(1,0) ; user connected, enable sending.
Case 4
DisableGadget(1,1) ; user disconnected, disable sending.
EndSelect
ForEver
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; ++++++++++++++++++++++++ Network Client Example +++++++++++++++++++++++++++++++
OpenWindow(0, 0, 0, 300, 400, "Network Client", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
ListViewGadget(1, 5, 5, 290, 390)
If InitNetwork()=0: End: EndIf ; Initialize Network Stuff
ConnID = OpenNetworkConnection("127.0.0.1",6000)
If ConnID = 0: End: EndIf
; The following call will activate this Trick
;
; ConnID : connection ID (from OpenNetworkConnection)
; WindowID() : ID of a Window, to send the Events to.
; #WM_NULL : The Message to be Send, if a Network Event occurs.
; #WM_NULL will be ignored, but still causes the WaitWindowEvent() to return,
; and then the NetworkEvent to be called.
; #FD_ALL : Event to be send to Callback Procedure, we request them all.
#FD_ALL = #FD_READ|#FD_WRITE|#FD_OOB|#FD_ACCEPT|#FD_CONNECT|#FD_CLOSE
WSAAsyncSelect_(ConnID, WindowID(0), #WM_NULL, #FD_ALL)
; Main Loop
Repeat
If WaitWindowEvent() = #PB_Event_CloseWindow
End
EndIf
If NetworkClientEvent(ConnID) = 2
Text.s = Space(500)
ReceiveNetworkData(ConnID, @Text, 500)
AddGadgetItem(1,-1,Text)
EndIf
ForEver
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Timo