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

  • Home
  • SEARCH
  • 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 6605887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:20:13+00:00 2026-05-25T19:20:13+00:00

So I made a Server and Client with sockets in Java. I’m trying to

  • 0

So I made a Server and Client with sockets in Java. I’m trying to get the server to read/write from the socket, but it only reads once the client disconnects:

        System.out.println("Server initialized");
        clientSocket = server.accept();
        System.out.println("Client connected");

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        for(int loop = 0;loop<5;loop++){
            out.write("Hello");
            System.out.println(in.readLine());
        }   //end for

The problem is that once the client connects, it says “Client connected” but then it doesn’t run through the for loop. Once the client disconnects, however, the server executes the for loop and returns

null
null
null
null
null

What am I missing here? Here is my full server code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public ServerSocket server;

    public Server(int port){
        try {
            server = new ServerSocket(port);
        } catch (IOException e) {
            System.out.println("Cannot bind to port: "+ port);
            System.exit(-1);
        }

    }   //end constructor

    public void WaitForClient() throws IOException{
        Socket clientSocket = null;

        try {
            System.out.println("Server initialized");
            clientSocket = server.accept();
            System.out.println("Client connected");

            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

            for(int loop = 0;loop<5;loop++){
                out.write("Hello");
                System.out.println(in.readLine());
            }   //end for

        }   //end try
        catch (IOException e) {
            System.out.println("Accept failed");
            System.exit(-1);
        }   //end catch

        clientSocket.close();
        server.close();

    }   //end method

}   //end class

(My main method is called from another class.)

  • 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-25T19:20:13+00:00Added an answer on May 25, 2026 at 7:20 pm

    You have to flush the stream on the server if you want to see the results immediately. The stream is auto-flushed on close(), that’s why you seen the output then.

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

Sidebar

Related Questions

I'm trying to get multiple images from a server with a client. My ultimate
Hello i made a php socket server to get data from plc, plc is
i had made the following programming for client server programming but it is not
I've been trying to send a packet from a client to a server via
I am trying to conceptually work through a model for a client-server socket application
I made a networked game in Java using Sockets. It works great, except... only
I'm trying to make a simple HTML server that will read a request from
I made a client & server that establishes a TCP connection through sockets, I'm
I'm trying to get a PC client to connect to an Android server. This
I notice that when http requests are made from clients through a proxy server,

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.