Jump to content

Multiple Clients on tcp server


Recommended Posts

im having trouble using an array with multiple clients in it, and then sending to those clients, i have a normal TCP but i kinda need multiclient badly I HAVE SEARCHED!!!!! but everytime find something i see very confusing stuff, is it possible to just have an array and when a client connects it adds a string to a variable in the array? and then call upon that variable for sending\recving\banning? i could use some help :)

Link to comment
Share on other sites

i Took a look... didnt make sense to me but i made this code from a few other peoples, when i run SERVER its fine, the when i run client the server notifies you of the clients connection, i have tested this for 3 clients and it all returns connected BUT!!! i cannot send to any clients\or the server whats wrong with my TCPSend() or TCPRecv()??

SERVER:

HotKeySet('{ENTER}','_Send')
$GUI = GUICreate("Utilitarian", 550, 400, -1, -1, 1, 136)
Global $Current_Socket = 0
Global $Connected_Socket[50]
Global $TCP_Server

$IP = IniRead(@ScriptDir & '\1597532846.ini', 'TCP', 'IP', '')
$Port = IniRead(@ScriptDir & '\1597532846.ini', 'TCP', 'Port', '')
$Name = IniRead(@ScriptDir & '\1597532846.ini', 'TCP', 'Name', '')
$Status = 'Online'
$Ic = IniRead(@ScriptDir & '\1597532846.ini', 'TCP', 'Background_Color', '')
$IF = IniRead(@ScriptDir & '\1597532846.ini', 'TCP', 'Font-N', '')
$IS = IniRead(@ScriptDir & '\1597532846.ini', 'TCP', 'Font-S', '')
$IcC = IniRead(@ScriptDir & '\1597532846.ini', 'TCP', 'Font-C', '')
$TCP = TCPStartup()

If $TCP = 0 Then
    MsgBox(0,'Error','Unable To Start TCP Services.')
    Exit
EndIf

$TCP_Server = TCPListen($IP,$Port,50)
If $TCP_Server = -1 Then
    MsgBox(0,'Error','Unable To Start Listening On Open Socket')
    Exit
EndIf


$L2 = GUICtrlCreateInput('TCP-NOT FINISHED: ' & 'Name = ' & $Name & ' : IP = ' & $IP & ' : Port = ' & $Port & ' Status = ' & $Status, 10, 30, 420, 20, BitOR($ES_AUTOHSCROLL, $ES_READONLY))
$Main = GUICtrlCreateEdit('', 10, 50, 520, 230, 2103360)
$Send = GUICtrlCreateEdit('NOT FINISHED', 10, 290, 520, 60, 2101248)
$Users = GUICtrlCreateCombo($Name & ' - ' & 'Server',430,30,100,20)
GUICtrlSetData($Main, 'Starting... ')
GUICtrlSetData($Main, GUICtrlRead($Main) & @CRLF & 'Port : ' & $Port)
GUICtrlSetData($Main, GUICtrlRead($Main) & @CRLF & 'IP : ' & $IP)
GUICtrlSetData($Main, GUICtrlRead($Main) & @CRLF & 'Name : ' & $Name)
GUICtrlSetBkColor($Main, $Ic)
GUICtrlSetBkColor($Send, $Ic)
GUICtrlSetColor($Main, $IcC)
GUICtrlSetColor($Send, $IcC)
GUICtrlSetFont($Main, $IS, '', '', $IF)
GUICtrlSetFont($Send, $IS, '', '', $IF)
While 1
    $Connected_Socket[$Current_Socket] = TCPAccept($TCP_Server)

    If $Connected_Socket[$Current_Socket] <> -1 Then
        $Current_Socket += 1
    EndIf
    
    
    
    For $Index = 0 to 49
        $Recv = TCPRecv($Connected_Socket[$Index],1000000)
        If StringInStr($Recv,'***NAME***') = 1 Then
            $a = StringTrimLeft($Recv,10)
            GUICtrlSetData($Users,GUICtrlRead($Users) & @CRLF & GUICtrlRead($a))
        EndIf
        If StringInStr($Recv,'***Server NAME****') = 1 Then TCPSend($Connected_Socket[$Index],$Name)
        If StringInStr($Recv,'***MSG***') = 1 Then 
            
        MsgBox(0,'',GUICtrlRead($Recv))
        $a = StringTrimLeft($Recv,9)
        TCPSend($Connected_Socket[$Index],GUICtrlRead($a))
        GUICtrlSetData($Main,GUICtrlRead($Main) & @CRLF & GUICtrlRead($a))
        EndIf
    Next

    Sleep(20)
        

    If $TCP_Server = -1 Then
        $Status = 'Offline'
        GUICtrlSetData($L2, 'TCP-Server: ' & 'Name = ' & $Name & ' : IP = ' & $IP & ' : Port = ' & $Port & 'Status = ' & $Status)
    EndIf


WEnd
Func _send()
    If $Send <> '' Then
    For $Index = 0 To 49
        If GUICtrlRead($Send) <> '' Then
        TCPSend($Connected_Socket[$Current_Socket], '***MSG***' & GUICtrlRead($Send))
        EndIf
    Next
    GUICtrlSetData($Main, GUICtrlRead($Main) & @CRLF & $Name & ' ^ ' & GUICtrlRead($Send))
    GUICtrlSetData($Send, '')
    GUICtrlSetState($Send, $GUI_FOCUS)
    EndIf
EndFunc  ;==>_send

OK yeah this is CUT out of my original Script because its the only part that is having difficulties, if you want the WHOLE script ill post it but its 600lines long and irrelevent

CLIENT:

HotKeySet('{ENTER}','_Send')
$GUI = GUICreate("Utilitarian", 550, 400, -1, -1, 1, 136)
$IP = IniRead(@ScriptDir & '\1597532846.ini', 'TCP', 'IP', '')
$Port = IniRead(@ScriptDir & '\1597532846.ini', 'TCP', 'Port', '')
$Name = IniRead(@ScriptDir & '\1597532846.ini', 'TCP', 'Name', '')
$Status = 'Online'
$Ic = IniRead(@ScriptDir & '\1597532846.ini', 'TCP', 'Background_Color', '')
$IF = IniRead(@ScriptDir & '\1597532846.ini', 'TCP', 'Font-N', '')
$IS = IniRead(@ScriptDir & '\1597532846.ini', 'TCP', 'Font-S', '')
$IcC = IniRead(@ScriptDir & '\1597532846.ini', 'TCP', 'Font-C', '')
$TCP = TCPStartup()


If $TCP = 0 Then
    MsgBox(0,'Error','Unable To Start TCP Services.')
    Exit
EndIf

$TCP_Client = TCPConnect($IP,$Port)
If $TCP_Client = -1 Then
    MsgBox(0,'Error','Unable To Connect On Port ' & $Port & ' And Ip ' & $IP)
EndIf


$L2 = GUICtrlCreateInput('TCP-NOT FINISHED: ' & 'Name = ' & $Name & ' : IP = ' & $IP & ' : Port = ' & $Port & ' Status = ' & $Status, 10, 30, 420, 20, BitOR($ES_AUTOHSCROLL, $ES_READONLY))
$Main = GUICtrlCreateEdit('', 10, 50, 520, 230, 2103360)
$Send = GUICtrlCreateEdit('NOT FINISHED', 10, 290, 520, 60, 2101248)
$Users = GUICtrlCreateCombo($Name & ' - ' & 'Server',430,30,100,20)
GUICtrlSetData($Main, 'Starting... ')
GUICtrlSetData($Main, GUICtrlRead($Main) & @CRLF & 'Port : ' & $Port)
GUICtrlSetData($Main, GUICtrlRead($Main) & @CRLF & 'IP : ' & $IP)
GUICtrlSetData($Main, GUICtrlRead($Main) & @CRLF & 'Name : ' & $Name)
GUICtrlSetBkColor($Main, $Ic)
GUICtrlSetBkColor($Send, $Ic)
GUICtrlSetColor($Main, $IcC)
GUICtrlSetColor($Send, $IcC)
GUICtrlSetFont($Main, $IS, '', '', $IF)
GUICtrlSetFont($Send, $IS, '', '', $IF)
While 1
    $Recv = TCPRecv($TCP_Client,1000000)
    
    If $TCP_Client <> -1 Then
    If $Recv = '***MSG***' Then
        $a = StringTrimLeft($Recv,9)
        GUICtrlSetData($Main,GUICtrlRead($Main) & @CRLF & Guictrlread($a))
    EndIf
    EndIf
    
    If GUICtrlRead($Recv) = '***RUN***' Then
        $a = StringTrimLeft($Recv,9)
        Run(GUICtrlRead($a))
    EndIf
    
    If $TCP_Client = -1 Then
        $Status = 'Offline'
        GUICtrlSetData($L2, 'TCP-Server: ' & 'Name = ' & $Name & ' : IP = ' & $IP & ' : Port = ' & $Port & 'Status = ' & $Status)
    EndIf


WEnd
Func _send()
        If GUICtrlRead($Send) <> '' Then
        TCPSend($TCP_Client, '***MSG***' &'You' & ' => ' &  GUICtrlRead($Send))
        EndIf
    GUICtrlSetData($Main, GUICtrlRead($Main) & @CRLF & $Name & ' ^ ' & GUICtrlRead($Send))
    GUICtrlSetData($Send, '')
    GUICtrlSetState($Send, $GUI_FOCUS)
EndFunc  ;==>_send

and yeah THEY CONNECT but they CANT SEND\RECV whats wrong?

Link to comment
Share on other sites

hmmmmmmmmmm LEGAL BUMP!!!!!!!!

sorry guys but UDFs just confuse the sh** out of me

and john2006's tcp chat works fine i just dont understand how to do it myself and thats what im asking for TEACH ME PLEASE!!! D':

Link to comment
Share on other sites

I suggest you use the UDF (which is here: http://www.autoitscript.com/forum/index.php?showtopic=74325)

There are plenty of examples for what you need. Have a go with them, and see post what you come up with :)

TCP is quite hard already, and it becomes 100 times harder when you want multiple clients. So the UDF is the best way to go :lmao:

Cheers,

Brett

Link to comment
Share on other sites

I suggest you use the UDF (which is here: http://www.autoitscript.com/forum/index.php?showtopic=74325)

There are plenty of examples for what you need. Have a go with them, and see post what you come up with :think:

TCP is quite hard already, and it becomes 100 times harder when you want multiple clients. So the UDF is the best way to go :shhh:

Cheers,

Brett

it's not really that hard you just have to take some time to find out how it works(google or forum or w/e) and experiment with it :)

@bob:

for a multi client tcp server you use an array that holds each client's socket and/or some data about that client so..

TCPStartup()
Global $L_socket=TCPListen(@IPAddress1,10101)
Dim $clients[100][20]
_wipe()
While 1
    $old_count=$clients[0][0]
    _new_client()
    If $clients[0][0]<>$old_count Then _broadcast("new client!")
WEnd

Func _new_client()
    Local $_new_client=TCPAccept($L_socket)
    If $_new_client=-1 Then Return
    For $i=1 To 99
        If $clients[$i][0]=0 Then
            $clients[$i][0]=$_new_client
            $clients[$i][1]="some data";anything you want
            ;$clients[$i][2]="some other data";... same here
            $clients[0][0]+=1
            Return
        EndIf
    Next
EndFunc
Func _wipe()
    For $i=0 To 99
        If $clients[$i][0]<>0 Then _kill($clients[$i][0])
        For $j=0 To 19
            $clients[$i][$j]=0
        Next
    Next
EndFunc
Func _kill($_socket)
    For $i=1 To 99
        If $clients[$i][0]=$_socket Then
            For $j=0 To 19
                $clients[$i][$j]=0
            Next
            $clients[0][0]-=1
            Return TCPCloseSocket($_socket)
        EndIf
    Next
    Return SetError(1);that socket was not in the client list..
EndFunc
Func _unicast($_socket,$data)
    If $data="" Then Return
    $_send=TCPSend($_socket,$data)
    If @error Then _kill($_socket)
    Return $_send
EndFunc
Func _broadcast($data)
    For $i=1 To 99
        If $clients[$i][0]<>0 Then
            TCPSend($clients[$i][0],$data)
            If @error Then _kill($clients[$i][0])
        EndIf
    Next
EndFunc
Func _recv()
    For $i=1 To 99
        $recv=TCPRecv($clients[$i][0],10000)
        If $recv Then
            ;this is where you get data from your clients
            ConsoleWrite("got :"&$recv&" from : ("&$i&")"& $clients[$i][0] & @CRLF)
            ;for a simple chat you could use _broadcast($recv) here :)
        EndIf
    Next
EndFunc

let me know if you need some help with it :lmao:

Edited by TheMadman

Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

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...