Jump to content

How to handle multiple tcp connections?


E1M1
 Share

Recommended Posts

How to modify this script to accept diferent IP addresses?

If I send packet from X IP then it shows that packet, but when I send packet from Y IP then it won't show that packet unless I restart this server before I send packet from Y IP.

Any ideas how to make it accept multiple IP addeses at once so I could send packets from IP X and from IP Y and I would see packets from both IPs in same window?

#include <GUIConstantsEx.au3>
#include <String.au3>
;~ Opt('MustDeclareVars', 1)

;==============================================
;==============================================
;SERVER!! Start Me First !!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
    ; Set Some reusable info
    ; Set your Public IP address (@IPAddress1) here.
    ;   Local $szServerPC = @ComputerName
    Local $szIPADDRESS = "0.0.0.0";localip
    Local $nPORT = 123;tcp port for login and chatlobby
    Local $remoteszIPADDRESS = "1.2.3.4";Server
    Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
    Local $msg, $recv

    ; Start The TCP Services
    ;==============================================
    TCPStartup()

    ; Create a Listening "SOCKET".
    ;   Using your IP Address and Port 123.
    ;==============================================
    $MainSocket = TCPListen($szIPADDRESS, $nPORT)

    ; If the Socket creation fails, exit.
    If $MainSocket = -1 Then Exit


    ; Create a GUI for messages
    ;==============================================
    $GOOEY = GUICreate("proxy (IP: " & $szIPADDRESS & ")", 800, 200)
    $edit = GUICtrlCreateEdit("", 10, 10, 780, 180)
    GUISetState()


    ; Initialize a variable to represent a connection
    ;==============================================
    $ConnectedSocket = -1


    ;Wait for and Accept a connection
    ;==============================================
    Do
        $ConnectedSocket = TCPAccept($MainSocket);Listen Tcp packets that come from 127.0.0.1
    Until $ConnectedSocket <> -1
    $remoteConnectedSocket = TCPConnect($remoteszIPADDRESS, $nPORT)
;~  MsgBox(0,0,$remoteConnectedSocket)


    ; Get IP of client connecting
    $szIP_Accepted = SocketToIP($ConnectedSocket)

    ; GUI Message Loop
    ;==============================================
    While 1
        $msg = GUIGetMsg()

        ; GUI Closed
        ;--------------------
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop

        ; Try to receive (up to) 2048 bytes
        ;----------------------------------------------------------------
        $recv = TCPRecv($ConnectedSocket, 4096,1)

        ; If the receive failed with @error then the socket has disconnected
        ;----------------------------------------------------------------
        If @error Then ExitLoop

        ; Update the edit control with what we have received
        ;----------------------------------------------------------------
        If $recv <> "" Then
            GUICtrlSetData($edit, $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
            ClipPut($recv)
        EndIf
    WEnd


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

    TCPShutdown()
EndFunc   ;==>Example

; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
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   ;==>SocketToIP

edited

Link to comment
Share on other sites

Can't help you any with your code (I'm justr starting down a similar track to you).

BUT

Have you tried Kips event driven TCP UDF?

http://www.autoitscript.com/forum/index.php?showtopic=74325

It works REAL well for multiple clients.

Good Luck

John Morrison

aka

Storm-E

Link to comment
Share on other sites

nice udf but how to make it show received packet?

Hmm thought that was in Kips examples...

Anyway like this..

_TCP_RegisterEvent($hServer, $TCP_RECEIVE, "Received") ; Called when data is received.
.
.
.
.
Func Received($hSocket, $sReceived, $iError) ; Called when data is received.
    Switch $iError
        Case True
            _TCP_Send($hSocket, "Recieved FAILED")
            ToolTip("SERVER: RECIEVE ERROR", 10, 50)
        Case Else
            _TCP_Send($hSocket, "Recieved OK") ; Just to tell client it has been recieved
            ToolTip("SERVER: " & $sReceived, 10, 50) ; Here is your incoming data
    EndSwitch
EndFunc   ;==>Received

I've only just started using this UDF so I'm no expert :-(

Good Luck!

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