Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_receiveMemoryStream lenght error #32

Open
michaelmnich opened this issue May 3, 2018 · 0 comments
Open

_receiveMemoryStream lenght error #32

michaelmnich opened this issue May 3, 2018 · 0 comments

Comments

@michaelmnich
Copy link

michaelmnich commented May 3, 2018

why always is needed to use

        private static void WriteInt32(byte[] buffer, int startIndex, int number)
        {
            buffer[startIndex] = (byte)((number >> 24) & 0xFF);
            buffer[startIndex + 1] = (byte)((number >> 16) & 0xFF);
            buffer[startIndex + 2] = (byte)((number >> 8) & 0xFF);
            buffer[startIndex + 3] = (byte)((number) & 0xFF);
        }

because of that offset you did soft hard to work wit any other languages.
If you made serwer side with your scs lib and client side with java. And you are using simple txt message you must remember to transform every command by that function because simple you will get Exception because:

  var messageLength = ReadInt32(_receiveMemoryStream);
            if (messageLength > MaxMessageLength) //That will be true

I'v made some solution for that soft can be compatible with java.

 private static void WriteInt32(byte[] buffer, int startIndex, int number)
    {
        buffer[startIndex] = (byte)((number >> 24) & 0xFF);
        buffer[startIndex + 1] = (byte)((number >> 16) & 0xFF);
        buffer[startIndex + 2] = (byte)((number >> 8) & 0xFF);
        buffer[startIndex + 3] = (byte)((number) & 0xFF);
    }


    public static byte[] GetBytes(String message) throws UnsupportedEncodingException {

        byte[] serializedMessage = message.getBytes("UTF-8");


       int messageLength = serializedMessage.length;


        //Create a byte array including the length of the message (4 bytes) and serialized message content
        byte[] bytes = new byte[messageLength + 4];
        WriteInt32(bytes, 0, messageLength);
        System.arraycopy(serializedMessage, 0, bytes, 4, messageLength);


        return bytes;
    }

and use it like that:


                String str = "asdsa\n";
                byte[] b = GetBytes(str);
                DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
                outToServer.write(b, 0, b.length);
michaelmnich added a commit to michaelmnich/scs that referenced this issue May 29, 2018
hi it will be fine to add part about integration with other languages. Iv made issue abut that. hikalkan#32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant