Welcome !
It's not quite that simple. With a GUI, all GUI events must always be processed.
A server should be outsourced to a thread. To get started with PurerBasic but first another solution
to process the server events in the GUI event loop. To do this, the WaitWindowEvent is exited every 10ms to process the server events.
Here is something rough quickly put together for testing.
Update
Code: Select all
;-TOP
#ProgramTitle = "Main Window"
#ProgramVersion = "v1.01.2"
Enumeration Windows
#Main
EndEnumeration
Enumeration MenuBar
#MainMenu
EndEnumeration
Enumeration MenuItems
#MainMenuAbout
#MainMenuExit
EndEnumeration
Enumeration Gadgets
#MainList
EndEnumeration
Enumeration StatusBar
#MainStatusBar
EndEnumeration
Global Font1, Value, Event, Port, ServerEvent, ClientID, Quit, IsInit
Global *Buffer
Global Buf$
Procedure Logging(Text.s)
AddGadgetItem(#MainList, -1, Text)
SetGadgetState(#MainList, CountGadgetItems(#MainList) - 1)
SetGadgetState(#MainList, -1)
EndProcedure
Procedure InitServer()
Port = 6832
*Buffer = AllocateMemory(1024)
If CreateNetworkServer(0, Port, #PB_Network_IPv4 | #PB_Network_TCP)
Logging("PureBasic Server created ")
ProcedureReturn #True
Else
Logging("Error: PureBasic Server not created ")
ProcedureReturn #False
EndIf
EndProcedure
Procedure DoServerEvent()
Protected len
Repeat
Select NetworkServerEvent()
Case #PB_NetworkEvent_Connect
ClientID = EventClient()
Logging("A new client has connected !")
Case #PB_NetworkEvent_Data
ClientID = EventClient()
Logging("Client has send a packet !")
len = ReceiveNetworkData(ClientID, *Buffer, 1024)
Buf$ = PeekS(*Buffer, len, #PB_Ascii)
Logging("String: " + Buf$)
Case #PB_NetworkEvent_Disconnect
ClientID = EventClient()
Logging("Client "+ ClientID + " has closed the connection...")
Case #PB_NetworkEvent_None
Break
EndSelect
ForEver
EndProcedure
Procedure UpdateWindow()
Protected dx, dy
dx = WindowWidth(#Main)
dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
; Resize gadgets
ResizeGadget(#MainList, 0, 0, dx, dy)
EndProcedure
Procedure Main()
Protected dx, dy
#MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, #ProgramTitle , #MainStyle)
; Menu
CreateMenu(#MainMenu, WindowID(#Main))
MenuTitle("&File")
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
MenuItem(#PB_Menu_About, "")
CompilerElse
MenuItem(#MainMenuAbout, "About")
CompilerEndIf
; Menu File Items
CompilerIf Not #PB_Compiler_OS = #PB_OS_MacOS
MenuBar()
MenuItem(#MainMenuExit, "E&xit")
CompilerEndIf
; StatusBar
CreateStatusBar(#MainStatusBar, WindowID(#Main))
AddStatusBarField(#PB_Ignore)
; Gadgets
dx = WindowWidth(#Main)
dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
ListViewGadget(#MainList, 0, 0, dx, dy)
; Bind Events
BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
IsInit = InitServer()
Repeat
Select WaitWindowEvent(10) ;- Release Wait min 10 ms
Case #PB_Event_CloseWindow
Select EventWindow()
Case #Main
Break
EndSelect
Case #PB_Event_Menu
Select EventMenu()
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
Case #PB_Menu_About
PostEvent(#PB_Event_Menu, #Main, #MainMenuAbout)
Case #PB_Menu_Preferences
Case #PB_Menu_Quit
PostEvent(#PB_Event_CloseWindow, #Main, #Null)
CompilerEndIf
Case #MainMenuExit
PostEvent(#PB_Event_CloseWindow, #Main, #Null)
Case #MainMenuAbout
MessageRequester("About", #ProgramTitle + #LF$ + #ProgramVersion, #PB_MessageRequester_Info)
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
Case #MainList
Select EventType()
Case #PB_EventType_Change
;
EndSelect
EndSelect
EndSelect
DoServerEvent()
ForEver
If IsInit
CloseNetworkServer(0)
FreeMemory(*buffer)
EndIf
EndIf
EndProcedure : Main()