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 6983861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:28:29+00:00 2026-05-27T18:28:29+00:00

My ServerSocket writes out the following lines: OutputStreamWriter outstream = new OutputStreamWriter(clientSocket.getOutputStream()); BufferedWriter out

  • 0

My ServerSocket writes out the following lines:

OutputStreamWriter outstream = new OutputStreamWriter(clientSocket.getOutputStream());

BufferedWriter out = new BufferedWriter(outstream);

out.write("Hello");
out.newLine();
out.write("People");

out.flush();

And my client reads it like so:

in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while(true){
    line = in.readLine();

    if(line == null){
        ClientDialog.gui.log.append("NULL LINE\r\n");
    } else{
        ClientDialog.gui.log.append(line+"\r\n");
    }

    if(in.readLine() == "SHUTDOWN"){
        break;
    }
}

As you can see I write “Hello”, a new line, and then “People” on the socket, but when I run my client, it only prints “Hello” and null repeatedly. I don’t see what is wrong?


PROBLEM SOLVED:

I had to add an out.newLine() after I wrote “People” to the socket, and I had to do line == "SHUTDOWN" not in.readLine() == "SHUTDOWN" as the in.readLine() was consuming “People”.

It was also recommended to use the equals() method in the String class, instead of ==.

Thanks!

This is for future viewers.

  • 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-27T18:28:30+00:00Added an answer on May 27, 2026 at 6:28 pm

    You call readLine twice per loop.

    if(in.readLine() == "SHUTDOWN"){
            break;
    }
    

    The code above consumes your “People” line

    to fix change to

    if("SHUTDOWN".equals(line){
                break;
    }
    

    Also you should send a newline after sending “People”

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

Sidebar

Related Questions

I have the following 3 lines of the code: ServerSocket listeningSocket = new ServerSocket(earPort);
To my understanding by serverSocket = new ServerSocket(portNumber) we create an object which potentially
I have this code: ServerSocket serverSideSocket = new ServerSocket(1234); serverSideSocket.accept(); BufferedReader in = new
I am trying to use ServerSocket with port 2649, and other people cannot connect.
I'm trying to build a simple HTTP Server using Java, using java.net.ServerSocket = new
I am new to Python and I am trying to write a Server program.
I have the following web server class found here . I need to write
I am new to socket programming and trying to write a simple cmd line
I'm writing a simple web server,code snippet: ServerSocket server = new ServerSocket(80); Socket client=server.accept();
I have the following situation: using a classical Java server (using ServerSocket) I would

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.