Jump to content

Only FIRST TCP client 'connects'


Recommended Posts

Well I can get my first TCP client to connect fine and login (I made a login script). But when that first TCP client is still running, the 2nd one CONNECTS, but after it connects, it like.... freezes?

Here is my server:

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

HotKeySet ("{`}",'_Exit_')
HotKeySet ('^m','_send_')
$0 = 0
$00 = 0; serverrrrrrrrrrrrrrrrr
$000 = 0
$ip = InputBox ('IP=?','Please choose a funtioning IP for your server...',@IPAddress1)
;Makes the servers IP address... default is your @IPADDRESS1.
$port = InputBox ('Port=?','Please Choose a port # that is not being used...',"4000")
;Makes the port # can be anything between 1 and 60000.
;(the maximum is a guess i don't know how many ports there are butits close).
;and ONLY if the port is not already being used.
$max_connections = InputBox ('Maximim Connections=?', 'Please choose an amount of clients that would be able to connect to the server...(recommended that its minimal)',10)
;How many clients can be connected FUNCTIONABLY to the server... any more and it will not function correctly on the clients side.


$Form1 = GUICreate("Server", 477, 454, 193, 125)
$User = GUICtrlCreateInput("User", 40, 48, 161, 21)
$Data = GUICtrlCreateInput("Data", 240, 48, 185, 21)
$Button1 = GUICtrlCreateButton("Send", 160, 96, 121, 25, 0)
$Edit = GUICtrlCreateEdit("", 64, 144, 345, 281)
GUISetState(@SW_SHOW)

Global $editread
TCPStartup ()

$TCPListen = TCPListen ($ip, $port, $max_connections)
If $TCPListen = -1 Then _Exit_ ()
;Creates a listening Socket

Dim $Socket_Data[$max_connections + 1][5]
;add more to the second array to add data.
;(ex) 2 would have another column for data.
;you could make Usernames or ClientIP's in it... anything you want.
;Socket_data EXAMPLE
;$Socket_Data[0][0] = AMOUNT OF CURRENT CLIENTS
;$Socket_Data[1][0] = client 1
;$Socket_Data[2][0] = client 2
;$Socket_Data[n][0] = client n

ProgressOn ('Socket Data', 'Reseting Socket Data...')
For $0 = 0 To $max_connections
    $Socket_Data[$0][0] = 0 
    ;To reset the Socket array.
    ProgressSet (Round (($0 / $max_connections) * 100),'','Reseting Socket Data...')
    Sleep (10)
Next
ProgressOff ()


$msg = 0
While $msg <> $GUI_EVENT_CLOSE
    $msg = GUIGetMsg()
    $listread = GuiCtrlRead($Edit)
    _send_()
    _Accept_Connection ()
    _Recv_From_Sockets_ ()
    Sleep (100)
WEnd
Func _Accept_Connection ()
    If $Socket_Data[0][0] = $max_connections Then Return
    ;Makes sure no more Connections can be made.
        $Accept = TCPAccept ($TCPListen)
        ;Accepts incomming connections.
        If $Accept = -1 Then Return
            For $0 = 1 To $max_connections
                If $Socket_Data[$0][0] = 0 Then
                ;Find the first open socket.
                $Socket_Data[$0][0] = $Accept
                ;assigns that socket the incomming connection.
                    ToolTip("")
                    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][3] = $Recv[3]
                    ;MsgBox (48, '', $Socket_Data[$0][1] & @CRLF & $Socket_Data[$0][2] & @CRLF & $Socket_Data[$0][3]);$Socket_Data[$0][n] = $Recv[n] DEPENDING O WHAT YOU DIMMED THE $SOCKET_ARRAY AS!!!
                    $Socket_Data[0][0] += 1
                    TCPSend ($Socket_Data[$0][0], ".")
                    ;adds one to the Socket list.
                    $00 = 'New Client'
                    ToolTip ($00,0, 0,'[-Server-]')
                    ;_Broadcast_To_Sockets_ ($00)
                    ;MsgBox(0,"",$Socket_Data[0][0])
                    ;Broadcasts to all the clients.
                    $0 = 0
                    $00 = 0
                    Return
                EndIf
            Next
        Return
EndFunc

Func _Broadcast_To_Sockets_ ($0)
    For $00 = 1 To $max_connections
        If $Socket_Data[$00][0] <> 0 Then TCPSend ($Socket_Data[$00][0], $0)
        ;sends to every client.
    Next
    $00 = 0
    Return
EndFunc
Func _send_ ()
    Select
        Case $msg = $Button1
            $Input = GUICtrlRead($User)
            $Data = GUICtrlRead($User)
            MsgBox(0, "", $Input & $Data)
    EndSelect       
    If $0 = '' Then Return
    _Broadcast_To_Sockets_ ($0)
    $0 = 0
    $00 = 0
    Return
EndFunc

Func _Recv_From_Sockets_()
    For $0 = 1 To $max_connections
        $Recv = TCPRecv ($Socket_Data[$0][0],1000000)
        If $Recv <> '' Then
            If Stringleft($Recv, StringLen('<DC>^')) = '<DC>^' Then
                $Recv = Stringtrimleft ($Recv, StringLen ("<DC>^"))
                $Recv = ""
                GuiCtrlSetData($Edit, $Recv)
                _Disconnect($Socket_Data[$0][0])
            ElseIf Stringleft($Recv, StringLen('<Split>^')) = '<Split>^' Then
                $Recv = Stringtrimleft ($Recv, StringLen ("<Split>^"))
                $Recv = StringSplit ($Recv, '^')
                    If $Recv[3] = "register" Then
                        IniWrite("Casino.ini", $Recv[1], "Password", $Recv[2])
                        IniWrite("Casino.ini", $Recv[1], "Money", "0")
                        $Recv = ""
                    ElseIf $Recv[3] = "login" Then
                        $login = IniRead("Casino.ini", $Recv[1], "Password", "error")
                    
                        
                        
                        
                        If $login = $editread Then
                            MsgBox(0, "", "good")
                            
                            
                        ElseIf $login <> $Recv[2] Then
                            TCPSend ($Socket_Data[$0][0], "login error")
                        Else
                            TCPSend ($Socket_Data[$0][0], "login success")
                            GuiCtrlSetData($Edit, $Recv[1])
                        EndIf
                    EndIf
            Else
                MsgBox(0,"", $Recv)
            EndIf
        Else
            $Recv = ""
            Return
        EndIf
    Next
EndFunc

Func _Disconnect($00)
For $0 = 1 To $max_connections
    If $Socket_Data[$0][0] = $00 Then
        $Socket_Data[$0][0] = 0
        $Socket_Data[$0][1] = 0
        $Socket_Data[$0][2] = 0
        $Socket_Data[$0][3] = 0
        $Socket_Data[0][0] -= 1
        Return
    EndIf
Next
EndFunc

Func _Exit_ ()
    TCPShutdown ()
    Exit
EndFunc

And here are bits and pieces of my client (but these bits are everything that my client has to do with TCP)

For $0 = 0 To 10
    $Socket = TCPConnect ($ip, $port)
    ToolTip("hey")
    If $Socket <> -1 Then
        TCPSend($Socket, "hey")
        GUISetState(@SW_SHOW, $Form6)
        ExitLoop
    ElseIf $Socket = -1 Then
        MsgBox(0, "Unable", "Unable to connect to the server. Try again or contact me (Projects) on Conquer")
        OFF()
    EndIf
    TCPCloseSocket ($Socket)
    Sleep (300)
Next






;-------------------------
            Case $msg = $ButtonL2
                GUISetState(@SW_HIDE, $Form6)
                GUISetState(@SW_SHOW, $Form0)


            Case $msg = $ButtonL
                $userL = GuiCtrlRead($UsernameL)
                $pwL = GUICtrlRead($PasswordL)
                TCPSend ($Socket,'<Split>^' & $userL & '^' & $pwL & '^login')
                Do
                    $Recv = TCPRecv ($Socket,1000000)
                Until $Recv <> ""
                If $Recv = "login error" Then
                    MsgBox(0, "Wrong", "Either your username or password is wrong")
                    GUICtrlSetData($UsernameL, "Username")
                    GUICtrlSetData($PasswordL, "Password")
                
                ElseIf $Recv = "login success" Then
                    GuiCtrlSetData($Label9, "Logged in as:  " & $userL)
                    GUISetState(@SW_SHOW, $Form1)
                    GUISetState(@SW_HIDE, $Form6)
                EndIf

$ButtonL in the Case is 'Log In' button and $ButtonL2 is the 'Register' Button. As you can see, the register BUTTON has nothing to do with TCP stuff. But the Log in button does...

So what is happening, I run my SECOND client, and it connects (I know this because of previous MsgBoxes I had before I put this code in)... NOW, if I press the REGISTER button, it does what it's suppose to do. Which lets me know a few things: First off the client is connected (because of my server msgboxes I get) and Second, the client is not the cause of this problem because my first client COULD connect, so its nothing wrong with the 'Log In' button script.

So then.... I have my account registered, all fine and dandy (Remember, the first client can connect, it has nothing to do with the account scripts... Something with the server handling more than 1 client)..... I type in my username and password and press 'Log In'.... Then my 2nd client just freezes.... But not techinally the client itself... Cause my hotkey for exiting the script is Esc and that works BUT here is my exit script in the client:

Func OFF()
    TCPsend ($Socket, '<DC>^' & $userL)
    Sleep(2000)
    TCPShutdown ()
    Exit
EndFunc

It sends a message to the server (ignore $userL for now, its kind of an error because they aren't logged in, but thats nothing to worry about now)........ And the server SHOULD receive it and subtract that client from the list (In the server script above its _Disconnect($00) func)...... But it doesn't...

So my theory is, and I've wasted about a total of 3 hours switching things around and trying to fix this, that when the Log In button is pressed the 2nd time something goes wrong in my server... Its not a loop problem... because the server accepts the 2nd client and the 2nd client gets connected.... So I'm guessing it has to be in my $Recv_From_Socket Func that is causing me all this pain and frustration xD.

(Sorry for the huge rant.... I wanted to be specific on what my problem is and what is NOT causing it)

Thanks if anyone can help me :)

Edited by UnknownWarrior
Link to comment
Share on other sites

hmm.. is the server slow?:

sending?

receiving?

now that you got it connecting.. try taking out the Msgbox.. cause that pauses the script until its closed.... place my tooltip back in there see if that helps...

and is it the server that is taking up cpu usage?

Link to comment
Share on other sites

gimme about an hour and i'll be able to get on GT and help you somemore

Link to comment
Share on other sites

After 2 hours of me and cody barrett working on it.... It REFUSES to work.... I need some expert advice here..... Nothing we did would fix the problem... When I run the 2nd client it wouldn't even go off with the Hotkey I have set to shut it off....It would close the program fine with the hotkey in the first client..... Now the first clients gone... Then I do same hotkey to get rid of 2nd client.... And nothing happens...

Something incredibly gay is happening here and I'm getting pissed ><

Link to comment
Share on other sites

yeah... anyone else know of what could be happening... his Socket arrays are fine as i can tell... i dunno what the problem is exactly

Link to comment
Share on other sites

Looking at your server script $Recv_From_Socket function:

For $0 = 1 To $max_connections should be For $x = 1 To $max_connections

or the line below the for loop

$Recv = TCPRecv ($Socket_Data[$x][0],1000000) should be $Recv = TCPRecv ($Socket_Data[$0][0],1000000)

Note:

You are using the following variable $0, $00, $000, $ip, $port, and $max_connections with all the functions in the server script. I will make them Global you may be loosing their values.

Example:

; Here the value of $max_connections is equal 4
$max_connections = 4

Func _Recv_From_Sockets_()
    ; But inside the function beacuse $max_connections is not Global the value is 0.
    For $0 = 1 To $max_connections
       ....
    Next
EndFunc
Edited by Danny35d
AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

Looking at your server script $Recv_From_Socket function:

For $0 = 1 To $max_connections should be For $x = 1 To $max_connections

or the line below the for loop

$Recv = TCPRecv ($Socket_Data[$x][0],1000000) should be $Recv = TCPRecv ($Socket_Data[$0][0],1000000)

Oh my bad, I missed one... I was just using $x as a test and changed them back when I copied and pasted. (I was thinking maybe $0 ='ed something else from previously func that I wasn't seeing)

But anyways, thats not the problem >_<.

Link to comment
Share on other sites

I think I solved it.

In your _Recv_From_Sockets_() function if no data have been received from a socket you go to this else statement:

Else
            $Recv = ""
            Return

Which obviously returns from your function making the second, third etc client's data go unfetched. Removing the return seems to fix it.

I tip is to debug in telnet. Makes things so much easier.

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

I think I solved it.

In your _Recv_From_Sockets_() function if no data have been received from a socket you go to this else statement:

Else
            $Recv = ""
            Return

Which obviously returns from your function making the second, third etc client's data go unfetched. Removing the return seems to fix it.

I tip is to debug in telnet. Makes things so much easier.

O

M

G

You have no idea how many hours I (including Cody Barrett) spent on trying to solve this... (I spent over 4 hours reading each of my scripts line by line and over and over again.... No lie either)

And it was something as simple as that. I didn't even look at my script, but once I saw what you posted it ALL made sense....

And it works now... grrr

Thank you VERY much, and I feel like an idiot xD. Now I can finally continue on with my project >_< .

EDIT: I'm still laughing at myself... A 6 letter word was the difference between a script fully working, or completely being useless xD.

Edited by UnknownWarrior
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...