Jump to content

AutoIt andRecv (packets)


Recommended Posts

Greetings,

I am new to AutoIt and I never coded with anything network related. Here is what I would like to do: when I receive a specific network packet from a specific IP I would like to execute a specific routine.

That routine is already coded, I only need the network part and I have no clue how to create it.

I was able to get the packet I need to check with WPE Pro:

Posted Image

Can anyone help me out please?

Thanks a lot in advance for your help and support.

Link to comment
Share on other sites

Welcome to the AUtoIt forums RyuHayabusa ;)

Have you looked at the TCP* functions in the help?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Martin, he wants to get packets from another program. Doesn't the TCP commands only allow communication between other AutoIt scripts?

No. The TCP functions uses the sends the same data as any other client using the TCP protocol. Data is always just data regardless of language or platform.

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Yeah, of course you would have to figure out how the data is formatted by the other application, but that's always an issue when dealing with other application.

To test, try running this script:

TCPStartup()
$listen = TCPListen(@IPAddress1,12345)
ConsoleWrite(@IPAddress1&@CRLF)

While True
    $socket = TCPAccept($listen)

    While True
        $data = TCPRecv($socket,4096)
        If @error Then ExitLoop
        ConsoleWrite($data)
        Sleep(1)
    WEnd
    Sleep(1)
WEnd

Then start cmd and type "telnet YOURIP 12345" (install telnet from control panel if you're on vista+) and start typing things after the connection is enabled.

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Greetings,

Thanks a lot for your help, I was able to trap what packets are coming but how can I see if a specific one (like in my first post) is being received. For instance, from the packet in my first post, what is the information that I should "check" on the listen?

Thanks.

Link to comment
Share on other sites

Greetings,

What I mean is what information on the following packet would I need to check with the "If" clause:

Posted Image

Do you mean how do you check that you have received that particular packet?

If so then you have to make sure that you receive it as binary because if you receive it as a string you will loose the second two strings. The data in that packet is either binary or it is 3 strings because 0x00 is used to mark the end of a string. So if you receive is as a string then you will only see the first one.

$stringToFind1 = "GDF|298;3;0"
$StringToFind2 = "Im0144"
$StringToFind3 = "JX|36;5268960;71741;72385" 

; ignore the strings and look for binary data
$binaryToFind = Binary('0x4744467C3239383B333B3000496D30313434004A587C33363B353236383936303B37313734313B373233383500')

;or, don't ignore the strings but convert them to binary
$binaryToFind = Binary(StringToBinary($stringToFind1) & "00" & STringReplace(StringToBinary($stringToFind2),"0x","") & "00" & StringReplace(StringToBinary($stringToFind3),"0x","") & "00" )

While 1
 $Recv = TcpRecv($Listen, $Size,1)

 If $Recv = $binaryToFind then
 ;You got the packet
 exitloop; or dosomething()
 Endif
 
Wend

To extract strings from binary data can be a bit messy, because if you use BinaryToString for binary data which contains more than one string you will get a string which is the length of all the strings but you will only see the first one.

So you could do this

;say $Recv is the received binary data

$s1 = BinaryToString($Recv)
Dim $ss[5]
$index = 0

for $n = 1 to stringlen($s1)
    ;ConsoleWrite(hex(asc(stringmid($s1,$n,1)),2) & @CRLF)
    if stringmid($s1,$n,1)='' Then
        $index += 1
        if $index > ubound($ss) then Redim $ss[$index+4]
    Else
    $ss[$index] &= stringmid($s1,$n,1)
    EndIf

Next

for $n = 0 to $index;show the extracted strings
    ConsoleWrite($ss[$n] & @CRLF)
Next

EDIT: I might be wrong about receiving the dat as a string. It could be that the string woulb be exactly the same as converting the binary data to a string, but you would still need to extract the strings as I showed, or some better way.

If I haven't confused you with this post then I must try harder ;)

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
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...