TCP Connect über Internet (chat)

  • Hallo zusammen,
    ich hab nen chat für mein lokales netzwerk programmiert. Funktioniert einwandfrei. Ich wollte jetzt fragen wie man das machen muss wenn mein PC quasi mit

    Spoiler anzeigen
    [autoit]

    $g_IP=@ipaddress1
    TCPStartUp()

    [/autoit] [autoit][/autoit] [autoit]

    ; Create a Listening "SOCKET"
    ;==============================================
    $MainSocket = TCPListen($g_IP, 65432, 100 )
    If $MainSocket = -1 Then Exit

    [/autoit] [autoit][/autoit] [autoit]

    ; look for client connection
    ;--------------------
    While 1
    $ConnectedSocket = TCPAccept( $MainSocket)
    If $ConnectedSocket >= 0 Then
    msgbox(0,"","my server - Client Connected")
    exit
    EndIf
    Wend

    [/autoit]


    als Server fungiert und eine andere Person über Internet (also nicht im lokalen Netzwerk) zu mir Connecten will.

    Bin für jede Hilfe dankbar. Es reicht ein Verweis auf ein tut oder ein kleines Beispiel Script.#

    PS: hier mein Chat programm bisher (wie gesagt im lokalen Netz funktionierts / mit anderen Personen aus dem I-Net noch nicht ausführlich getestet)

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #Include <Constants.au3>
    #Include <ScreenCapture.au3>
    ;#NoTrayIcon

    [/autoit] [autoit][/autoit] [autoit]

    ; Change into the WinTitleMatchMode that supports classnames and handles
    AutoItSetOption("WinTitleMatchMode", 4)
    ; AutoIt script kann nicht über icon beendet werden ^^
    ;Break(0)
    ;Zuallererst nehmen wir uns das Tray icon vor ^HEHE^
    Opt("TrayMenuMode",1) ; Default tray menu items (Script Paused/Exit) will not be shown.
    $GuiToggle=TrayCreateItem ( "Chat - Fenster zeigen/verstecken" )
    $beenden=TrayCreateItem ( "Beenden!" )

    [/autoit] [autoit][/autoit] [autoit]

    Global $chat=GUICreate("Chat",400,500)
    $mylist = GUICtrlCreateEdit("Rechner Online:"& @CRLF, 1, 1, 400, 400)
    $SendB=GUICtrlCreateButton("Senden!",1,450)
    $Edit=GUICtrlCreateInput("",1,400,500,20)
    $filemenu = GUICtrlCreateMenu("Chatten mit ...")
    $othermenu=GUICtrlCreateMenu("Andere Funktionen")

    [/autoit] [autoit][/autoit] [autoit]

    ;Enter = Senden
    HotKeySet("{Enter}","EnterSend")

    [/autoit] [autoit][/autoit] [autoit]

    Global $menuitem[23]
    For $m=0 to 21
    $menuitem[$m]=0
    next
    $menuitem[0] = GUICtrlCreateMenuItem("Einen Chat hosten", $othermenu)
    $menuitem[22]=GUICtrlCreateMenuItem("Nach Comptern suchen.", $othermenu)
    $MITEMdisc=GUICtrlCreateMenuItem("Verbindung trennen.", $othermenu)
    GUISetState(@SW_SHOW)
    ;------Variablen:
    Global $port=1337
    Global $mhheight=0
    Global $mhwidth=0
    Global $connected=0
    Global $Guishow=1
    Global $sendm=""
    Global $Name=@ComputerName
    Global $recv=""
    Global $szIPADDRESS = @IPAddress1
    Global $szIP_Accepted
    Global $MainSocket
    Global $Host=0
    Global $s=1
    Global $sockets[100]
    Global $recmessage=""
    $sockets[0]=-1
    $sockets[1]=-1
    ;;;;;;;
    MsgBox(0,"Deine IP-Adresse ist:",@IPAddress1&" Benutzter Port: "&$port)
    ;;;;;
    Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet

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

    [/autoit] [autoit][/autoit] [autoit]

    $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

    [/autoit] [autoit][/autoit] [autoit]

    $sockaddr = 0

    [/autoit] [autoit][/autoit] [autoit]

    Return $aRet
    EndFunc ;==>SocketToIP

    [/autoit] [autoit][/autoit] [autoit]

    ;Host !!!!!!!!!!!!!!!
    Func HostFunc()
    if $Host=1 Then
    $sockets[$s]=-1
    $sockets[$s] = TCPAccept($MainSocket)
    if $sockets[$s] <> -1 then $s=$s+1
    if $recv<>"" Then
    for $se=0 to $s
    TCPSend($sockets[$se],$recv)
    Next
    EndIf
    EndIf
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]


    ;;;;Nachricht empfangen
    Func MessageRecv()
    for $so=0 to ($s-1)
    if $sockets[$so]<>-1 Then
    $recv = TCPRecv($sockets[$so], 2048)
    ;if $recv="¿"then
    ;
    ;EndIf


    ;Ab hier die Verarbeitung der empfangenen Nachricht==============
    ;================================================================
    If @error Then
    AddText( "Fehler mit Verbindung! Eine Verbindung wurde getrennt")
    TCPCloseSocket($sockets[$so])
    $sockets[$so]=-1
    ;
    $connected=0
    for $so2=0 to ($s-1)
    if $sockets[$so2]<>-1 Then $connected=1
    Next

    if $connected=0 Then
    TCPCloseSocket($MainSocket)
    AddText( "Fehler mit Verbindung! ALLE VERBINDUNGEN WURDEN GETRENNT !!")
    EndIf
    ;
    $sockets[$so]=-1
    EndIf

    If $recv <> "" Then


    if $Beepon=1 then
    if WinActive($chat,"")=False then
    Beep(3000)
    EndIf
    EndIf
    AddText($recv)
    ;Falls Fenster Hide !
    if $Guishow=0 Then
    TrayTip ( "Neue Nachricht", $recv ,7)
    EndIf
    EndIf
    EndIf
    Next
    ;Bis hier ____________________________________________________
    ;===============================================================
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;BEEEEEb ^^
    Global $Beepon=0
    $Beep=GUICtrlCreateMenuItem("Piepton anschalten",$othermenu)
    Func Piepton()
    if $Beepon=1 Then
    GUIctrlDelete($Beep)
    $Beep=GUICtrlCreateMenuItem("Piepton anschalten",$othermenu)
    $Beepon=0
    ;AddText("Benachrichtigung bei Chat Nachricht durch Piepton ist ausgeschaltet")
    Else
    GUIctrlDelete($Beep)
    $Beep=GUICtrlCreateMenuItem("Piepton ausschalten",$othermenu)
    $Beepon=1
    ;AddText("Benachrichtigung bei Chat Nachricht durch Piepton ist eingeschaltet")
    EndIf
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;IP-Adressen ::zu:: PC-Namen
    Global $IPshow=0
    $IPonoff=GUICtrlCreateMenuItem("IP-Adresse senden", $othermenu)
    Func Nametoggle()
    if $IPshow=1 Then
    GUIctrlDelete($IPonoff)
    $IPonoff=GUICtrlCreateMenuItem("IP-Adressen senden", $othermenu)
    $IPshow=0
    $Name=@ComputerName
    Else
    GUIctrlDelete($IPonoff)
    $IPonoff=GUICtrlCreateMenuItem("PC-Namen senden", $othermenu)
    $IPshow=1
    $Name=@IPAddress1
    EndIf
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func EnterSend()
    if WinActive($chat,"") Then
    Senden()
    Else
    HotKeySet("{Enter}")
    Send("{Enter}")
    HotKeySet("{Enter}","EnterSend")
    EndIf
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;Nachricht SENDEN !
    func Senden()
    if $sockets[0]<>-1 Or StringMid ( GUICtrlRead($Edit), 1 , 1 )="/" Then
    if GUICtrlRead($Edit)<>"" and StringLen (GUICtrlRead($Edit))<255 Then
    ;Checken auf Consolenbefehl
    if StringMid ( GUICtrlRead($Edit), 1 , 1 )="/" Then
    Select
    Case GUICtrlRead($Edit) = "/cls"
    Cls()
    Case StringMid(GUICtrlRead($Edit) ,1,4)= "/con"
    ;einfach Die Connectfunktion abgeändert
    if $sockets[0]=-1 Then
    $MainSocket=TCPListen(@IPAddress1,200)
    Local $IPtoConnect=StringMid(GUICtrlRead($Edit),6,(StringLen(GUICtrlRead($Edit))-4));Neue Variable
    $sockets[0]=TCPConnect($IPtoConnect,$port);Hier die Änderung
    If $sockets[0] = -1 Then
    AddText($IPtoConnect&" Verbindung fehlgeschlagen!");Hier auch
    Else
    $szIP_Accepted =$IPtoConnect;und hier
    Cls()
    AddText( "Verbunden mit: "&$szIP_Accepted)
    $connected=1
    endIf
    Else
    AddText("Es besteht bereits eine Verbindung")
    EndIf
    ;============================================
    Case Else
    AddText("unbekannter Befehl")
    EndSelect
    GUICtrlSetData($Edit, "")
    ;==============================================Else
    Else
    $sendm="["&$Name&"]:"&GUICtrlRead($Edit)
    TCPSend($sockets[0],$sendm)
    if $Host=1 then
    AddText( $sendm)
    for $so3=1 to ($s-1)
    TCPSend($sockets[$so3],$sendm)
    Next
    EndIf
    GUICtrlSetData($Edit, "")
    EndIf
    Else
    if GUICtrlRead($Edit)<>"" then AddText("Nachricht zu lang")
    EndIf
    Else
    AddText("keine bestehende Verbindung")
    EndIf
    EndFunc
    Func Connect($m)
    ;---Connect
    if $sockets[0]=-1 Then
    $MainSocket=TCPListen(@IPAddress1,200)
    $sockets[0]=TCPConnect("192.168.2."&($m+99),$port)
    If $sockets[0] = -1 Then
    AddText("192.168.2."&($m+99)&" Verbindung fehlgeschlagen!")
    Else
    $szIP_Accepted ="192.168.2."&($m+99)
    Cls()
    AddText( "Verbunden mit: "&$szIP_Accepted)
    $connected=1
    endIf
    Else
    AddText("Es besteht bereits eine Verbindung")
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    EndFunc

    Func AddText($message)
    GUICtrlSetData($mylist,GUICtrlRead($mylist) & @CRLF & $message)
    EndFunc
    Func Cls()
    GUICtrlSetData($mylist,"")
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    TCPStartUp()
    ;Check()
    Func Check()
    Cls()
    AddText("Rechner Online"&@CRLF)
    For $m=1 to 21
    GUICtrlDelete($menuitem[$m])
    next

    for $i=100 to 120
    $var = Ping("192.168.2."&$i,200)
    If $var Then; also possible: If @error = 0 Then ...
    AddText("192.168.2."&$i&" Online, Ping (in ms): " & $var)
    $menuitem[($i-99)] = GUICtrlCreateMenuItem("192.168.2."&$i, $filemenu)
    ;$MainSocket=TCPListen(@IPAddress1,200)
    ;$sockets[0]=TCPConnect("192.168.2."&$i,$port)

    Else
    AddText( "192.168.2."&$i&" Offline")
    EndIf
    next
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func Host()
    if $sockets[0]=-1 Then
    $MainSocket = TCPListen($szIPADDRESS, $port)
    If $MainSocket = -1 Then Exit
    ; Initialize a variable to represent a connection
    ;==============================================
    $ConnectedSocket = -1

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;Wait for and Accept a connection
    ;==============================================
    AddText( "Erstelle Server.... zum Abbrechen auf `Senden!` drücken!")
    Local $abr=0
    Do
    ;zum Abbrechen
    $nmsg=GUIGetMsg()
    if $nmsg = $SendB Then
    AddText( "ABGEBROCHEN !!!!!!!!!!")
    $abr=1
    TCPCloseSocket($MainSocket)
    ExitLoop
    EndIf

    $ConnectedSocket = TCPAccept($MainSocket)

    Until $ConnectedSocket <> -1

    [/autoit] [autoit][/autoit] [autoit]

    if $abr=0 then
    ; Get IP of client connecting
    Global $szIP_Accepted = SocketToIP($ConnectedSocket)
    Cls()
    AddText( "Verbunden mit: "&$szIP_Accepted)
    $sockets[0]=$ConnectedSocket
    ; Variable die Verwendung als Server anzeigt
    $Host=1
    $connected=1
    EndIf
    Else
    MsgBox(0,"Fehler","Zum Hosten erst bestehende Verbindungen trennen!")
    EndIf
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func Disconnect()
    Local $Disc=0
    GUICtrlSetData ( $Edit, $Name & " ist jetzt Offline" )
    Senden()
    For $seo4=0 to $s
    If $sockets[$seo4] <> -1 Then
    TCPCloseSocket($sockets[$seo4])
    $Disc=1
    EndIf
    Next
    TCPCloseSocket($MainSocket)
    if $Disc=1 then AddText("ALLE VERBINDUNGEN WURDEN GETRENNT !!")
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;DIE HAUPT SCHLEIFE !!!!!!!!!!!!!!======================================================
    While 1
    $msg = GUIGetMsg()

    [/autoit] [autoit][/autoit] [autoit]

    ; GUI Closed
    ;--------------------
    If $msg = $GUI_EVENT_CLOSE Then
    GuiToggle()
    Local $janein=7
    if $connected=0 then $janein=MsgBox ( 4, "Chat", "Erreichbar bleiben ??" )
    if $janein=6 then Host()
    EndIf


    Select
    Case $msg=$SendB
    Senden()
    Case $msg=$menuitem[22]
    Check()
    Case $msg=$MITEMdisc
    Disconnect()
    Case $msg=$Beep
    Piepton()
    Case $msg=$IPonoff
    Nametoggle()
    EndSelect
    ;Für das Tray icon :::
    $Tmsg = TrayGetMsg ( )
    Select
    Case $Tmsg=$beenden
    Beenden()
    Case $Tmsg=$GuiToggle
    GuiToggle()
    EndSelect
    ;

    MessageRecv()


    For $m=1 to 20
    if $menuitem[$m]<>0 Then
    If $msg=$menuitem[$m] Then
    Connect($m)
    EndIf
    EndIf
    Next
    if $msg=$menuitem[0] Then Host()

    HostFunc()

    WEnd
    ;==========================================================================

    [/autoit] [autoit][/autoit] [autoit]

    ;GUI schliessen
    Func GuiToggle()
    if $Guishow=1 Then
    GUISetState ( @SW_HIDE )
    $Guishow=0
    Else
    GUISetState ( @SW_SHOW )
    $Guishow=1
    EndIf
    EndFunc
    Beenden()
    Func Beenden()
    GUICtrlSetData ( $Edit, $Name & " ist jetzt Offline" )
    Senden()
    For $seo4=0 to $s
    If $sockets[$seo4] <> -1 Then TCPCloseSocket($sockets[$seo4])
    Next
    TCPCloseSocket($MainSocket)
    TCPShutdown ( )
    Exit
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit]