Jump to content

Help sending messages over network


dargx001
 Share

Recommended Posts

I've been using AutoIt for a few weeks now, and I've decided to expand my knowledge by being able to send messages over the network connection. I would eventually like to expand this to be able to send commands to computers that I manage on the network. Though for now, I am trying to get this simple task to work. Here is my code so far.

This is the server. It simply sends what is typed in the input box to the remote computer.

Global $socket, $com
TCPStartup ( )

getclient()

Func getclient()
    $socket = TCPListen("192.168.1.133", 675)
    
    If $socket = -1 Then
        Exit
    EndIf
    
    If $socket <> -1 Then
        sendcom()
    EndIf
EndFunc

Func sendcom()
    $com = InputBox("Send what to client", "")
    TCPSend($socket, $com)
EndFunc

TCPShutdown ( )

This is the client. It seems to be quitting early, and this is where I am having my problem. I can't seem to find a way for it to wait to quit until after it gets the input.

Global $socket, $reccom
TCPStartup ( )

Checkin()

Func Checkin()
    $socket = TCPConnect("192.168.1.133", 675)
    
    If $socket = -1 Then
        Exit
    EndIf
    
    If $socket <> -1 Then
        getcom()
    EndIf
    
EndFunc

Func getcom()
    
    TCPAccept($socket)  
    $reccom = TCPRecv($socket, 2048)
    MsgBox(0, "Recieved Command", $reccom)
EndFunc

TCPShutdown ( )

I know at the moment they are both on the same ip, but I would like to test it on a local machine, before I go testing it on the rest of the network. Any suggestions?

Link to comment
Share on other sites

Start with the TCPSend() and TCPRecv() examples in the help file. I think you have confused the roles of "Server" and "Client" here. TCPAccept() belongs in your "Server" script. You "Accept" a connection to a socket that your script is "Listening" to.

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Your client script is closing early because there is no loop to keep it open. Put in a While...WEnd or Do...Until loop to keep it running so it can receive the message.

Global $socket, $reccom
TCPStartup ( )

While 1
    $socket = TCPConnect("192.168.1.133", 675)
    
    If $socket = -1 Then
        Exit
    EndIf
    
    If $socket <> -1 Then
        getcom()
    EndIf
WEnd

Func getcom()
    
    TCPAccept($socket)  
    $reccom = TCPRecv($socket, 2048)
    MsgBox(0, "Recieved Command", $reccom)
EndFunc

TCPShutdown ( )

Edit: Not Tested! Just posting this edited code as an example

Edited by Gestalt
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...