Jump to content
Sign in to follow this  
qwer85

My UDP server

Recommended Posts

I have UDP server, which wait's for client to connect, when it happens the server processes the packet and writes processed data to file. But when i connect simultaneously 5 clients - server logs only 2-3 of them. Why this happens?

Server:

Opt("MustDeclareVars", 1)

UDPStartup()
Global $socket = UDPBind("192.168.1.1", 65532)
If @error <> 0 Then Exit

Global $users[10][3]
For $i = 0 To UBound($users, 1) - 1
    For $k = 0 To UBound($users, 2) - 1
        $users[$i][$k] = 0
    Next
Next
Global $data

While 1
    $data = UDPRecv($socket, 1024)
    If $data <> "" Then
        If StringLeft($data, 4) = "qwer" Then
            Local $n_data = StringTrimLeft($data, 4)
            Local $IDnPass = StringSplit($n_data, "|")
            For $i = 0 To UBound($users) - 1
                Select
                    Case $users[$i][0] = 0
                        $users[$i][0] = $IDnPass[1]
                        $users[$i][1] = $IDnPass[2]
                        $users[$i][2] = TimerInit()
                        ExitLoop
                    Case $users[$i][0] = $IDnPass[1]
                        $users[$i][2] = TimerInit()
                        ExitLoop
                EndSelect
            Next
            _WriteData() ; write data into file
        EndIf
    EndIf
    For $i = 0 To UBound($users) - 1
        If $users[$i][0] <> 0 And TimerDiff($users[$i][2]) >= 10000 Then
            $users[$i][0] = 0
            $users[$i][1] = 0
            $users[$i][2] = 0
            _WriteData()
        EndIf
    Next
WEnd

Func _WriteData()
    Local $file = FileOpen("test.txt", 2)
    For $i = 0 To UBound($users) - 1
        If $users[$i][0] = 0 Then ContinueLoop
        FileWriteLine($file, $users[$i][0] & " | " & $users[$i][1] & " | " & TimerDiff($users[$i][2]))
    Next
    FileClose($file)
EndFunc

Func OnAutoItExit()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFuncoÝ÷ Ø)bz{ZºÚ"µÍÜ
    ][ÝÓ]ÝXÛUÉ][ÝËJBQÝ

BÛØ[  ÌÍÜÛØÚÙ]HQÜ[    ][ÝÌNLMKI][ÝË
MLÌBYÜ    ÉÝÈ[^]ÛØ[  ÌÍÜHH[ÛJKNNNKJBÛØ[    ÌÍÜÜÝÙH[ÛJLLLLLLLKNNNNNNNKJBÚ[HBQÙ[
    ÌÍÜÛØÚÙ] ][ÝÜ]Ù][ÝÈ [È ÌÍÜH [È ][Ýß  ][ÝÈ  [È ÌÍÜÜÝÙ
BTÛY
L
BÑ[[ÈÛ]]Ò]^]

BQÛÜÙTÛØÚÙ]
    ÌÍÜÛØÚÙ]
BQÚ]ÝÛ
B[[

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...