Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8621

Other programming languages • Re: Basic on Raspberry

$
0
0
I was able to write test code that checks the number of open connections.
On Raspberry PI after 1010 connections it was not possible to make a new connection until the current ones were closed.
Tested on my Windows PC with 10 thousand connections and works great.

Server test code.

Code:

#FileLog = 0CreateFile(#FileLog, "ServerLog.txt") ; Create an empty file for client log.Port = 6832ClientCount = 0If CreateNetworkServer(0, Port, #PB_Network_IPv4 | #PB_Network_TCP)  MessageRequester("PureBasic - Server", "Server created (Port "+Port+").", 0)    Repeat          ServerEvent = NetworkServerEvent()      If ServerEvent          ClientID = EventClient()            ClientInfo.s = " ID " + ClientID            If ServerEvent <> #PB_NetworkEvent_Disconnect        ClientInfo + " IP "+IPString(GetClientIP(ClientID))+" Port "+GetClientPort(ClientID)       EndIf            Select ServerEvent              Case #PB_NetworkEvent_Connect          ClientCount+1          WriteStringN(#FileLog, "A new client "+ClientCount+" has connected!"+ClientInfo)        Case #PB_NetworkEvent_Data                    ReceiveNetworkData(ClientID, @Buffer, 4)          WriteStringN(#FileLog, "Client has send a data "+Buffer+ClientInfo)          Buffer + 1000 ; We add a number to a variable.          SendNetworkData(ClientID, @Buffer, 4) ; Send 4 bytes from variable Buffer to the client.          Case #PB_NetworkEvent_Disconnect          ClientCount - 1          Debug "Disconnect "+ClientCount          WriteStringN(#FileLog, "Client has closed the connection..."+ClientInfo+"    Currently the number of connected clients "+ClientCount)          If ClientCount<=0 ; All clients have disconnected.            Break ; Break the main loop and thus end the execution of the application.          EndIf      EndSelect    EndIf      ForEver    MessageRequester("PureBasic - Server", "Click to quit the server.", 0)    CloseNetworkServer(0)Else  MessageRequester("Error", "Can't create the server (port in use ?).", 0)EndIfCloseFile(#FileLog)  End
Test client code.

Code:

IP.s = InputRequester("","Input IP Raspberry Pi", "")If ip = ""  EndEndIfPort = 6832CountConnetions = 1000     ; Number of connections.#FileLog=0CreateFile(#FileLog, "ClientLog.txt") ; Create an empty file for log.Dim ConnectID(CountConnetions)Dim ServerResponse(CountConnetions); Connecting to the server.For i = 1 To CountConnetions  ConnectID(i) = OpenNetworkConnection(IP, Port, #PB_Network_IPv4 | #PB_Network_TCP)  If ConnectID(i)    SendNetworkData(ConnectID(i), @i, 4) ;Send data to server.  Else    Debug "Can't find the server (Is it launched ?). "+i    WriteStringN(#FileLog, "Can't find the server (Is it launched ?). "+i)  EndIfNextDelay(2000) ; Wait 2 seconds.; Receiving data.For i = 1 To CountConnetions  If ConnectID(i)    If NetworkClientEvent(ConnectID(i)) = #PB_NetworkEvent_Data ; The client received data from the server.      ServerResponse(i) = 1 ; Check that the client has received the data.      ReceiveNetworkData(ConnectID(i), @Buff, 4)      If Buff-1000 <> i        WriteStringN(#FileLog, "Stream number "+i+" received incorrect data "+Buff)        Debug "Stream number "+i+" received incorrect data "+Buff      EndIf    EndIf  EndIfNextFor i = 1 To CountConnetions  If ServerResponse(i) <> 1    WriteStringN(#FileLog, "Stream number "+i+" did not receive data")    Debug "Stream number "+i+" did not receive data"  EndIfNext; Closing connection.For i = 1 To CountConnetions  If ConnectID(i)    CloseNetworkConnection(ConnectID(i))  EndIfNextMessageRequester("PureBasic - Client", "Client finished", 0)End
The archive contains compiled files for Windows and Raspberry PI (32 bit).
Client and server raspberry pi.7z

Statistics: Posted by Vasian — Tue Mar 25, 2025 11:16 pm



Viewing all articles
Browse latest Browse all 8621

Trending Articles