1

Client side:

        out = new DataOutputStream(connection.getOutputStream());
        String process;
        System.out.println("Connecting to server on "+ host + " port " + port +" at " + timestamp);
        process = "Connection: "+host + ","+port+","+timestamp; 
        System.out.println("client len " + process.length());
        out.write(process.length());

Prints: Client len 55

Server side:

       in = new DataInputStream(connection.getInputStream());
       while (true) {
            int len = in.readInt();
            System.out.println("Length of pkt: "+len);

Prints: Length of pkt: 927166318

What's going on here? I tried writing 0 and it printed 3621743 on the server side. I checked some other sites and a few people had problems with other streams. I read about the issues arising with big vs little endianness, but I am not sure what the problem is here since I am using the data*streams that should work fine with each other.

2 Answers 2

3

If you call readInt() on one side, you should call writeInt(int) on the other. Change this

out.write(process.length());

to

out.writeInt(process.length());

From the Javadoc for write(int),

Writes the specified byte (the low eight bits of the argument b) to the underlying output stream.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. I knew it was something basic. Thanks for the help!
1

Use out.writeInt(process.length()); instead of out.write(...); since you read an Integer from the stream afterwards.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.