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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:55:11+00:00 2026-05-28T01:55:11+00:00

I wrote a simple program where a server should print data sent by multiple

  • 0

I wrote a simple program where a server should print data sent by multiple clients. But the server receives only partial data. Following are the relevant pieces of the code.

Server:

try {
        serverSocket = new ServerSocket(8888);
    } catch (IOException e) {
        System.err.println("Could not listen on port: 8888");
        System.exit(-1);
    }
 while (listening) {
    Socket clientSocket = serverSocket.accept();
    BufferedReader reader = new BufferedReader(new InputStreamReader(
                            clientSocket.getInputStream()));
        System.out.println(reader.readLine());

        reader.close();
        clientSocket.close();
  }
  serverSocket.close();

Client:

  try {
        socket = new Socket("nimbus", 8888);
        writer = new PrintWriter(socket.getOutputStream(), true);
        localHost = InetAddress.getLocalHost();
    } 
    catch (UnknownHostException e) {} 
    catch (IOException e) {}

    StringBuilder msg1 = new StringBuilder("A: ");
    for(int i=1; i<=3; i++)
        msg1.append(i).append(' ');
    writer.println(localHost.getHostName() + " - " + msg1);

    StringBuilder msg2 = new StringBuilder("B: ");
    for(int i=4; i<=6; i++)
        msg2.append(i).append(' ');
    writer.println(localHost.getHostName() + " - " + msg2);

    StringBuilder msg3 = new StringBuilder("C: ");
    for(int i=7; i<=9; i++)
        msg3.append(i).append(' ');
    writer.println(localHost.getHostName() + " - " + msg3);

    writer.close();
    socket.close();

I get the following output (when run on 3 clients)

nimbus2 - A: 1 2 3 
nimbus3 - A: 1 2 3 
nimbus4 - A: 1 2 3

I don’t get the second and third messages. Server keeps waiting. Where am I going wrong?

Edit: In the server code, I tried removing reader.close() and clientSocket.close(). That didn’t work either. Another question — if 3 clients send 3 messages, does it require 9 connections? (this is the reason, I closed the connection in the server code)

  • 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-28T01:55:11+00:00Added an answer on May 28, 2026 at 1:55 am

    You probably want to be delegating the handing of the socket to another thread. I’ve written up an example that works by passing each incoming socket to an Executor so it can read all the inputs. I use a Executors.newCachedThreadPool() which should grow to be as big as needed. You could also use Executors.newFixedThreadPool(1) if you want it to only be able to handle 1 client at a time.

    The only other change I made was I removed the BufferedReader and replaced it with a Scanner. I was having issues with the BufferedReader not returning data. I’m not sure why.

    Executor exe = Executors.newCachedThreadPool();
    ServerSocket serverSocket = null;
    try {
        serverSocket = new ServerSocket(8888);
    } catch (IOException e) {
        System.err.println("Could not listen on port: 8888");
        System.exit(-1);
    }
    while (listening) {
        final Socket clientSocket = serverSocket.accept();
    
        exe.execute(new Runnable() {
    
            @Override
            public void run() {
                try {
                    Scanner reader = new Scanner(clientSocket.getInputStream());
                    while(reader.hasNextLine()){
                        String line = reader.nextLine();
                        System.out.println(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    clientSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    
    }
    serverSocket.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a simple program in java web forms but i am receiving the
I wrote this simple Java program which connects to internic server and returns the
I wrote a simple program to solve the math problem: A^2+B^2 = 12 A*B
I wrote a simple program to test a theory that finally block will always
A while back I wrote a simple python program to brute-force the single solution
I recently wrote a program that used a simple producer/consumer pattern. It initially had
I need to write a simple program for work that does the following: read
I am trying to implement a bidirectional client-server program, where clients and servers can
I'm exploring MSMQ services, and I wrote a simple console client-server application that sends
Thanks for reading and answering in advance! I wrote a simple C# program that

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.