tcp/ip chat

  • hallo,
    ich hätte da nochmal ein problem und zwar:
    ich möchte gerne mit einem freund einen kleinen chatroom machen.
    also habe ich mir mal aus der helpfile die scripte zu tcprecive und tcpsend geholt.

    wir haben es auch hinbekommen, jedoch kann immer nur einer senden und ein anderer empfangen.
    habe leider noch nicht so viel ahnung von chats.

    also habe ich mal in der suche geschaut, jedoch habe ich dort nur sehr aufwändigt scripts zum thema tcp chat gefunden.
    ist es möglich einen simplen chat aus diesen 2 scripten zu erstellen? :
    ich denke dass es nicht so schwer sein wird für leute die sich damit auskennen :)

    danke für antworten

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>

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

    Opt('MustDeclareVars', 1)

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

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

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

    Example()

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

    Func Example()
    ; Set Some reusable info
    ; Set your Public IP address (@IPAddress1) here.
    ; Local $szServerPC = @ComputerName
    ; Local $szIPADDRESS = TCPNameToIP($szServerPC)
    Local $szIPADDRESS = @IPAddress1
    Local $nPORT = 33891
    Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
    Local $msg, $recv

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

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

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

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

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

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

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

    ; Create a GUI for messages
    ;==============================================
    $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)
    $edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
    GUISetState()

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

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

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

    ;Wait for and Accept a connection
    ;==============================================
    Do
    $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    TCPShutdown()
    EndFunc ;==>Example

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

    ; Function to return IP Address from a connected socket.
    ;----------------------------------------------------------------------
    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), "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

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

    $sockaddr = 0

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

    Return $aRet
    EndFunc ;==>SocketToIP

    [/autoit]
    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>

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

    Opt('MustDeclareVars', 1)

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

    ;==============================================
    ;==============================================
    ;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
    ;==============================================
    ;==============================================

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

    Example()

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

    Func Example()
    ; Set Some reusable info
    ;--------------------------
    Local $ConnectedSocket, $szData
    ; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
    ; Local $szServerPC = @ComputerName
    ; Local $szIPADDRESS = TCPNameToIP($szServerPC)
    Local $szIPADDRESS = @IPAddress1
    Local $nPORT = 33891

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

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

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

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

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

    ;Attempt to connect to SERVER at its IP and PORT 33891
    ;=======================================================
    $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)

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

    ; If there is an error... show it
    If @error Then
    MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
    ; If there is no error loop an inputbox for data
    ; to send to the SERVER.
    Else
    ;Loop forever asking for data to send to the SERVER
    While 1
    ; InputBox for data to transmit
    $szData = InputBox("Data for Server", @LF & @LF & "Enter data to transmit to the SERVER:")

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

    ; If they cancel the InputBox or leave it blank we exit our forever loop
    If @error Or $szData = "" Then ExitLoop

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

    ; We should have data in $szData... lets attempt to send it through our connected socket.
    TCPSend($ConnectedSocket, $szData)

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

    ; If the send failed with @error then the socket has disconnected
    ;----------------------------------------------------------------
    If @error Then ExitLoop
    WEnd
    EndIf
    EndFunc ;==>Example

    [/autoit]
  • hi,

    Sry, das ich erst jetz antworte, aber ich hab deinem Post gerade erst gesehen.

    Ich hab lange nicht mehr am Code geschrieben und weiß daher nicht genau wieso dieser Fehler auftritt. Es kann aber sein das es vielleicht ein alter Code war, ich habe mal den Code aktualisiert, der neue Code funzt zumindest bei mir einwandfrei...ich hoffe es ist auch bei dir so. ;)