Jump to content

Best way for multiple connections?


Recommended Posts

Heya all. Im wondering the best way to manage multiple connections using the same IP and Port?

I -can- have two scripts for this - I.E. one for listening for connections and getting socket IDs (and possibly assigning those socket IDs to a file) and one for servicing those sockets. Problem is, I have no idea how or even if I can transfer those same connections...

Any help? :?

-Monkey

Monkeh.

Link to comment
Share on other sites

#include"GUIConstants.au3"
Global $MainSocket
Global $ConnectedSocket = -1
dim $ConnectedSocket[101]
dim $recv[101]
$confirmation = 0

$IP = InputBox("serverip","Insert the serverip",@IPAddress2)
$port = 65432
$maxcon = 100
$MaxLength = 2048

for $i = 1 to 100 step 1
    $ConnectedSocket[$i] = -1
Next

TCPStartUp()
$MainSocket = TCPListen($IP, $port,  $maxcon )
If $MainSocket = -1 Then 
    MsgBox(16,"Error","Error!("&@error&")"&@CRLF&"This server will be terminated!")
    Exit 0
Endif
    $winwidth = 300
    $buttonwidth = 50
GUICreate("My Server (IP: " & $IP & ")",$winwidth,250)
$edit = GUICtrlCreateEdit("",10,10,280,180)
$Clearlog = GuiCtrlCreateButton("Clearlog",($winwidth-$buttonwidth)/2, 220, $buttonwidth)
GUISetState()

GUICtrlSetData($edit,"Server up and running!"& @CRLF & GUICtrlRead($edit))

while 1
    $msg = GUIGetMsg()
    if $msg = $GUI_EVENT_CLOSE Then
        for $i = 1 to 100 step 1
            if $ConnectedSocket[$i] <> -1 Then
            EndIf
        Next
        tcpshutdown()
        Exit 0
    EndIf
    
    if $msg = $Clearlog Then
        GuiCtrlSetData($edit,"")
    Endif
    
    for $i = 1 to 100 step 1
        if $ConnectedSocket[$i] <> -1 Then
            $recv[$i] = tcprecv($ConnectedSocket[$i], 512)
            
            if $recv[$i] <> "" Then
                for $j = 1 to 100 step 1
                    if $ConnectedSocket[$j] <> -1 Then
                        If @error Then GUICtrlSetData($edit,"Error: " & "There is a error @number2 -"&@error & @CRLF & GUICtrlRead($edit))                      ;Error 2
                            ;-----------------LOGIN CHECK-----------------
                            $data = StringSplit($recv[$i],",")
                            if Not @error Then
                                if $data[1] = "login" Then
                                $userread = IniReadSection("users.ini",$data[2])
                                    if Not @error then
                                        if $userread[1][1] = $data[3] Then 
                                            GUICtrlSetData($edit,"> " & "User has logged in! id="&$data[2] & @CRLF & GUICtrlRead($edit))
                                            TCPSend($ConnectedSocket[$i],"Loggedin")
                                        Endif
                                    Endif   
                                Endif
                                
                            Endif
                            ;/----------------LOGIN CHECK-----------------
                            
                            ;-----------------DC CHECK-----------------
                            $data2 = StringSplit($recv[$i],"-")
                            if Not @error Then
                                if $data2[1] = "DC" Then
                                    GUICtrlSetData($edit,"> " & "User has been DC'ed="&$data2[2] & @CRLF & GUICtrlRead($edit))
                                EndIf
                            Endif
                            ;/----------------DC CHECK-----------------
                        GuiCtrlSetData($edit,"> "& $recv[$i] &@CRLF& GUICtrlRead($edit))
                        
                    EndIf
                    
                    
            Next
        EndIf
            
        Else
            $ConnectedSocket[$i] = tcpaccept($MainSocket)
            if $ConnectedSocket[$i] <> -1 Then
            TCPSend($ConnectedSocket[$i],"Connection initialized")
            GUICtrlSetData ($edit,"> User connected - Socket("& $i &")"& @crlf & guictrlread($edit))
            Endif
            
        EndIf
    Next
WEnd

Func SocketToIP($SHOCKET)

    Local $sockaddr = DLLStructCreate("short;ushort;uint;char[8]")

    Local $aRet = DLLCall("Ws2_32.dll","int","getpeername","int",$SHOCKET, _
            "ptr",DLLStructGetPtr($sockaddr),"int_ptr",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

Its just a script i scrambled and used. Maby you can get something out of 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...