Jump to content

Reset TCP server count connection


Recommended Posts

hi all. i have this script in which i set server to accept to 3 client connections and when server reaches 3rd connection it will prompt for a message that it is full and would not accept for connection. my problem is, i cannot reset the serverback to zero so that it will accept again for any incoming connection. i have put a context menu on the script so that i have control to reset and stop the server. can someone help me on how to reset.

here is what i have

#Include <GuiListView.au3>
#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <inet.au3>
#include <String.au3>
#Include <Constants.au3>

Opt("TrayMenuMode",1) 

Opt("WinTitleMatchMode", 2)
Opt('MustDeclareVars', 0)


Global $CURRENT_SOCKET 
Global $CONNECTED_SOCKET[3]
Global $TCP_SERVER
Global $SPLIT, $RECV

$accept = TrayCreateItem("Accept Incoming")
TrayCreateItem("")
$stop = TrayCreateItem("Stop Connection")
TrayCreateItem("")
$close = TrayCreateItem("Close")

TraySetState()


While 1
    
$msg = TrayGetMsg()
    Select      
        Case $msg = $accept         
            ReConnect()
        Case $msg = $stop
            Stop()
        Case $msg = $close
            Close()
    EndSelect
    
    $CONNECTED_SOCKET[$CURRENT_SOCKET] = TCPAccept($TCP_SERVER)         
        If $CONNECTED_SOCKET[$CURRENT_SOCKET] <> -1 Then            
            
            $CURRENT_SOCKET += 1            
            
            If $CURRENT_SOCKET = 2 Then
                
                TCPCloseSocket($CONNECTED_SOCKET[$CURRENT_SOCKET])
                            
                
                MsgBox(64, "Warning!", "Server is now Full!")           
            EndIf
        EndIf       
    
    
    For $INDEX = 0 To 2
        $RECV = TCPRecv($CONNECTED_SOCKET[$INDEX],512)
                                
                    
        
    Next
    
    
Wend


Func ReConnect()
    
$TCP = TCPStartup()

$TCP_SERVER = TCPListen(@IPAddress1,7000,16)
    


ENdFunc


Func Stop()
    
    TCPCloseSocket($CONNECTED_SOCKET[$CURRENT_SOCKET])
    TCPCloseSocket($TCP_SERVER)
    TCPShutdown()   
    
EndFunc

can somebody help me here...

thanks,,,

Edited by JohnRichard
Link to comment
Share on other sites

You could use a keep alive message of sorts to probe who is still connected or have the client send a message when it disconnects. When that happens decrement $CURRENT_SOCKET and _ArrayDelete() the one that isn't connected anymore from $CONNECTED_SOCKET.

This is kind of what I'm visioning.

For $i = 0 To $CURRENT_SOCKET
    TCPSend($CONNECTED_SOCKET[$i], "ping")
    If TCPRecv($CONNECTED_SOCKET[$i], 1024) = "" Then
        $CURRENT_SOCKET -= 1
        _ArrayDelete($CONNECTED_SOCKET, $i)
    EndIf
Next
Edited by SoulA
Link to comment
Share on other sites

thanks soula for the reply. :P i'm thinking of way how to decrement it but can't put it in the script. i'm confused now. where exactly i will insert the script. i'm trying a trial and error but server exits when the 3rd client get connected. i wanted to keep the script alive then reset the connection on the context "accept connection"

$CONNECTED_SOCKET[$CURRENT_SOCKET] = TCPAccept($TCP_SERVER)         
        If $CONNECTED_SOCKET[$CURRENT_SOCKET] <> -1 Then            
            
            $CURRENT_SOCKET += 1            
            
            If $CURRENT_SOCKET = 2 Then             
                TCPCloseSocket($CONNECTED_SOCKET[$CURRENT_SOCKET])
                TCPCloseSocket($TCP_SERVER)             
                Sleep(100)
                MsgBox(64, "Warning!", "Server is now Full! It will not accept any new connection.")            
            EndIf
        EndIf
Link to comment
Share on other sites

I'm confused, when you select reconnect do you want it to drop the current clients and allow 3 new clients to connect?

basically i will just do reconnect when server reaches its 3 client connection. when it did, i will select reconnect then server will be ready again to accept incoming 3 connection. what is happening in the script, when it received the 3rd client connection, script will exit. script should stay alive so that i have the option from the context menu to reset the connection...

i'm just don't know how to put it on the script...

While 1
    
$msg = TrayGetMsg()
    Select      
        Case $msg = $accept         
            Connect()
        Case $msg = $stop
            Stop()
        Case $msg = $close
            Close()
    EndSelect
    
    $CONNECTED_SOCKET[$CURRENT_SOCKET] = TCPAccept($TCP_SERVER)         
        If $CONNECTED_SOCKET[$CURRENT_SOCKET] <> -1 Then            
            
            $CURRENT_SOCKET += 1            
            
            If $CURRENT_SOCKET = 2 Then 

            For $i = 0 To $CURRENT_SOCKET
            TCPSend($CONNECTED_SOCKET[$i], "ping")          
            If TCPRecv($CONNECTED_SOCKET[$i], 1024) = "" Then
            
                $CURRENT_SOCKET -= 1
                _ArrayDelete($CONNECTED_SOCKET, $i)
                TCPCloseSocket($CONNECTED_SOCKET[$CURRENT_SOCKET])      
            EndIf
            Next

                                 EndIf
        EndIf       
    
    
    For $INDEX = 0 To 2
        $RECV = TCPRecv($CONNECTED_SOCKET[$INDEX],512)      
        If $RECV <> "" Then             
        ConsoleWrite("Connection established from... " & @CRLF & $RECV & @CRLF & "Connect Time: " & @HOUR & ":" & @MIN & ":" & @SEC & @CRLF)            
                                
            EndIf       
        EndIf
    Next
    
    Sleep(20)
Wend
Edited by JohnRichard
Link to comment
Share on other sites

I do not see what is causing the script to exit. Also when you select reconnect yourself, do you want to allow 3 new clients to connect, or drop the 3 already connected and allow 3 new ones?

Does this do what you want?

Func ReConnect()
    TCPShutdown()
        $TCP = TCPStartup()
    $TCP_SERVER = TCPListen(@IPAddress1,7000,16)
    $CURRENT_SOCKET = 0
ENdFunc
Edited by foster74
Link to comment
Share on other sites

I do not see what is causing the script to exit. Also when you select reconnect yourself, do you want to allow 3 new clients to connect, or drop the 3 already connected and allow 3 new ones?

Does this do what you want?

Func ReConnect()
    TCPShutdown()
        $TCP = TCPStartup()
    $TCP_SERVER = TCPListen(@IPAddress1,7000,16)
    $CURRENT_SOCKET = 0
ENdFunc
thanks foster. what i want to happen when i reconnect myself, it will drop the three already connected and allow 3 new incoming connection. does this will solve the script?

i'll test and see to it...

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