1) The network functions of PureBasic work very well.
However, a lot of editing still needs to be done. It is not the receive packet that is read out, but the receive buffer of the system. So you have to take care of the completeness of the data yourself. For example, if you send data of up to 65kb at a time via TCP/IP, it will not arrive in the receive buffer all at once. This is because the data in the network is split into different packets and must be reassembled in the programme. This is normal and therefore many protocols also use header information about the size of the data.
2) The PB network server works as listening and informs you via an event that a new client has connected. To manage the clients you should use MAP.
3) The server should not be processed in the main scope, but in a thread. Here you can also build a server event loop, where a delay of 10 ms takes the CPU load away.
4) Read at wiki how the protocol TCP/IP works. Also the meaning of the layers in the ISO model
Small base for server thread:
Code: Select all
Structure udtClientData
ConnectionID.i
Time.i
Date.i
Login.i
Name.s
Text.s
EndStructure
Structure udtServerData
*ThreadID
*ServerID
ExitServer.i
Map Client.udtClientData()
EndStructure
Global ExitApplication
Global ServerData.udtServerData
Procedure ThreadServer(*ServerData.udtServerData)
Protected Event, ConnectionID, keyConnectionID.s, count, Text.s, Name.s, ok, time
Protected len, *buffer
With *ServerData
time = ElapsedMilliseconds()
*buffer = AllocateMemory($FFFF)
Repeat
Event = NetworkServerEvent(\ServerID)
If Event
ConnectionID = EventClient()
keyConnectionID = Hex(ConnectionID)
EndIf
Select Event
Case #PB_NetworkEvent_Connect
If FindMapElement(\Client(), keyConnectionID)
;TODO
DeleteMapElement(\Client(), keyConnectionID)
EndIf
AddMapElement(\Client(), keyConnectionID)
\Client()\ConnectionID = ConnectionID
\Client()\Time = ElapsedMilliseconds()
\Client()\Date = Date()
;TODO
Case #PB_NetworkEvent_Data
len = ReceiveNetworkData(ConnectionID, *Buffer, $FFFF)
If FindMapElement(\Client(), keyConnectionID)
\Client()\Time = ElapsedMilliseconds()
;TODO
EndIf
Case #PB_NetworkEvent_Disconnect
If FindMapElement(\Client(), keyConnectionID)
;TODO
DeleteMapElement(\Client(), keyConnectionID)
EndIf
Case #PB_NetworkEvent_None
Delay(10)
EndSelect
Until \ExitServer
CloseNetworkServer(\ServerID)
\ThreadID = 0
\ServerID = 0
\ExitServer = 0
ClearMap(\Client())
FreeMemory(*buffer)
EndWith
EndProcedure