Sign in to follow this  
Followers 0
mircea

Tcp server & client

14 posts in this topic

Hello guys, im trying to make a TCP server and a TCP client, so i searched the help file the forums and i tried to add some changes, the problem is i can add only 1 client to the server

here is the server from the help file and the only changed i did and i think it should fix that it was the max connections at the TCPLISTEN but no succes.

I searched the forum and i found this example -> but there i searched it was almost the same as mine i mean the TCPlisten command.

What could be the problem ?

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)



TcpServer()

Func TcpServer()

;   Local $szServerPC = @ComputerName
;   Local $szIPADDRESS = TCPNameToIP($szServerPC)
    Local $szIPADDRESS = @IPAddress1
    Local $nPORT = Iniread("setariserver.ini","setari","Port","")
    Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
    Local $msg, $recv
    
    TCPStartup()
    

    $MainSocket = TCPListen($szIPADDRESS, $nPORT,50)

    If $MainSocket = -1 Then Exit


    $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)
    $edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
    GUISetState()

    $ConnectedSocket = -1


    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1

    $szIP_Accepted = SocketToIP($ConnectedSocket)


    While 1
        $msg = GUIGetMsg()

        If $msg = $GUI_EVENT_CLOSE Then ExitLoop

        $recv = TCPRecv($ConnectedSocket, 2048)
        

        If @error Then ExitLoop

        If $recv <> "" Then GUICtrlSetData($edit, _
                $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
    WEnd


    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

    TCPShutdown()
EndFunc  
Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet
    
    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc

the client code

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


TcpClient()

Func TCPClient()
    ; Set Some reusable info
    ;--------------------------
    Local $ConnectedSocket, $szData
    ; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
;   Local $szServerPC = @ComputerName
;   Local $szIPADDRESS = TCPNameToIP($szServerPC)
    ;Local $szIPADDRESS = @IPAddress1
    ;Local $nPORT = 33891
    
    $Main = GUICreate("Cft Tester Call", 391, 246, 192, 124)
    $Button1 = GUICtrlCreateButton("Vision Blocat", 112, 16, 161, 57)
    $Button2 = GUICtrlCreateButton("CFT Blocat", 112, 88, 161, 57)
    $Button3 = GUICtrlCreateButton("Multe failuri la CFT/Vision", 112, 168, 161, 57)
    GUISetState(@SW_SHOW)

    

    Local $Adresaip = iniread("setariclient.ini","Setari","Ipserver","")
    local $port = iniread("setariclient.ini","setari","Port","")
    local $statia = iniread("setariclient.ini","setari","NumeleStatiei","")
    local $linia = iniread("Setariclient.ini","setari","Linia","")

    TCPStartup()

    $ConnectedSocket = -1

    $ConnectedSocket = TCPConnect($adresaip, $PORT)

    If @error Then
        MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
    
    Else

        While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exitloop
            case $button1
                $szData = "Vision Blocat pe statia:"&$Statia&"->De pe linia:"&$linia
                TCPSend($ConnectedSocket, $szData)
            Case $button2
                $data2 = "CFT BLOCAT pe linia:"&$linia
                TCPSend($ConnectedSocket,$data2)
            Case $button3
                $data3 = "Multe failuri pe linia:"&$linia
                TCPSend($connectedSocket,$data3)
        EndSwitch
        WEnd

    EndIf
EndFunc

Share this post


Link to post
Share on other sites



Well, the first thing i see is that, you only have the one loop for accepting new connections. that could be why you only have one client. maybe try using an array and putting the tcpaccept into the main loop

just an example buuuut.....

.............other code

dim $s_array[5]

.............other code

while 1

$socket = tcpaccept (...) <---------temporary var

if $socket <> -1 then <-----------checking for error message

For $n = 0 to ubound-1 ($s_array) <---------looping through the socket array (s_array)

if $s_array[$n] <> -1 then <----------checking for empty array element

$s_array[$n] = $socket <----------adding the temp var to the empty array element

exitloop <--------exiting the "for to" loop

endif

next

if $n = ubound ($s_array)-1 then <-------------if no empty elements then close the temporary socket

tcpclosesocket ($socket)

endif

endif

...............other code

wend

there are alot of multiclient servers on these forums, i've got a couple in my signature.

Share this post


Link to post
Share on other sites

Sorry i didn't understand very well, but let's see if i understand

i have to change this

$MainSocket = TCPListen($szIPADDRESS, $nPORT,50)
with
$MainSocket = TCPListen($szIPADDRESS, $nPORT,clients[0][0]+1)

and this 3 i don't understand unforttely :huh2:

For $n = 0 to ubound-1 ($s_array) <---------looping through the socket array (s_array)
if $s_array[$n] <> -1 then <----------checking for empty array element
$s_array[$n] = $socket <----------adding the temp var to the empty array element

Share this post


Link to post
Share on other sites

and this 3 i don't understand unforttely :huh2:

For $n = 0 to ubound-1 ($s_array) <---------looping through the socket array (s_array)
if $s_array[$n] <> -1 then <----------checking for empty array element
$s_array[$n] = $socket <----------adding the temp var to the empty array element

Array Tutorial

The wiki has lots of other neat stuff too, you should check it out ;)

Share this post


Link to post
Share on other sites

@admiralalkex, i don't want to be rude but on each topic of mine you show me the array tutorial, maybe it my fault that i didn't make my self understandable but anyway that's another thing.

What is my problem is why in the help file dosen't show anything about array in the TCPLISTEN

more exacly the command line tcplisten TCPListen ( IPAddr, port [, MaxPendingConnection] ) it dosen't show anything about array except the example that showed me @codybarret and thank you for that.

Anyway i will look more deep in the the example that @codybarret has and make a comparison between what he told me and hes code

Share this post


Link to post
Share on other sites

So i tried to use the example that @admiralalkex has in his topic and the client gives me WSA Error: 10061 ( i checked and it dosen't wanna to connect to the server)

;server





$ip = @IPAddress1
$port = iniread("serverdata.ini","data","port","500")
$connections = iniread("serverdata.ini","Data","CatiClienti?","5")

$GOOEY = GUICreate("My Server (IP: " & $iP & ")", 300, 200)
$edit = GUICtrlCreateEdit("", 10, 10, 280, 180)

GUISetState()

$listen = tcplisten($ip,500,$connections)

;If $Listen = -1 Then close_()

    Dim $Socket_Data[$connections + 1][3]
    ProgressOn ('Socket Data', 'Reseting Socket Data...')
    For $0 = 0 To $connections
        $Socket_Data[$0][0] = 0 
        ProgressSet (Round (($0 / $connections) * 100),'','Reseting Socket Data...')
        Sleep (10)
    Next
    ProgressOff ()
    
    
    
while 1
_accept_connection()
_Recv_From_Sockets_ ()

WEnd

Func _Accept_Connection ()
    If $Socket_Data[0][0] = $connections Then Return
    ;Makes sure no more Connections can be made.
    $Accept = TCPAccept ($Listen)
    ;Accepts incomming connections.
    If $Accept = -1 Then Return
    For $0 = 1 To $connections
        If $Socket_Data[$0][0] = 0 Then 
            ;Find the first open socket.
            $Socket_Data[$0][0] = $Accept
            ;assigns that socket the incomming connection.
            Do
                $Recv = TCPRecv ($Accept, 1000000)
            Until $Recv <> ''
            $Recv = StringSplit ($Recv, '^')
            $Socket_Data[$0][1] = $Recv[1]
            $Socket_Data[$0][2] = $Recv[2]
            ;$Socket_Data[$0][n] = $Recv[n] DEPENDING O WHAT YOU DIMMED THE $SOCKET_DATA AS!!!
            $Socket_Data[0][0] += 1
            ;adds one to the Socket list.
            
            
            
            
            
            Return
        EndIf
    Next
    Return
EndFunc

Func _Recv_From_Sockets_ ()
    For $0 = 1 To $connections
        $Recv = TCPRecv ($Socket_Data[$0][0],1000000)
        
    Next
    If $recv <> "" Then GUICtrlSetData($edit, " > " & $recv & @CRLF & GUICtrlRead($edit))
EndFunc

Share this post


Link to post
Share on other sites

And i found out the problem, using @ Kip udf

Thank you very much Kip, too bad it's a bit hard to find your topic (atleast for me)

Share this post


Link to post
Share on other sites

@admiralalkex, i don't want to be rude but on each topic of mine you show me the array tutorial, maybe it my fault that i didn't make my self understandable but anyway that's another thing.

This is the only thread we've met, so I don't know what you mean by that. Also you really should take a few minutes to read it, then you wouldn't need to continue asking array-related questions.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

It was an other topic were you recommend me the same thing with array, but thank you for the help you are trying to offer me .

Edited by mircea

Share this post


Link to post
Share on other sites

Yes, and i learned alot from that topic you provided me. But my problem, or better say what i didn't understand on the tcplisten it was how he did to add more clients, but i fix it and understand it with the udf from @Kip

Share this post


Link to post
Share on other sites

here is my personal Multiclient example, i really should tweak it to make it more easy to understand, but this DOES NOT use KIPs UDF, (though a very nice set of functions he has made), i like to know what my program is doing exactly and have complete control over it.

Share this post


Link to post
Share on other sites

@codyBarre yours was the first one i tried but unforttely the little time i had and i was upset cause i looked in your code and didn't understand very well the part of the TCPlisten with the array (the problem to me was that in the help file wasn't show me anything about array, but now i understand something of what array has to do with the maxconnections)

Share this post


Link to post
Share on other sites

really (something i have to edit in my example) the MaxPending Connections doesn't limit the server to have that many clients, it limits the server to have that many PENDING connections (clients trying to connect at the same time before the sockets are saved into the array) ooops heh....

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  
Followers 0