Sign in to follow this  
Followers 0

Packet-based TCP comms Helper

1 post in this topic

If you are talking using text based protocols, Kip's TCP.au3 event driven UDF is great. But what happens if you want to talk to a 3rd party providers device/software that talks using binary format data in packet form? What if the packet size is variable? PTCP is a wrapper around Kip's TCP.au3 that lets you focus on dealing with the packets, rather than figuring out how to determine if there is a complete packet available etc.
 For sending, you just send a binary string (the actual packet data)

 For receiving, you just get a complete packet - even if the packet size is variable!

How? When you connect, you pass an AutoIt expression that tells PTCP how to tell if a packet is complete. For fixed sized packets, that's easy - just pass the number of bytes. For variable length packets, the expression can contain references to the packet data itself, as well as the number of bytes currently in the Rx buffer. This means you can specify the packet size as some combination of bytes in the packet.

An example might be: "($iAvail>7)?((BitAND(BinaryMid($aPacket,7,1), '0xff'))+(BitShift(BitAND(BinaryMid($aPacket,8,1),'0xff'),-8))+8):(0)"

This says: we need at least 8 bytes of the packet to know the packet size; once we have these 8 bytes, the packet size is stored in bytes 7 & 8 as little-endian (LSB first)

Other than the packetizing details, it's pretty much just Kip's event driven TCP code.

Hope you find it useful.


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  
Followers 0

  • Similar Content

    • Rimoun
      By Rimoun
      Hello Everyone
      this is the first time to post a problem with autoit, I have been using it for more than 3 years but im still feel new
      I have a VPS running online game ,
    • algiuxas
      By algiuxas
      Hello everybody,
      I'm making an program witch reads packets from server/client, first of all it reads size of the packet(it may be 1-5 bytes in size), and then reads what's left.

      Problem: TCPRecv($socket,1,1) tooks really long to receive 1 byte, most of the time 100ms!
      You can see packets here and how much time it took to read size of them(1-5 bytes, all of them are 1 byte in size): 0xAA63 (99.8802703210873ms) 0x53 (99.8829718030123ms) 0x2B (99.7435753356861ms) 0x4E (100.176352740059ms) I want to interrupt sometimes server or client, send packets, but I cannot send packets if server/client didn't get last packet. Sometimes packets are huge(16MB or even more), sometimes they are 2-6 bytes in size. I just don't want to receive everything and just send it. I want to see every packet, and collect data from these packets.
      EDIT: I receive couple packets at the same time with TCPRecv to make it faster. I use TCPRecv($socket,10000,1). It made program a bit faster... But still, when receiving again these packets, it still tooks 100ms. It's still slow.

      How to make TCPRecv($socket,1,1) faster? I don't need any error checking or etc., just to make it as fast as possible. (I only receive everything in hexadecimal)

      I really need help with this... It's so slow and annoying!
    • ripdad
      By ripdad
      This script is based on algorithm code from EnrMa.
      Updated: January 07, 2017
      Changes are in the script header.
      Known Issues:
      POST is not working consistently in uploading files. AutoIt x64 does not work properly with this script.  
    • Turtlix21
      By Turtlix21
      I've a problem with receiving data on my PC by TCP and UDP. I can send data to another computer, but can't receive it.
      I'm not shure where is the problem (on PC or on "another comupers")
      This is client code:
      ;TCP client TCPStartup() $socket = TCPConnect("", 7777) ;try to connect to server and save number of socket If $socket = -1 Then ;if $socket = -1 then error MsgBox(16, "Error:", "Can't connect to server") EndIf $sendedBytes = TCPSend($socket, "nothing here :)") ;send message to connected socket If $sendedBytes = 0 Then ;if receiving data TCPSend(...) = 0 then error MsgBox(16, "Error", "Can't send message") EndIf TCPCloseSocket($socket) TCPShutdown() and server code:
      ;TCP server TCPStartup() $mainsocket = TCPListen("", 7777) ;making main receiving socket While 1 ;receiving loop $acceptedSocket = TCPAccept($mainsocket) ;possible connection to accept If $acceptedSocket <> -1 Then $receivedData = TCPRecv($acceptedSocket, 1024) ;if main socket is connected then receive message MsgBox(64, "Received message!", "Message: " & $receivedData) TCPCloseSocket($acceptedSocket) ;close open connecion EndIf WEnd TCPShutdown()  
      When server is on PC I can't receive any messages from any other computers.
      When client is on PC I can receive messages on any another computer.
      I tried to turn off antiviruses and windows firewall but it did't change anything
      Error code from client from TCPConnect is 10060 (connection time out) and from TCPSend: 10038
      I've found something about 10060 error code, Microsoft says "Connection timed out. A connection attempt failed because the connected party did not properly respond after a period of time, or the established connection failed because the connected host has failed to respond."
      So it means that the problem is with time of response but it makes no sense couse "another computer" is Virtual Machine with bridge internet connection from PC (pings beetwen PC  and VM are lower than 1ms.
      About 10038 error code microsoft says that this is problem with socket and it actually makes sense.
    • Blank517
      By Blank517
      Hi, when a client sends /logout to the server i want that the server find the id of the client and set his online status to 0
      Using 'default' I have no problems, but when I do this with 'blank517' gives me id 10 and then for the database remains online
      Database while Blank517 logout:
      id  |  username  |  password |   permissions  |  online  |
      0   |  default        |    pass1      |           0             |      0       |
      1   |  Blank517    |     pass       |           0              |     1       |
      Server recv:
      Func _Recv_From_Sockets_() For $0 = 1 To $max_connections $Recv = TCPRecv ($Socket_Data[$0][0],1024) If StringLeft($Recv, 1) = "/" Then If StringInStr($Recv, "logout") Then _SQLite_Query(-1, "SELECT id FROM Users WHERE username = '" & $Socket_Data[$0][1] & "' AND online = '1';", $hQuery) While _SQLite_FetchData($hQuery, $aRow) = $SQLITE_OK $sMsg &= $aRow[0]; <-- $sMsg = 10 after FetchData WEnd _SQLite_Exec(-1, "UPDATE Users SET online = '0' WHERE id = '" & $sMsg & "';") For $000 = 1 To $max_connections TCPSend($Socket_Data[$000][0], $Socket_Data[$0][1] & " ha effettuato il logout") Next TCPCloseSocket($Socket_Data[$0][0]) $sMsg = Null EndIf Else _Broadcast_To_Sockets_ ($Recv) EndIf Next EndFunc *excuse me for my bad english *