Jump to content

TCP Server


Recommended Posts

I'm creating a TCP server and I have a problem. At the same time a client connects it disconnects now. If someone can tell me where is the problem!

#include <Array.au3>
#include "functions.au3"

TCPStartup()

global $Clients[0][2]
global $OnlineUsers
global $Listen

$Listen = TCPListen(@IPAddress1, 50000, 20)

while 1
   Clean()
   Proccess()
   Accept()
wend

func Clean()
   local $i
   for $i = UBound($Clients) - 1 to 0 step -1
      TCPSend($Clients[$i][0], 'test')
      if @error then
         TrayTip('SERVER', 'DISCONNECTED CLIENT', 5)
         TCPCloseSocket($Clients[$i][0])
         _ArrayDelete($Clients, $i)
      endif
   next
endfunc

func Proccess()
    local $i
    for $i = UBound($Clients) - 1 to 0 step -1
    local $Recv = TCPRecv($Clients[$i][0], 256)
        if $Recv then
            if StringSplit($Recv, ';')[1] = 'login' then
                if correctInfo(StringSplit($Recv, ';')[2], StringSplit($Recv, ';')[3]) then 
                    if not isOnline(StringSplit($Recv, ';')[2], $OnlineUsers) then 
                        TCPSend($Clients[$i][0], 'success')
                        $Clients[$i][1] = StringSplit($Recv, ';')[2]
                    else 
                        TCPSend($Clients[$i][0], 'already_online')
                    endif 
                else
                    TCPSend($Clients[$i][0], 'wrong_info')
                endif 

            elseif StringSplit($Recv, ';')[1] = 'register' then
                if not userExists(StringSplit($Recv, ';')[2]) then 
                    regUser(StringSplit($Recv, ';')[2], StringSplit($Recv, ';')[3])
                    TCPSend($Clients[$i][0], 'success')
                else
                    TCPSend($Clients[$i][0], 'user_exists')
                endif
            endif
            MsgBox(0, 'SERVER', $Clients[$i][1] & ' send ' & $recv)
      endif
   next
endfunc

func Accept()
   local $Socket = TCPAccept($Listen)
   if $Socket <> -1 then
      TrayTip('SERVER', 'NEW CLIENT', 5)
      _ArrayAdd($Clients, 0)
      redim $Clients[UBound($Clients) + 1][2]
      $Clients[UBound($Clients) - 1][0] = $Socket
      $Clients[UBound($Clients) - 1][1] = 'null'
   endif
endfunc
Edited by dromenox
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...