Socket communication

Hi,
I developed webSocket communication: client developed in HTML5 and server in xojo.
All works fine, but I can’t send messages from server to client.
I read on the internet that: data frames are delimited by the bytes 0x00 e 0xFF and have UTF-8 text between them
I don’t know how convert 0x00 and 0xFF to bytes and pass them to Write() method of TCPSocket.
I should write something like this:

self.Write(0x00)
self.Write("Hello client")
self.Write(0xFF)

But write() method accepts string.

I found a c# code on the internet, but I don’t know how to convert it in xojo language.

private void SendDataToClient(byte[] toSendBack)
        {
            //data frames are delimeted by the bytes 0x00 e 0xFF and 
            //have UTF-8 text between them
            try
            {
                _socketClient.Send(BitConverter.GetBytes(0x00));
                _socketClient.Send(toSendBack);
                _socketClient.Send(BitConverter.GetBytes(0xFF));
            }
            catch
            {
                Shutdown();
            }
        }

Can you help me?
Thanks.

Hi,

I have been doing IPC and sockets for years and the convention of sending 0x00 and then the data followed by a 0xFF is just a convention that the developer of the C# code implemented. There are no actual conventions except the one(s) that you decide to implement.

I am just starting with Xojo so I cannot show you some XoJo code but I can show you the multi-platform C IPC code I have been using for years.

TCP low level API: http://www.future-lab.com/software/library/ipcomm.html
IPC client API: http://www.future-lab.com/software/library/ipclient.html
IPC server API: http://www.future-lab.com/software/library/ipcsrv.html

The IPC API’s work on any Windows version 95 >, any Linux, any Unix and any version of Mac OS X. The IPC API’s also do QNX message passing, you can ignore those parts of the code.

Download at : http://www.future-lab.com/download/index.html

Just download the source only package.

Good luck,
Rick

use ChrB() to convert a number from 0x00 - 0xFF to a single character to send.

me.Write(chrb(&x00))
me.Write(“the message to send”)
me.Write(chrb(&xff))

[quote=46140:@Tim Hare]use ChrB() to convert a number from 0x00 - 0xFF to a single character to send.

me.Write(chrb(&x00))
me.Write(“the message to send”)
me.Write(chrb(&xff))[/quote]
Syntax error. ChrB doesn’t accept &x00 format.

Ok, the correct syntax is &h00 and &hFF

Now I need an equivalent method of php pack() function: http://php.net/pack

pack('CCn', $b1, 126, $length);

I must pack data into binary string

I have used the ascii values in decimal while converting them to Binary with ChrB like Tim mentioned above.

me.Write(ChrB(00)) // Null 0x00
me.Write("the message to send")
me.Write(chrB(255)) // 0xFF

Also here is also a function I use to pass decimal and it returns the binary value.

Private Function mConvertDecToBin(InputString as String) as String
  // Convert Input String from Decimal into ASCII Binary
  Dim Process as String
  Process = ChrB(Val(InputString))
  Return Process
End Function

Cristian,
Here is a function I wrote to build a TELNET ECHO message response.

I call this function and return the “packed frame(s)” in preparation for the “Write” to the socket. In this example I am using a bunch of Constants to represent decimal values. For example “cIAC” is 255, “cDO” is 253, and “cECHO” is 01. After this function happens the frame for example has a binary value of FFFD01. HTH. In this case I know already that my frame size has to be and is 3 Bytes long.

Private Function mBuild_ECHO_ResponseFrames(OptionVerb as String) As string
  Dim Response As String
  
  If OptionVerb = Str(cWILL) Then
    
    If StateBit_ECHO_InMode = False Then
      If UserEcho = True Then
        Response = ChrB(cIAC) + ChrB(cDO) + ChrB(cECHO)
        StateBit_ECHO_InMode = True
        
      Elseif UserEcho = False Then
        Response = ChrB(cIAC) + ChrB(cDONT) + ChrB(cECHO)
        StateBit_ECHO_InMode = False
      End If
      
    Elseif StateBit_ECHO_InMode = True Then
      // Do Nothing as we are in Mode already
    End If
    
  Elseif OptionVerb = Str(cDO)  Then
    If StateBit_ECHO_InMode = False Then
      If UserEcho = True Then
        Response = ChrB(cIAC) + ChrB(cWILL) + ChrB(cECHO)
        StateBit_ECHO_InMode = True
      Elseif UserEcho = False Then
        Response = ChrB(cIAC) + ChrB(cWONT) + ChrB(cECHO)
      End If
    Elseif StateBit_ECHO_InMode = True Then
      // Do Nothing as we are in Mode already
    End If
    
  Elseif OptionVerb = Str(cWONT) Then
    // Do not Reply as server does not want this mode
    
  Elseif OptionVerb = Str(cDONT) Then
    // Do not Reply as server does not want this mode
  End If
  
  
  Return Response
  Response = ""
End Function