Jump to content
Sign in to follow this  
marko001

Problem sending strings with Telnet

Recommended Posts

marko001

Hi all mates,

i have this problem:

i implemented in my main program a script similar to this:

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

Example()

Func Example()
    
    Local $szIPADDRESS = @IPAddress1
    Local $nPORT = 5000
    Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
    Local $msg, $recv

    TCPStartup()

    $MainSocket = TCPListen($szIPADDRESS, $nPORT)

    If $MainSocket = -1 Then Exit


    $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)
    $edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
    GUISetState()


     $ConnectedSocket = -1


    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1
        $szIP_Accepted = SocketToIP($ConnectedSocket)
    TCPSend($ConnectedSocket, "Connected" & @CRLF & @CRLF)
    
    While 1
        $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
        $recv = TCPRecv($ConnectedSocket, 1500)
        If @error Then ExitLoop
        If $recv <> "" Then GUICtrlSetData($edit, _
            $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))

    WEnd


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

    TCPShutdown()
EndFunc  ;==>Example

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), "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

    $sockaddr = 0

    Return $aRet
EndFunc  ;==>SocketToIP

I launch it then i run TELNET from CMD.

IT correctly connects but i see that i can send to the server ONLY single letters, not complete words:

i.e.:

If $recv = "S" then msgbox(0,"...","S Pressed")
if $recv = "Status" then msgbox(0,"OK","Status received")

so if in telnet i press S T A T U S, from the client will appear the 1st msgbox and i can't NEVER write the entire word "STATUS"

How can this be solved?

Thanks,

marco

Share this post


Link to post
Share on other sites
ProgAndy

you have to wait for @CRLF:

Dim $recv = "", $Enter = False, $Temprecv
    While 1
        $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
        If Not $Enter Then
            $Temprecv = TCPRecv($ConnectedSocket, 1500)
            If @error Then ExitLoop
            If $Temprecv = @CRLF Then
                $Enter = True
            Else
                $recv &= $Temprecv
            EndIf
        Else
            GUICtrlSetData($edit, _
            $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
            TCPSend($ConnectedSocket,$szIP_Accepted & " > " & $recv & @CRLF)
            TCPSend($ConnectedSocket,"]>")
            Dim $Enter = False,$recv = ""
        EndIf
    WEnd

;The SocketToIP was wrong: Corrected:
; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
Func SocketToIP($SHOCKET)
    Local $sockaddr = DLLStructCreate("short;ushort;uint;char[8]")

    Local $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
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites
marko001

Great!!! Now it works!

I have another request i couldn't find a way to solve.

Given the same code, how can i set a password (server side) that i need to send to have access to other things?

Thanks a lot

you have to wait for @CRLF:

Dim $recv = "", $Enter = False, $Temprecv
    While 1
        $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
        If Not $Enter Then
            $Temprecv = TCPRecv($ConnectedSocket, 1500)
            If @error Then ExitLoop
            If $Temprecv = @CRLF Then
                $Enter = True
            Else
                $recv &= $Temprecv
            EndIf
        Else
            GUICtrlSetData($edit, _
            $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
            TCPSend($ConnectedSocket,$szIP_Accepted & " > " & $recv & @CRLF)
            TCPSend($ConnectedSocket,"]>")
            Dim $Enter = False,$recv = ""
        EndIf
    WEnd

;The SocketToIP was wrong: Corrected:
; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
Func SocketToIP($SHOCKET)
    Local $sockaddr = DLLStructCreate("short;ushort;uint;char[8]")

    Local $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

Share this post


Link to post
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
Sign in to follow this  

×