Jump to content

multiple connections in 1 server


Recommended Posts

Well i have an server script, but u must re-open it and shut the first client down when you want to recieve data from another client and i have no idea how i could put it to recieve data from multiple clients at once, so could someone make this script to do that?

#include <GUIConstantsEx.au3>
Global $ConnectedSocket

$Gui = GUICreate(@IPaddress1 & ":33891", 624, 451, 193, 125)
$edit = GUICtrlCreateEdit("", 8, 48, 609, 375)
$cIP = GUICtrlCreateCombo("<local>", 8, 16, 169, 25)
GUICtrlSetData(-1, "<local>")
$connect = GUICtrlCreateButton("Connect",180,15,100,23)
$filemnu = GUICtrlCreateMenu("File")
$SaveToFile = GUICtrlCreateMenuItem("Save Output to file...",$filemnu)
$Exit = GUICtrlCreateMenuItem("Exit",$filemnu)
GUISetState(@SW_SHOW)

While 1
        Switch GUIGetMsg()
            case -3
                Exit
            Case $connect
                $ip = GUICtrlRead($cIP)
                if $ip = "<local>" then $ip = @IPAddress1
                _Connect($ip)
            Case $Exit
                TCPShutdown()
                Exit
            Case $SaveToFile
                _saveOutputToFile(Guictrlread($edit))
        EndSwitch
        
        $recv = TCPRecv($ConnectedSocket, 1024 * 1024)

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


Func _Connect($ipaddr)
    Local $szIPADDRESS = $ipaddr
    Local $nPORT = 33891

    TCPStartup()
    $MainSocket = TCPListen($szIPADDRESS, $nPORT)
    If $MainSocket = -1 Then Exit
    $ConnectedSocket = -1   
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1
    
    GUICtrlSetData($edit,"Client Connected...")
EndFunc  ;==>Connect

Func _saveOutputToFile($text)
    $file = FileSaveDialog("Save as...",@DesktopDir,"txt (*.txt)|any (*.*)",16,"output_" & random(1,1000) & ".txt")
    if not @error Then
        $fl = FileOpen($file,2)
        FileWrite($fl,$text)
        FileClose($fl)
    EndIf
EndFunc
Link to comment
Share on other sites

This is the 'layout' of a server which supports multiple clients:

global $Client[1]

While 1

If {a new TCPAccept, which isn't already in $Client[]} Then

{redim $Client[Ubound+1], put the return value of TCPAccept in $Client[]}

Endif

for $i = 1 to ubound($Client)-1

if TCPRecv($Client[$i],1024) Then
{do what you wanna do if a client sends something}
endif


next

WEnd
Edited by Kip
Link to comment
Share on other sites

like this?

global $Client[1]
#include <GUIConstantsEx.au3>
$Gui = GUICreate(@IPaddress1 & ":33891", 624, 451, 193, 125)
$edit = GUICtrlCreateEdit("", 8, 48, 609, 375)
GUISetState(@SW_SHOW)
While 1
Local $szIPADDRESS = @IPAddress1
Local $nPORT = 33891

TCPStartup()
$MainSocket = TCPListen($szIPADDRESS, $nPORT)
If $MainSocket = -1 Then Exit
TCPAccept($Mainsocket)
if not @error then

redim $Client[+1]

Endif

for $i = 1 to ubound($Client)-1

        $recv = TCPRecv($Client[$i],1024)
        If $recv <> "" Then GUICtrlSetData($edit,GUICtrlRead($edit) & @CRLF & ">" & $recv)
next

WEnd

Dont work btw

Link to comment
Share on other sites

1st: don't put TCPStartup in a loop.

2nd: No, not like that :)

This is like the most easiest, to understand, tcp server which can handle multiple clients.

And you don't even have to change the functions, they'll always work (if $Clients is declared, TCPStartup called and $iMainSocket is valid)

Global $Clients[1]

TCPStartup()

$iMainSocket = TCPListen(@IPAddress1,88); if @IPAddress1 doesnt work, try @IPAddress2, or "127.0.0.1"

While 1
    
    _Accept($iMainSocket)
    
    $iRecieved = _Recieve()
    
    If $iRecieved[1] Then; if any text is recieved then
        TCPSend($iRecieved[0],"I send you something back"); $iRecieved[0] = the client socket
    EndIf
    
WEnd

Func _Recieve(); check if a client has send us something
    
    Local $iReturn[2]
    
    For $i = 1 to UBound($Clients)-1; check every client
        $Recv = TCPRecv($Clients[$i],1024)
        If $Recv Then; if something has been recieved, then...
            
            $iReturn[0] = $Clients[$i]
            $iReturn[1] = $Recv
            
            Return $iReturn; ...Return the socket and the recieved text.
            
        EndIf
    Next
    
    Return $iReturn
    
EndFunc

Func _Accept($iMainSocket); Checks if a new client connects to our server
    $iAccept = TCPAccept($iMainSocket); So, if there is a new client, then...
    If $iAccept Then
        
        $Add = 1
        
        for $i = 1 to UBound($Clients)-1
            If $Clients[$i] = $iAccept Then; ...Then we're going to check if it isn't already in our array
                $Add = 0
                ExitLoop
            EndIf
        Next
        
        If $Add Then; If the client is new then...
            ReDim $Clients[UBound($Clients)+1]; Add a element to $Clients
            $Clients[UBound($Clients)-1] = $iAccept; Put the socket identifier into the last element.
        EndIf
        
    EndIf
EndFunc

Edit: added comments

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