Jump to content

Fun with TCP Sockets...


Recommended Posts

In an attemt to learn autoit and tinker with socket connections at the same time...

I'm trying to create an app that can send text / execute script on a remote box via a socket connect and using hotkeys... just trying to K.I.S.S it.. ie hit shift alt + A and send "Hello World" hit shift alt + b and open Notepad on the remote box (function server side that would execute based off of the key words received off of client side)

Ninja'n working code off of the example forums I can get one script to send another script but if i try to run TCPSendMessage's only when a hotkey is pressed... it works... but the server constantly is reporting a "-1" is being received from the client as well...

which would kinda make sense... because there is a function that reports:

[script]

Func TCPReceive($ConnectedSocket, $MaxChar = 512)

Local $Data

$Data = TCPRecv($ConnectedSocket, $MaxChar)

Select

Case $Data = ""

SetError(@Error)

Return -1

Case Else

SetError(0)

Return $Data

EndSelect

EndFunc

[/script]

but then again... why does the server act like its constantly receiving data from an otherwise idle connection?

my hotkey client that is trying to send is...

#include 'tcp_func.au3'
TCPStartup()
$Socket = TCPConnect("68.90.110.80", 7777)

HotKeySet("+!d", "SendData") ;Shift-Alt-d

Func SendData()
     TCPSendMessage($Socket, "This is a test!")
EndFunc

while 1
    sleep (1000)
wend  

TCPCloseSocket($Socket)
TCPShutdown()

if I need to post everything i can.. i'm just assuming I'm missing something stupid because ima noob :)

Don't let that status fool you, I am no advanced memeber!

Link to comment
Share on other sites

Is that your full code? TCP should have a client and a server.

TCP Server...

#include 'tcp_func.au3'
Local $ConnectedSocket = -1
$MainSocket  = TCPStartServer(7777)
While 1
    If $ConnectedSocket = -1 Then $ConnectedSocket = TCPAllowConnection($MainSocket)
    $Data = TCPReceive($ConnectedSocket)
    If @Error = 0 Then MsgBox(0, "Message Received", $Data)
    Sleep(100)
WEnd
TCPStopServer($MainSocket, $ConnectedSocket)

TCP Client (thats flooding bogus data.. or to word better makes the server seem like its constantly receiving instead of being idle...

#include 'tcp_func.au3'
TCPStartup()
$Socket = TCPConnect("192.168.1.10", 7777)

HotKeySet("+!d", "SendData");Shift-Alt-d

Func SendData()
     TCPSendMessage($Socket, "This is a test!")
EndFunc

while 1
    sleep (1000)
wend  

TCPCloseSocket($Socket)
TCPShutdown()

the include file...

#include-once
; ----------------------------------------------------------------------------
; AutoIt Version: 3.1.1.92
; ----------------------------------------------------------------------------
; Function: TCPStartServer($Port[, $MaxConnection])
;    $Port = Port To Start Server On
;    $MaxConnection = (Optional) Maximum Connections Allowed; Default 1
;     
; Returns:
;    Success : Returns Socket and @Error = 0
;     Failure : Returns 0 or 1 and sets @Error to Windows API WSAGetLasterror
;           0 = TCP Services Not Avialible On That Port
;           -1 = TCP Services Would Not StartUp                 
; ----------------------------------------------------------------------------
Func TCPStartServer($Port, $MaxConnect = 1)
    Local $Socket
    $Socket = TCPStartup()
    Select
    Case $Socket = 0
        SetError(@Error)
        Return -1
    EndSelect
    $Socket = TCPListen(@IpAddress1, $Port, $MaxConnect)
    Select
    Case $Socket = -1
        SetError(@Error)
        Return 0
    EndSelect
    SetError(0)
    Return $Socket
EndFunc
; ----------------------------------------------------------------------------
; Function: TCPAllowConnection($Socket)
;    $Socket = The Main Socket As Returned By TCPStartServer
;     
; Returns:
;    Success : Returns ConnectedSocket and @Error = 0
;     Failure : Returns -1 and sets @Error to Windows API WSAGetLasterror             
; ----------------------------------------------------------------------------
Func TCPAllowConnection($Socket)
    Local $ConnectedSocket
    $ConnectedSocket = TCPAccept($Socket)
    Select
    Case $ConnectedSocket >= 0
        SetError(0)
        Return $ConnectedSocket
    Case Else
        SetError(@Error)
        Return -1
    EndSelect
EndFunc
; ----------------------------------------------------------------------------
; Function: TCPReceive($ConnectedSocket, $MaxChar)
;    $ConnectedSocket = The Connected Socket As Returned By TCPAllowConnection
;     
; Returns:
;    Success : Returns Recieved String and @Error = 0
;     Failure : Returns -1 and sets @Error to Windows API WSAGetLasterror             
; ----------------------------------------------------------------------------
Func TCPReceive($ConnectedSocket, $MaxChar = 512)
    Local $Data
    $Data = TCPRecv($ConnectedSocket, $MaxChar)
    Select
    Case $Data = ""
        SetError(@Error)
        Return -1 ;For some reason holding a socket open with no data being sent will constantly report this
    Case Else
        SetError(0)
        Return $Data
    EndSelect
EndFunc
; ----------------------------------------------------------------------------
; Function: TCPSendMessage($ConnectedSocket, $String)
;    $ConnectedSocket = The Connected Socket As Returned By TCPAllowConnection
;    $String = Data to be sent
;     
; Returns:
;    Success : Returns 1 and @Error = 0
;     Failure : Returns -1 or 0 and sets @Error to Windows API WSAGetLasterror
;    
; ----------------------------------------------------------------------------
Func TCPSendMessage($ConnectedSocket, $String)
    Local $Sent
    $Sent = TCPSend($ConnectedSocket, $String)
    Select
    Case $Sent = 0
        SetError(@Error)
        Return -1
    Case Else
        SetError(0)
        Return 1
    EndSelect
EndFunc
; ----------------------------------------------------------------------------
; Function: TCPStopServer($MainSocket, $ConnectedSocket)
;    $MainSocket = The Socket As Returned By TCPStartServer
;    $ConnectedSocket = The Connected Socket As Returned By TCPAllowConnection
;     
; Returns:
;    Success : Returns 1 and @Error = 0
;     Failure : Returns -1 and sets @Error to Windows API WSAGetLasterror
;           0 = Unable To Shutdown TCP Services
;          -1 = Unable To Close Socket/s
; ----------------------------------------------------------------------------
Func TCPStopServer($MainSocket, $ConnectedSocket)
    Local $Shutdown
    Select
    Case $ConnectedSocket <> -1 
        $ConnectedSocket = TCPCloseSocket($ConnectedSocket)
        Select
        Case $ConnectedSocket = 0
            SetError(@Error)
            Return -1
        EndSelect
    EndSelect
    Select
    Case $MainSocket <> -1
        $MainSocket = TCPCloseSocket($MainSocket)
        Select
        Case $MainSocket = 0
            SetError(@Error)
            Return -1
        EndSelect
    EndSelect
    $Shutdown = TCPShutdown()
    Select
    Case $Shutdown = 0
        SetError(@Error)
        Return 0
    EndSelect
    SetError(0)
    Return 1
EndFunc
[\code]


            
                


    Edited  by zhenyalix
    
    

            
        

        

        
            

    
        

        
            Don't let that status fool you, I am no advanced memeber!
        
    

        
    

    

    




    Link to comment
    
        
    
    
    

    
    Share on other sites
    

    
        
            

    

        
            

    

        
            

    

        
            

    

        
    


    
    More sharing options...

    


    

                    
                    
                    
                

                    

                    
                    





    

    

    
        
            
                


    
        
    

                
                
                    
                        

                    
                
            
        
        
            
                


MuffettsMan
            
            
                Posted 
                
            
        
    
    
        


MuffettsMan
            
        
        
            
                
                    


    
        
    

                    
                    
                        

                    
                
            
            
                Active Members
                
            
            
                
                    
                        
                            
                                
                            
                                 183
                            
                                
                            
                        
                        
                            
                                
                                    
                                        
                                        1
                                
                                    
                                
                            
                        
                    
                
            
            
                

            
        
    
    
        



    
        
            
                
                    
                        Author
                    
                    
                    
                    
                    
                
            
            
                
                    
                    
                        
                        
                            Share
                        
                        
                        
                        
                        
                            
                                
                            
                            
                            
                            
                            
                            
                        
                    
                
                
            
        

        
            Posted 
            
            
                
                
            
        
    

    

    

    
        
        
            
forinstance.... if i edit the server line to this... I obviously won't get the spam:

#include 'tcp_func.au3'
Local $ConnectedSocket = -1
$MainSocket  = TCPStartServer(7777)
While 1
    If $ConnectedSocket = -1 Then $ConnectedSocket = TCPAllowConnection($MainSocket)
    $Data = TCPReceive($ConnectedSocket)
    If @Error = 0 and $Data <> -1 Then MsgBox(0, "Message Received", $Data)
    Sleep(100)
WEnd
TCPStopServer($MainSocket, $ConnectedSocket)

but if the only thing thats keeping the message box from popping up is "and $Data <> -1" then that means the server still is constantly thinking its receiving data from the client :)

Don't let that status fool you, I am no advanced memeber!

Link to comment
Share on other sites

This is a short TCP example:

Client:

TCPStartup()
$socket = TCPConnect(@IPAddress1, 7777)
While 1
    $msg = InputBox("Command", "Enter command to send to server:")
    If @error Then Exit
    If $msg <> "" Then
        TCPSend($socket, $msg)
        If StringLower($msg) == "exit" Then Exit
    EndIf
WEnd
Func OnAutoitExit()
    TCPCloseSocket($socket)
    TCPShutdown()
EndFuncoÝ÷ Ù'«½êÚºÚ"µÍÔÝ

BÌÍÛXZ[ÛØÚÙ]HÔÝ[TYÜÌK
ÍÍÍÊBÂIÌÍÜÛØÚÙ]HÔXØÙ
    ÌÍÛXZ[ÛØÚÙ]
B[[ ÌÍÜÛØÚÙ] ÝÈÚ[HB   ÌÍÙ]HHÔXÝ  ÌÍÜÛØÚÙ]
LLBY    ÌÍÙ]H    ÉÝÈ  ][ÝÉ][ÝÈ[BTÙ[XÝBBPØÙHÝ[Ò[ÝÝ[ÓÝÙ  ÌÍÙ]JK   ][ÝÙ^]    ][ÝÊBBBBQ^]BBPØÙHÝ[ÓY
Ý[ÓÝÙ   ÌÍÙ]JK
HOH ][ÝÛÙØÞ    ][ÝÂBBBSÙÐÞ
    ][ÝÉ][ÝËÝ[Õ[SY
    ÌÍÙ]K
ÊJBBBPØÙH[ÙBBBBSÙÐÞ
M   ][ÝÑÜ][ÝË  ][ÝÑÜÌÌÎÈ[ÛÝÛÛÛ[X[][ÝÊBBQ[Ù[XÝQ[YÛY
L
BÑ[[ÈÛ]]Ú]^]

BUÔÛÜÙTÛØÚÙ]
    ÌÍÜÛØÚÙ]
BUÔÚ]ÝÛ
B[[
Link to comment
Share on other sites

on that example... if the client connects and disconnects does the server / port have to be bounced for other clients to conect? other issue it seems to have is once a client disconnects the server though still running is non responsive (starting up another client will not get it to respond... end up having to bounce the server completely

Don't let that status fool you, I am no advanced memeber!

Link to comment
Share on other sites

Just modify servers code to wait for a connection if client disconnects:

TCPStartup()
$mainsocket = TCPListen(@IPAddress1, 7777)
Do
    $socket = TCPAccept($mainsocket)
Until $socket > 0
While 1
    $data = TCPRecv($socket, 512)
    If @error Then
        Do
            $socket = TCPAccept($mainsocket)
        Until $socket > 0
    EndIf
    If $data <> "" Then 
        Select
            Case StringInStr(StringLower($data), "exit")
                Exit
            Case StringLeft(StringLower($data), 6) == "msgbox"
                MsgBox(0, "", StringTrimLeft($data, 7))
            Case Else
                MsgBox(16, "Error", "Error! Unknown command.")
        EndSelect
    EndIf
    Sleep(10)
WEnd
Func OnAutoitExit()
    TCPCloseSocket($socket)
    TCPShutdown()
EndFunc

Oh, and in client, commands are "exit" and "msgbox text_to_be_displayed_in_msgbox".

Link to comment
Share on other sites

whoot :P thats what i was trying to go for... something simple enough for me to understand and now tinker with thx thx... I butchered the client slightly and now i have my hotkeys :)

TCPStartup()
HotKeySet("+!d", "SendData");Shift-Alt-d
$socket = TCPConnect("192.168.0.10", 7777)
dim $data2send
While 1
  ;$msg = InputBox("Command", "Enter command to send to server:")
  ;If @error Then Exit
  ;If $msg <> "" Then
    If $data2send <> "" Then
        TCPSend($socket, $data2send)
        If StringLower($data2send) == "exit" Then Exit
        $data2send = ""
    EndIf
WEnd

Func SendData()
  ;MsgBox(4096,"","Hotkey... atempting to send",1)
    $data2send = InputBox("Command", "Enter command to send to server:")
EndFunc

Func OnAutoitExit()
    MsgBox(4096,"","Closing Socket Connection", 1)
    TCPCloseSocket($socket)
    TCPShutdown()
EndFunc
Edited by zhenyalix

Don't let that status fool you, I am no advanced memeber!

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...