TCP - IP Chat

  • hi,
    Ich habe mit einen kleinen "TCP - IP Chat" angefangen nur leider komme ich nicht richtig weiter wie kann man z.B.
    nachrichten abfangen ohne diese i-wo zu speichern?
    Wie kann ich die ip von dem User bekommen gerade connected?
    Vllt ein paar verbesserungsmöglichkeiten für meinen chat.

    Spoiler anzeigen
    [autoit]


    #include <ButtonConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <ListViewConstants.au3>
    #include <WindowsConstants.au3>
    #include <StaticConstants.au3>
    #include <GuiIPAddress.au3>
    #include <inet.au3>
    #include <GuiStatusBar.au3>

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

    dim $ip = @ipaddress1
    dim $mitem[6]

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

    $Username = iniread("config.ini","config","name","")
    if $username = "" Then
    #Region gui
    $Namegui = GUICreate("Username", 316, 128)
    $GroupBox = GUICtrlCreateGroup("", 8, 1, 297, 81)
    $Input = GUICtrlCreateInput("", 24, 40, 265, 21)
    $Label = GUICtrlCreateLabel("Geben Sie Ihren Username ein.", 24, 16, 152, 17)
    GUICtrlCreateGroup("", -99, -99, 1, 1)
    $Buttonok = GUICtrlCreateButton("&OK", 65, 91, 75, 25, 0)
    $Buttoncancel = GUICtrlCreateButton("&Cancel", 162, 91, 75, 25, 0)
    GUISetState(@SW_SHOW)
    #EndRegion gui
    While 1
    $Msg = GUIGetMsg()
    switch $msg
    Case $buttoncancel
    exitloop
    case $GUI_EVENT_CLOSE
    exitloop
    case $buttonok
    $username = GUICtrlRead($input)
    if $username = "" Then $username = "Defaultuser"
    iniwrite("config.ini","config","name",$username)
    exitloop
    Endswitch
    wend
    GUISetState(@SW_hide,$namegui)
    EndIf

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

    #Region gui
    $Form2 = GUICreate("Messenger", 531, 365)
    $ListView1 = GUICtrlCreateListView("Name", 336, 8, 177, 305)
    GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 173)
    $Menu = GuiCtrlCreateMenu("Menu")
    $mitem[1] = GUICtrlCreateMenuItem("Start server",$menu)
    $mitem[2] = GUICtrlCreateMenuItem("connect",$menu)
    $mitem[3] = GUICtrlCreateMenuItem("Kick User",$menu)
    GUICtrlSetState ($mitem[3], $GUI_DISABLE)
    $mitem[4] = GUICtrlCreateMenuItem("Ban User",$menu)
    GUICtrlSetState ($mitem[4], $GUI_DISABLE)
    $mitem[5] = GUICtrlCreateMenuItem("close",$menu)
    $About = GuiCtrlCreateMenu("About")
    $aitem = GUICtrlCreateMenuItem("About",$About)
    $Help = GuiCtrlCreateMenu("Help")
    $hitem = GUICtrlCreateMenuItem("?",$Help)
    $Edit = GUICtrlCreateEdit("", 16, 8, 289, 265)
    GUICtrlSetState ($edit, $GUI_DISABLE)
    GUICtrlSetFont($edit, 10, 400, 0, "Courier New")
    $Input = GUICtrlCreateInput("", 16, 288, 225, 21)
    GUICtrlSetState ($input, $GUI_DISABLE)
    $buttonsend = GUICtrlCreateButton("Send", 256, 288, 49, 25, $BS_DEFPUSHBUTTON)
    GUICtrlSetState ($buttonsend, $GUI_DISABLE)
    GUISetState(@SW_SHOW)
    #EndRegion gui

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

    tcpstartup()

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

    While 1
    $Msg = GUIGetMsg()
    Switch $Msg
    Case $GUI_EVENT_CLOSE
    Exit

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

    case $mitem[1]
    $MainSocket = TCPListen($ip,80,100)
    $conSocket = TCPconnect($ip,80)
    If $ConSocket >= 0 Then
    msgbox(0,"","Client Connected")
    GUICtrlSetState ($buttonsend, $gui_enable)
    GUICtrlSetState ($edit, $gui_enable)
    GUICtrlSetState ($input, $gui_enable)
    GUICtrlSetState ($mitem[4], $gui_enable)
    GUICtrlSetState ($mitem[3], $gui_enable)
    GUISetState()
    $ret = TCPSend($ConSocket, "- " & $username & " is online now.")
    GUICtrlSetData($edit, "- " & $username & " is online now." & @CRLF)
    Else
    msgbox(0,"","Error Server Nicht erreichbar")
    EndIf
    while 1
    $Msg = GUIGetMsg()
    Switch $Msg
    case $buttonsend
    If $ConSocket > -1 Then
    $ret = TCPSend($ConSocket, GUICtrlRead($input))
    If @error Or $ret < 0 Then
    TCPCloseSocket($ConSocket)
    WinSetTitle($Form2, "", "my server - Client Disconnected")
    $ConSocket = -1
    ElseIf $ret > 0 Then
    GUICtrlSetData($edit, GUICtrlRead($edit) & $username & ": " & GUICtrlRead($input) & @CRLF)
    EndIf
    EndIf
    GUICtrlSetData($input, "")
    case $GUI_EVENT_CLOSE
    TCPCloseSocket ($mainSocket)
    GUISetState(@SW_hide,$form2)
    exit
    endswitch
    wend

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

    case $mitem[2]
    #Region gui
    $connectgui = GUICreate("Connect", 316, 128)
    $GroupBox = GUICtrlCreateGroup("", 8, 1, 297, 81)
    $Label = GUICtrlCreateLabel("Geben Sie die ip des Servers ein.", 24, 16, 161, 17)
    $IPAddress = _GUICtrlIpAddress_Create($connectgui, 24, 40, 257, 25)
    _GUICtrlIpAddress_Set($IPAddress, "192.168.0.0")
    GUICtrlCreateGroup("", -99, -99, 1, 1)
    $Buttonok = GUICtrlCreateButton("&OK", 65, 91, 75, 25, 0)
    $Buttoncancel = GUICtrlCreateButton("&Cancel", 162, 91, 75, 25, 0)
    GUISetState(@SW_SHOW)
    #EndRegion gui
    While 1
    $Msg = GUIGetMsg()
    $conip = GUICtrlRead($IPAddress)
    switch $msg
    Case $Buttonok
    $conSocket = TCPconnect($conip,80)
    msgbox(0,"",$consocket&" "&@error)
    If $ConSocket >= 0 Then
    msgbox(0,"","Client Connected")
    GUISetState(@SW_hide,$connectgui)
    GUICtrlSetState ($buttonsend, $gui_enable)
    GUICtrlSetState ($edit, $gui_enable)
    GUICtrlSetState ($input, $gui_enable)
    GUICtrlSetState ($mitem[4], $gui_enable)
    GUICtrlSetState ($mitem[3], $gui_enable)
    $ret = TCPSend($ConSocket, "- " & $username & " is online now.")
    GUICtrlSetData($edit, "- " & $username & " is online now." & @CRLF)

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

    while 1
    $Msg = GUIGetMsg()
    Switch $Msg
    case $buttonsend
    If $ConSocket > -1 Then
    $ret = TCPSend($ConSocket, GUICtrlRead($input))
    If @error Or $ret < 0 Then

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

    TCPCloseSocket($ConSocket)
    WinSetTitle($Form2, "", "my server - Client Disconnected")
    $ConSocket = -1
    ElseIf $ret > 0 Then

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

    GUICtrlSetData($edit, GUICtrlRead($edit) & $username & ": " & GUICtrlRead($input) & @CRLF)
    EndIf
    EndIf
    GUICtrlSetData($input, "")
    case $GUI_EVENT_CLOSE
    GUISetState(@SW_hide,$form2)
    exit
    endswitch
    wend
    Else
    msgbox(0,"","Error Server Nicht erreichbar")
    GUISetState(@SW_hide,$connectgui)
    exit
    EndIf
    Case $buttoncancel
    GUISetState(@SW_hide,$connectgui)
    Exit
    case $GUI_EVENT_CLOSE
    GUISetState(@SW_hide,$connectgui)
    exit
    endswitch
    wend

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

    case $mitem[3]
    exitloop
    case $aitem
    msgbox(0,"","Noch in Arbeit...")
    case $hitem
    msgbox(0,"","Noch in Arbeit...")

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

    EndSwitch
    WEnd

    [/autoit]
  • Server

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <Inet.au3>

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

    Example()

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

    Func Example()
    TCPStartup()
    $MainSocket = TCPListen(@ipaddress1,12,100)
    $ConnectedSocket = tcpconnect(@ipaddress1,80)
    While 1
    $a = inputbox("Send","Senden")
    tcpsend($connectedsocket,$a)
    WEnd
    TCPShutdown()
    EndFunc

    [/autoit]

    Client

    Spoiler anzeigen
    [autoit]


    #include <GUIConstantsEx.au3>
    #include <Inet.au3>

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

    Example()

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

    Func Example()
    $GOOEY = GUICreate("get", 300, 200)
    $edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
    GUISetState()
    Local $msg, $recv
    TCPStartup()
    $ConnectedSocket = tcpconnect(@ipaddress1,12)
    While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    $recv = TCPRecv($ConnectedSocket, 2048)
    If $recv <> "" Then
    GUICtrlSetData($edit,$recv ))
    GUISetState()
    endif
    WEnd
    TCPShutdown()
    EndFunc

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

    </Inet>

    Einmal editiert, zuletzt von Griss (23. März 2009 um 21:09)

  • Wenn du dir meinen Chat-Server mal angegckt hättest, währe dir aufgefallen das deiner Falsch ist!

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <Inet.au3>

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

    Example()

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

    Func Example()
    TCPStartup()
    $MainSocket = TCPListen(@ipaddress1,12,100)
    DO
    $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1
    While 1
    $a = inputbox("Send","Senden")
    tcpsend($ConnectedSocket,$a)
    WEnd
    TCPShutdown()
    EndFunc

    [/autoit]


    Auserdem, so wie du deinen Client gemacht hast geht der Chat nur auf dem Pc wo auch der Server gestartet ist! Du musst die Ip-Adresse des Server beim Client angeben, anstad @IPAdress1, das geht nur wenn es auf dem Gleichen Pc ist, weil es dann den gleichen wert ha!

    mfg. Jam00

  • hi,
    noch ein kleines Problem wenn ich jetzt "$ConSocket = TCPAccept($MainSocket)" eingbe funktioniert der teil des scrpites nicht mehr.

    [autoit]

    Switch $Msg

    case $buttonsend
    If $ConSocket > -1 Then
    $ret = TCPSend($ConSocket,"Nachricht|" & $username & "|" & _NowTime(5) & "|" & GUICtrlRead($input))
    If @error Or $ret < 0 Then
    TCPCloseSocket($ConSocket)
    WinSetTitle($Form2, "", "my server - Client Disconnected")
    $ConSocket = -1
    ElseIf $ret > 0 Then
    GUICtrlSetData($edit, GUICtrlRead($edit) & $username & " " & _NowTime(5) & ": " & GUICtrlRead($input) & @CRLF)
    EndIf
    EndIf
    GUICtrlSetData($input, "")
    case $GUI_EVENT_CLOSE
    TCPCloseSocket ($mainSocket)
    GUISetState(@SW_hide,$form2)
    exit
    endswitch

    [/autoit]