postak05

"struct" packet from AutoIt to TCP server written in C

12 posts in this topic

Dear Gurus!

I'm to trying to communicate from AutoIt with chat server written in C via packets.

Server side (as described) is written in C and is using packet formatted as "struct".

    #define BUFFSIZE 1024
    #define ALIASLEN 32
    #define OPTLEN 16
     
    struct PACKET {
        char option[OPTLEN]; // instruction
        char alias[ALIASLEN]; // client's alias
        char buff[BUFFSIZE]; // payload
    };
    
    ...
    ...
    ...
    ...
    bytes = recv(threadinfo.sockfd, (void *)&packet, sizeof(struct PACKET), 0);
    printf("[%d] %s %s %s\n", threadinfo.sockfd, packet.option, packet.alias, packet.buff);
    ...
    ...
    ...
    ...

I'm trying to send packet from AutoIt with same structure:

#include <MsgBoxConstants.au3>
#include <Misc.au3>

Opt( "TrayAutoPause", 0 )
Opt( "TrayMenuMode", 1 )
Opt( "TrayOnEventMode", 1 )
Opt( "GUIOnEventMode", 1 )
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1,"_Exit")

TCPStartup()

Global Const $tagSTRUCT1 = "struct;char option[16];char alias[32];char buff[1024];endstruct"
Global $tSTRUCT1 = DllStructCreate($tagSTRUCT1)
DllStructSetData($tSTRUCT1, "option", "alias")
DllStructSetData($tSTRUCT1, "alias", "postak")
DllStructSetData($tSTRUCT1, "buff", "")

Global $iSock = TCPConnect("server_ip_address", $port)
If @error Then
   _Exit
Else
  TCPSend($iSock, $tSTRUCT1)
EndIf

 Func _Exit()
    $tSTRUCT1 = 0
    Exit 0
 EndFunc

But the packet on server-side is not received correctly (checking packet.option for text "alias"). If I change code to

TCPSend($iSock, "alias")

packet.option on serverside is read as "alias", but I don't know how to set next 2 variables.

 

Can anybody help me, please? Thanks in advance

postak

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

As far as i know, TCPSend does not accept structs as the data parameter. So i would suggest one of two solutions.

as string:

$tData = DllStructCreate("CHAR data["&DllStructGetSize($tSTRUCT1)&"]", DllStructGetPtr($tSTRUCT1))
TCPSend($iSock, DllStructGetData($tData, 1))

;or unicode below:

$tData = DllStructCreate("WCHAR data["&DllStructGetSize($tSTRUCT1)/2&"]", DllStructGetPtr($tSTRUCT1))
TCPSend($iSock, DllStructGetData($tData, 1))

or as binary

$tData = DllStructCreate("BYTE data["&DllStructGetSize($tSTRUCT1)&"]", DllStructGetPtr($tSTRUCT1))
TCPSend($iSock, DllStructGetData($tData, 1))

 

Edited by genius257
Forgot original struct pointer reference & struct def. mistype

Share this post


Link to post
Share on other sites

thanks, try it ASAP!

 

postak

Share this post


Link to post
Share on other sites
1 hour ago, genius257 said:

As far as i know, TCPSend does not accept structs as the data parameter. So i would suggest one of two solutions.

as string:

$tData = DllStructCreate("CHAR data["&DllStructGetSize($tSTRUCT1)&"]", DllStructGetPtr($tSTRUCT1))
TCPSend($iSock, DllStructGetData($tData, 1))

;or unicode below:

$tData = DllStructCreate("WCHAR data["&DllStructGetSize($tSTRUCT1)/2&"]", DllStructGetPtr($tSTRUCT1))
TCPSend($iSock, DllStructGetData($tData, 1))

or as binary

$tData = DllStructCreate("BYTE data["&DllStructGetSize($tSTRUCT1)&"]", DllStructGetPtr($tSTRUCT1))
TCPSend($iSock, DllStructGetData($tData, 1))

 

seems only binary version is working fine!! Will investigate tomorrow more. BIG THANK YOU and Merry XMAS and a happy New Year!

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

25 minutes ago, postak05 said:

seems only binary version is working fine!! Will investigate tomorrow more. BIG THANK YOU and Merry XMAS and a happy New Year!

Great! :D

I did some checking, and the reason binary is working, but not char/wchar is because not all chars are set. Here is a small example:

$t1 = DllStructCreate("char a[2];char b[2]")
$t1.a = "ab"
$t1.b = "ba"
$t2 = DllStructCreate("char a[4]", DllStructGetPtr($t1))
MsgBox(0, "", DllStructGetData($t2, 1));will show "abba". No problem!

$t1.a = "a"
$t1.b = "ba"
$t2 = DllStructCreate("char a[4]", DllStructGetPtr($t1))
MsgBox(0, "", DllStructGetData($t2, 1));will show "a". Problem!

So just to re-cap: the data IS within the struct, but extracting when all chars have not been set, will result in string until first chr(0)

 

Edit:

Also: in the end binary makes more sense. You can send different types, not just strings and re-structure on your server end.

Edited by genius257

Share this post


Link to post
Share on other sites

I'm very satisfied with any version working. If you are interested and have a few seconds, can you help me to construct "reverse direction scenario"? So AutoIt receive packet from C server with this structure and I have to split it into variables (option, alias, buff)?

As I'm looking to your last post it is not enough to do:
 

$option = DllStructGetData($packet, 1));
$alias = DllStructGetData($packet, 2));
$buff = DllStructGetData($packet, 3));

Thanks! postak

 

Share this post


Link to post
Share on other sites

I did some research and I can get all variables with

 

Global Const $OPTLEN = 16
Global Const $ALIASLEN = 32
Global Const $BUFFLEN = 1024

$option = BinaryToString(BinaryMid($packet,1,$OPTLEN))
$alias = BinaryToString(BinaryMid($packet,$OPTLEN+1,$ALIASLEN))
$buffer = BinaryToString(BinaryMid($packet,$OPTLEN+$ALIASLEN+1,$BUFFLEN))

but not sure if this is the "best" and easiest way according to "struct" being used :) (at least it works somehow :D )

Share this post


Link to post
Share on other sites

Rather that messing with individual fields, try this, which uses a binary overlay structure (like C union):

#include <MsgBoxConstants.au3>
#include <Misc.au3>

Opt( "TrayAutoPause", 0 )
Opt( "TrayMenuMode", 1 )
Opt( "TrayOnEventMode", 1 )
Opt( "GUIOnEventMode", 1 )
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1,"_Exit")

TCPStartup()

Global Const $tagSTRUCT1 = "char option[16];char alias[32];char buff[1024]"
Global $tSTRUCT1 = DllStructCreate($tagSTRUCT1)
Local $tBinBuffer = DllStructCreate("byte[" & DllStructGetSize($tSTRUCT1) & "]", $tSTRUCT1)
DllStructSetData($tSTRUCT1, "option", "alias")
DllStructSetData($tSTRUCT1, "alias", "postak")
DllStructSetData($tSTRUCT1, "buff", "")

Global $iSock = TCPConnect("server_ip_address", $port)
If @error Then
   _Exit
Else
  TCPSend($iSock, DllStructGetData($tBinBuffer, 1))
EndIf

 Func _Exit()
    $tSTRUCT1 = 0
    Exit 0
 EndFunc

 


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
8 hours ago, postak05 said:

I'm very satisfied with any version working. If you are interested and have a few seconds, can you help me to construct "reverse direction scenario"? So AutoIt receive packet from C server with this structure and I have to split it into variables (option, alias, buff)?

As I'm looking to your last post it is not enough to do:
 

$option = DllStructGetData($packet, 1));
$alias = DllStructGetData($packet, 2));
$buff = DllStructGetData($packet, 3));

Thanks! postak

 

 

7 hours ago, postak05 said:

I did some research and I can get all variables with

 

Global Const $OPTLEN = 16
Global Const $ALIASLEN = 32
Global Const $BUFFLEN = 1024

$option = BinaryToString(BinaryMid($packet,1,$OPTLEN))
$alias = BinaryToString(BinaryMid($packet,$OPTLEN+1,$ALIASLEN))
$buffer = BinaryToString(BinaryMid($packet,$OPTLEN+$ALIASLEN+1,$BUFFLEN))

but not sure if this is the "best" and easiest way according to "struct" being used :) (at least it works somehow :D )

well i imagine your server response ("$packet") if from "TCPRecv", if so i recommend the "$TCP_DATA_BINARY" flag, just to be sure and do:

$tagSTRUCT1 = "char option[16];char alias[32];char buff[1024]"
$packet = Binary("test");should be from TCPRecv, this is just test data
$t = DllStructCreate("BYTE data["&DllStructGetSize(DllStructCreate($tagSTRUCT1))&"]")
DllStructSetData($t, 1, $packet)
$tSTRUCT1 = DllStructCreate($tagSTRUCT1, DllStructGetPtr($t))
MsgBox(0, "", $tSTRUCT1.option); just to show the test data is there

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

15 minutes ago, jchd said:

Rather that messing with individual fields, try this, which uses a binary overlay structure (like C union):

#include <MsgBoxConstants.au3>
#include <Misc.au3>

Opt( "TrayAutoPause", 0 )
Opt( "TrayMenuMode", 1 )
Opt( "TrayOnEventMode", 1 )
Opt( "GUIOnEventMode", 1 )
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1,"_Exit")

TCPStartup()

Global Const $tagSTRUCT1 = "char option[16];char alias[32];char buff[1024]"
Global $tSTRUCT1 = DllStructCreate($tagSTRUCT1)
Local $tBinBuffer = DllStructCreate("byte[" & DllStructGetSize($tSTRUCT1) & "]", $tSTRUCT1)
DllStructSetData($tSTRUCT1, "option", "alias")
DllStructSetData($tSTRUCT1, "alias", "postak")
DllStructSetData($tSTRUCT1, "buff", "")

Global $iSock = TCPConnect("server_ip_address", $port)
If @error Then
   _Exit
Else
  TCPSend($iSock, DllStructGetData($tBinBuffer, 1))
EndIf

 Func _Exit()
    $tSTRUCT1 = 0
    Exit 0
 EndFunc

 

isn't that basically what i did in:

18 hours ago, genius257 said:

or as binary

$tData = DllStructCreate("BYTE data["&DllStructGetSize($tSTRUCT1)&"]", DllStructGetPtr($tSTRUCT1))
TCPSend($iSock, DllStructGetData($tData, 1))

 

 

Edited by genius257
spelling *blush*

Share this post


Link to post
Share on other sites

Yes it is, sorry I didn't look back enough.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

thanks guys and have a nice Christmas time!

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