Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 791561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:51:25+00:00 2026-05-14T21:51:25+00:00

While working on a WebSocket server in Java I came across this strange bug.

  • 0

While working on a WebSocket server in Java I came across this strange bug. I’ve reduced it down to two small java files, one is the server, the other is the client. The client simply sends 0x00, the string Hello and then 0xFF (per the WebSocket specification).

On my windows machine, the server prints the following:

Listening
byte: 0
72 101 108 108 111 recieved: 'Hello'

While on my unix box the same code prints the following:

Listening
byte: 0
72 101 108 108 111 -3

Instead of receiving 0xFF it gets -3, never breaks out of the loop and never prints what it has received.

The important part of the code looks like this:

byte b = (byte)in.read();
System.out.println("byte: "+b);

StringBuilder input = new StringBuilder();
b = (byte)in.read();
while((b & 0xFF) != 0xFF){
 input.append((char)b);
 System.out.print(b+" ");
 b = (byte)in.read();
}
inputLine = input.toString();

System.out.println("recieved: '" + inputLine+"'");
if(inputLine.equals("bye")){
 break;
}

I’ve also uploaded the two files to my server:

  • Server.java
  • Client.java

My Windows machine is running windows 7 and my Linux machine is running Debian

Edit:
When b is an int, it still acts strange. I send 0xFF (255) but receive 65533 (not 65535 or 255).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:51:26+00:00Added an answer on May 14, 2026 at 9:51 pm

    The problem isn’t in the code you’ve shown. It’s here:

    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    

    You’re dealing with binary data so you should be using the raw stream – don’t turn it into a Reader, which is meant for reading characters.

    You’re receiving 65533 because that’s the integer used for the “Unicode replacement character” used when a value can’t be represented as a real Unicode character. The exact behaviour of your current code will depend on the default character encoding on your system – which again isn’t something you should rely on.

    Further, you’re assuming each byte should translate to a single character – essentially you’re assuming ISO-8859-1. I haven’t checked the spec, but I doubt that that’s what you should be using.

    Finally, you’re not checking for b being -1 – which is used to indicate that the client has closed the stream.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 440k
  • Answers 440k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Since you already have an http server implemented you may… May 15, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer The ptr passed to callackwrapper is most likely an invalid… May 15, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer by doing this, you risk calling delete (in your process,… May 15, 2026 at 5:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.