I was attempting to send data to Scratch with sockets using Visual C#. I send this command:
sensor-value 'currentPressedKey' 5
Yes, I have a variable in Scratch names currentPressedKey. Anyway, I keep getting this error from Scratch:
message too big; bad size field?
My C# code is a method to send data (I picked it up from the internet):
public static void Send(Socket socket, byte[] buffer, int offset, int size, int timeout)
{
int startTickCount = Environment.TickCount;
int sent = 0; // how many bytes is already sent
do {
if (Environment.TickCount > startTickCount + timeout)
throw new Exception("Timeout.");
try {
sent += socket.Send(buffer, offset + sent, size - sent, SocketFlags.None);
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.WouldBlock ||
ex.SocketErrorCode == SocketError.IOPending ||
ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
{
// socket buffer is probably full, wait and try again
Thread.Sleep(30);
}
else
throw ex; // any serious error occurr
}
} while (sent < size);
}And the code that calls the Send method:
string commandToSend = "sensor-value 'currentPressedKey' 5"; Send(this.socket, Encoding.UTF8.GetBytes(commandToSend), 0, (int)commandToSend.Length, 100);
Anybody know what is wrong? Am I encoding it wrong or something like that?
Offline
You need to include the length of the message. http://wiki.scratch.mit.edu/wiki/Remote … s_Protocol
Offline
Magnie wrote:
You need to include the length of the message. http://wiki.scratch.mit.edu/wiki/Remote … s_Protocol
Oh. But I don't know how to do that. In words, could you explain how to turn the message into 4-byte? And after i have it, do I just add it on to the beginning of the message?
Offline
For the most part. Basically, Scratch uses big-endian for lengths--in other words, biggest byte first. So, if the length of the string was say, 7, the length in bytes would be:
0, 0, 0, 7.
If it is 255, it would be,
0, 0, 0, 255.
Note: if it is 256, then it carries, like binary does.
0, 0, 1, 1
You'll have to use bit-wise operations. Here's some sample code, taken from Kinect-scratch:
unsigned char sizeBytes[4];
int len = strlen(sendBuff);
sizeBytes[0] = (unsigned char)(len >> 24);
sizeBytes[1] = (unsigned char)((len << 8) >> 24);
sizeBytes[2] = (unsigned char)((len << 16) >> 24);
sizeBytes[3] = (unsigned char)((len << 24) >> 24);
write(newsockfd, sizeBytes, 4);
write(newsockfd, sendBuff, strlen(sendBuff));Offline
bobbybee wrote:
For the most part. Basically, Scratch uses big-endian for lengths--in other words, biggest byte first. So, if the length of the string was say, 7, the length in bytes would be:
0, 0, 0, 7.
If it is 255, it would be,
0, 0, 0, 255.
Note: if it is 256, then it carries, like binary does.
0, 0, 1, 1
You'll have to use bit-wise operations. Here's some sample code, taken from Kinect-scratch:Code:
unsigned char sizeBytes[4]; int len = strlen(sendBuff); sizeBytes[0] = (unsigned char)(len >> 24); sizeBytes[1] = (unsigned char)((len << 8) >> 24); sizeBytes[2] = (unsigned char)((len << 16) >> 24); sizeBytes[3] = (unsigned char)((len << 24) >> 24); write(newsockfd, sizeBytes, 4); write(newsockfd, sendBuff, strlen(sendBuff));
Okay. Kinect2Scratch was written in Java, but I think most of the functions are the same.
EDIT: They ARE almost the same! This is the code I used to get the bytes (I hope it is right...)
//get bytes
char[] sizeBytes = new char[4];
int len = commandToSend.Length;
sizeBytes[0] = (char)(len >> 24);
sizeBytes[1] = (char)((len << 8) >> 24);
sizeBytes[2] = (char)((len << 16) >> 24);
sizeBytes[3] = (char)((len << 24) >> 24);Last edited by GP1 (2012-06-28 14:04:01)
Offline
GP1 wrote:
bobbybee wrote:
For the most part. Basically, Scratch uses big-endian for lengths--in other words, biggest byte first. So, if the length of the string was say, 7, the length in bytes would be:
0, 0, 0, 7.
If it is 255, it would be,
0, 0, 0, 255.
Note: if it is 256, then it carries, like binary does.
0, 0, 1, 1
You'll have to use bit-wise operations. Here's some sample code, taken from Kinect-scratch:Code:
unsigned char sizeBytes[4]; int len = strlen(sendBuff); sizeBytes[0] = (unsigned char)(len >> 24); sizeBytes[1] = (unsigned char)((len << 8) >> 24); sizeBytes[2] = (unsigned char)((len << 16) >> 24); sizeBytes[3] = (unsigned char)((len << 24) >> 24); write(newsockfd, sizeBytes, 4); write(newsockfd, sendBuff, strlen(sendBuff));Okay. Kinect2Scratch was written in Java, but I think most of the functions are the same.
EDIT: They ARE almost the same! This is the code I used to get the bytes (I hope it is right...)Code:
//get bytes char[] sizeBytes = new char[4]; int len = commandToSend.Length; sizeBytes[0] = (char)(len >> 24); sizeBytes[1] = (char)((len << 8) >> 24); sizeBytes[2] = (char)((len << 16) >> 24); sizeBytes[3] = (char)((len << 24) >> 24);
hmm, this code looks very familiar
Offline