This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-06-27 23:04:34

GP1
Scratcher
Registered: 2009-07-06
Posts: 1000+

HELP! Scratch giving error when message is sent C#

I was attempting to send data to Scratch with sockets using Visual C#. I send this command:

Code:

sensor-value 'currentPressedKey' 5

Yes,  I have a variable in Scratch names currentPressedKey. Anyway, I keep getting this error from Scratch:

Code:

message too big; bad size field?

My C# code is a method to send data (I picked it up from the internet):

Code:

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:

Code:

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?


I am currently http://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=imagehttp://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=text and I finally got over 1000 posts.

Offline

 

#2 2012-06-27 23:22:27

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: HELP! Scratch giving error when message is sent C#

You need to include the length of the message. http://wiki.scratch.mit.edu/wiki/Remote … s_Protocol

Offline

 

#3 2012-06-28 00:51:58

GP1
Scratcher
Registered: 2009-07-06
Posts: 1000+

Re: HELP! Scratch giving error when message is sent C#

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?


I am currently http://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=imagehttp://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=text and I finally got over 1000 posts.

Offline

 

#4 2012-06-28 06:39:13

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: HELP! Scratch giving error when message is sent C#

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));

I support the Free Software Foundation. Protect our digital rights!

Offline

 

#5 2012-06-28 12:56:30

GP1
Scratcher
Registered: 2009-07-06
Posts: 1000+

Re: HELP! Scratch giving error when message is sent C#

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);

Last edited by GP1 (2012-06-28 14:04:01)


I am currently http://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=imagehttp://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=text and I finally got over 1000 posts.

Offline

 

#6 2012-06-28 14:23:25

GP1
Scratcher
Registered: 2009-07-06
Posts: 1000+

Re: HELP! Scratch giving error when message is sent C#

Yay! IT WORKS!


I am currently http://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=imagehttp://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=text and I finally got over 1000 posts.

Offline

 

#7 2012-06-28 14:24:40

TRocket
Scratcher
Registered: 2009-08-18
Posts: 1000+

Re: HELP! Scratch giving error when message is sent C#

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  tongue


http://i.imgur.com/1QqnHxQ.png

Offline

 

#8 2012-06-28 15:07:31

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: HELP! Scratch giving error when message is sent C#

Closed by request of the topic owner.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

Board footer