Page 1 of 1

How do I process datagram data from mergSocket?

Posted: Sat Aug 27, 2016 10:11 pm
by MrFollies
I'm writing a simple discovery client/server app using Java (for the server) and a LiveCode app on iOS for the client.
The idea is that the client (iOS) will find it's server on the local lan via a UDP broadcast.

The server responds to a broadcast request just fine and I am able to get a response into LiveCode.
However the data is somehow coded differently to normal strings. It's a form of binary data that some functions struggle to process as a string.

The java code to send the datagram looks like this (sendData is a byte array):
serverSocket.send(new DatagramPacket(sendData, 1024, IPAddress, 9877));


The LiveCode that receives this packet looks like this:
mergSocketAcceptConnections 9877,true

I tried putting the data received into a database using revExecuteSQL, but I get an "Unrecognised token" error returned.
The message at the moment is simply a ip address on the first line, and a port number on the second line. I'll use these two things to create a http connection to the server.

When I try and use the data to make a connection via "get URL data" I get an "unsupported URL" error.

I need to either format the data before sending it from Java or process it after receiving it in LiveCode.
I suspect it could be the fact that the data put into the string is indeed the string I want, plus a lot of "\0" characters padded out to the 1024 byte size of the byte array.
So I guess my question is, how can I format the string that I get back properly?

Re: How do I process datagram data from mergSocket?

Posted: Sat Aug 27, 2016 11:05 pm
by MrFollies
I've found the answer:
I was sending a load of null characters with the datagram packet. These also made it into the string, but of course depending on the function, it either displayed correctly or caused the function to fail.
Solution was to only send the data I need and not the entire array. i.e.

Code: Select all

String myData = "Some data, that I need to send to the client...";
sendData = myData.getBytes();
DatagramPacket(sendData, myData.length(), IPAddress, 9877));
But it would be nice to know how to fix strings like this in LiveCode. Trim didn't work...