Page 1 of 1

Socket data protocol with binary data size

Posted: Fri Dec 14, 2012 8:09 pm
by Paul.RaulersonBUSrLn
I am writing an application that has to run on a PC, but talks via socket communications with a Linux instance on an IBM mainframe. The problem comes in with the data format.

Each transmission has a four byte (full word, 32bit integer, etc.) size that is transmitted before the data packet. And of course, a PC is byte order reversed from the mainframe.

Rather than try to deal with that on the host side, I usually have the remote applications translate a 32bit integer with the file size in it into the host order I need and just transmit it as a binary array. I reverse that process on the way back, taking a BigEndian full word integer and translating it to a LittleEndian 32 bit integer.

I thought I could do this with binaryDecode, but for the life of me, I cannot seem to get it work. Help? Please? :?

Basic sample code below.

-Paul

Code: Select all

local XMLText
local XMLTextL
local MFFormatTextL

on mouseUp
   open socket to "zlin040:3706" with message "clientConnected" 
end mouseUp

on clientConnected pSocket
   put "<IM08XMLI><PROCCD>10</PROCCD><PIDCD></PIDCD><DESKNO>5PR</DESKNO>"  & \
         "<FILECD></FILECD><DOCTYPE>11</DOCTYPE><DOCDATE>20121214</DOCDATE>"    & \
         "<PAGECNT>0001</PAGENCT><PRIPOL>7100001301</PRIPOL><STAGEID></STAGEID>" & \
         "</IM08XMLI>" & return  \
         into XMLText 
   put length(XMLText) into XMLTextL
   add 4 to XMLTextL
      put binaryDecode("M1",XMLTextL, MFFormatTextL) into theResult 
      -- write to the socket
   write  MFFormatTextL to socket pSocket 
   write  XMLText to socket pSocket
   read  from socket pSocket  for 4 bytes
   put it into MFFormatTextL
   put zero into XMLTextL
   put binaryDecode("S1", MFFormatTextL, XMLTextL) into theResult
   answer "Return Data size is [" & XMLTextL & "]"
   subtract 4 from XMLTextL
   if XMLTextL > 0 then
      read from socket pSocket for XMLTextL bytes
      put "Received: [" & it & "]" into field txtResponse
   else 
      put "A Read error occurred, XMLTextL = " & XMLTextL into field txtResponse
   end if
   close socket pSocket
   end clientConnected

Re: Socket data protocol with binary data size

Posted: Sun Dec 16, 2012 6:10 am
by Paul.RaulersonBUSIvAg
Finally managed to stubborn my way into a solution with this. Seems to be working just fine, but if anyone has a better suggestion, I am all ears! Sample code is reproduced below.
Thanks -Paul

Code: Select all

on IM08Connected pSocket
   -- save the socket ID in a global variable
   put pSocket into IM08SockeT
   -- write the size and XML (CTI standard format)
   add 4 to XMLTextLength
   put BinaryEncode("M", XMLTextLength) into MFPacketSize
   write MFPacketSize to socket IM08Socket 
   write XMLText to socket IM08Socket
   -- read the return packet size from the mainframe
   read from socket IM08Socket for 4 bytes
   put it into rxMFLength
   put binaryDecode("N",rxMFLength,MFPacketSize) into rc
   -- read in the returned XML packet
   read from socket IM08Socket for MFPacketSize bytes
   put it into tmpText
   -- parse XML response
   put revCreateXMLTree (tmpText, true, true, true, true) into XMLResponseID
   put text of field txtResponse & return & "XML Data --> " & \
         revXMLNodeContents(XMLResponseID, "/IM08XMLO/ERRCD")  & " : "  & \
         revXMLNodeContents(XMLResponseID, "/IM08XMLO/ERRMSG")  into field txtResponse
   put text of field txtResponse & return & "Decoded Data ->" & tmpText into field txtResponse
   --
   close socket IM08Socket
end IM08Connected