Server:
Code: Alles auswählen
; English forum:
; Author: freak (updated for PB4.00 by blbltheworm)
; Date: 21. January 2003
; OS: Windows
; Demo: No
; ++++++++++++++++++++++++ Network Server Example +++++++++++++++++++++++++++++++
OpenWindow(0,0,0,300,80, "Network Server", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
;von freak
; ++++++++++++++++++++++++ Network Server Example +++++++++++++++++++++++++++++++
CreateGadgetList(WindowID(0))
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.l = CreateNetworkServer(#PB_Any,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
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; IDE Options = PureBasic v4.00 (Windows - x86)
; Folding = -
Code: Alles auswählen
; English forum:
; Author: freak (updated for PB4.00 by blbltheworm)
; Date: 21. January 2003
; OS: Windows
; Demo: No
; ++++++++++++++++++++++++ Network Client Example +++++++++++++++++++++++++++++++
OpenWindow(0, 0, 0, 300, 400, "Network Client", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CreateGadgetList(WindowID(0))
ListViewGadget(1, 5, 5, 290, 390)
If InitNetwork()=0: End: EndIf ; Initialize Network Stuff
ConnID.l = 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
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; IDE Options = PureBasic v4.00 (Windows - x86)
; Folding = -
Im Code steht doch das der Befehl WSAAsyncSelect bei einem auftretenden Netzwerkevent ein #WM_NULL an das Fenster sendet, und somit das Netzwerkevent verarbeitet wird.
Warum funktioniert das nicht?
Nachtrag:
Ich habe mir die WindowEvent's und die ServerEvent's im Debuger anzeigen lassen. Dabei ist mir aufgefallen das nur beim ersten Server- / ClientEvent ein #WM_NULL gesendet wird. Wenn ich statt des WSAAsyncSelect Befehls einen Timeout bei WaitWindowEvent() angebe, klappt alles wunderbar.