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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:01:50+00:00 2026-06-18T08:01:50+00:00

I am trying to make a simple server/client application to get my head around

  • 0

I am trying to make a simple server/client application to get my head around using sockets. I can successfully get single line communication between server and client, for example client sends message to server, server acknowledges and sends single message back.

The problem is when I try to read the incoming replies from the server back to the client when there are multiple lines in the BufferedReader.

Examples…

Server and client giving one line to each other.

import java.io.*;
import java.net.*;
import java.awt.*;

class Server {
public static void main(String args[])throws IOException
{
    Server myServ = new Server();
    myServ.run();
}//end main
public void run() throws IOException
{
ServerSocket serSKT = new ServerSocket(729);
Socket socket = serSKT.accept();

BufferedReader BR = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter PW = new PrintWriter(socket.getOutputStream());

String fromClient = BR.readLine();
System.out.println(fromClient);

if(fromClient !=null)
{PW.println("Server giving response to client");PW.flush();}
}}//end class server

Client:

import java.io.*;
import java.net.*;

class Client
{
public static void main(String args[])throws IOException
{
Client myCli = new Client();
myCli.run();
}
public void run() throws IOException
{
int port = 729;

Socket mySkt = new Socket("localhost",port);

PrintWriter myPW = new PrintWriter(mySkt.getOutputStream(),true);

BufferedReader myBR = new BufferedReader(new InputStreamReader(mySkt.getInputStream()));

myPW.println("Message from client to server");

String temp=myBR.readLine();
System.out.println(temp);
}}

So the above works fine..now when I try to use for loops to print 10 lines of text to the server and then 10 lines back from the server to the client, I run in to problems. For example..Here is the server and client..

import java.io.*;
import java.net.*;
import java.awt.*;

class Server {
public static void main(String args[])throws IOException
{
    Server myServ = new Server();
    myServ.run();
}//end main
public void run() throws IOException
{
ServerSocket serSKT = new ServerSocket(729);
Socket socket = serSKT.accept();

BufferedReader BR = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter PW = new PrintWriter(socket.getOutputStream());
String fromClient = null;
while((fromClient = BR.readLine())!=null)
{
System.out.println(fromClient);//this is being printed successfully
}

if(fromClient !=null)
{
for(int i=0;i<10;i++){
PW.println("Server giving response to client"+i);}
PW.flush();}
}}//end class server

Client:

import java.io.*;
import java.net.*;

class Client
{
public static void main(String args[])throws IOException
{
Client myCli = new Client();
myCli.run();
}
public void run() throws IOException
{
int port = 729;

Socket mySkt = new Socket("localhost",port);

PrintWriter myPW = new PrintWriter(mySkt.getOutputStream(),true);

BufferedReader myBR = new BufferedReader(new InputStreamReader(mySkt.getInputStream()));

for(int i =0;i<10;i++)
myPW.println("Message from client to server"+i);

String temp=null;
while((temp=myBR.readLine())!=null){
System.out.println(temp);}
}}

The above will print the message 10 times to the server and the server will display what the client has sent it successfully:

while((fromClient = BR.readLine())!=null)
{
 System.out.println(fromClient);//this is being printed successfully
}

What I do not understand is why the client will not get the message “Server giving response to client” and print it to the console:

String temp=null;
while((temp=myBR.readLine())!=null){
 System.out.println(temp);} //should be printing "Server giving repsonse to client"

Sorry it is long, thanks but I am trying with no success!

****EDIT***

Incase anyone else comes across this problem, This was solved by printing a command to the printwriter in the client class to allow the server to know it has finished. e.g:

for(int i =0;i<10;i++){
myPW.println("Message from client to server"+i);}
myPW.println("Finished");

Now in the server class the text being sent to it should be checked for the “Finished” command, if so, break out of the while loop and start sending back to the client, I believe that the only way BufferedReader will be null is if it is closed, so it is infinitely looping. Thanks for the help everyone.

while((fromClient = BR.readLine())!=null)
{
if(fromClient.equals("Finished"))
{
break;
}
System.out.println(fromClient);
}
  • 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-06-18T08:01:51+00:00Added an answer on June 18, 2026 at 8:01 am

    It looks like you never exit the while() loop in the server, after the 10th line, the server waits for another line (and another, and another, etc.).
    The client has to notify the server that he has finished sending, e.g. by sending a special line.
    Then you should exit the while loop on the server and start sending.

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

Sidebar

Related Questions

I am trying to make a simple Client-Server application but when I execute the
I'm trying to make a simple http client server using java. It will show
I am trying to create a simple client/server application using Thrift which is going
I'm trying to make a basic client-server application for windows phone 7 (using Mango
I am trying to make a simple client-server chat program. On the client side
I'm trying to make simple IMAP client using winsock/OpenSSL, is this possible without additional
I'm trying to learn how to do some very simple client-server application programming. Basically,
im currently trying to make a simple IRC Gui Client. Im using the SmartIrc4net
I am trying to make a simple web application using ASP.NET and Interop COM.
I'm using wxPython, and I'm trying to make a single instance application. As far

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.