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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:03:07+00:00 2026-05-26T12:03:07+00:00

I have been banging my head trying to figure out what is going wrong.

  • 0

I have been banging my head trying to figure out what is going wrong.

I am trying a simple server client TCP socket in java. Server can send multiple lines as response.
I am using while((str = in.readLine())!= null) at client side and it hangs after reading the response. Please find the code below. I have searched before posting. I am making sure I am ending the response with new line.

I have tried combinations of \n, \r\n but readLine is not detecting the end of line.
But it works fine at server end.

Please help.

Thanks.

SERVER:

import java.net.*;
import java.io.*;
public class SimpleServer {
public static void main(String args[]) throws IOException {

    ServerSocket s = new ServerSocket(55555);
    Socket socket = s.accept();

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

    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

    String inputLine= null;
    System.out.println("call processInput"); 

       while ((inputLine = in.readLine()) != null) {
           System.out.println("before call processInput");           
           out.print("200 Success \r\n");
           out.flush();
           System.out.println("after call processInput: ");            
    }
    System.out.println("after while");
    out.close();
    in.close();
    socket.close();
    }
}

CLIENT:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.CharBuffer;

public class Test {
     public static void main(String[] args) throws IOException {

          try {

              System.out.println(":Connect");
                  Socket s = new Socket("192.168.1.114",55555);
                  System.out.println("Socket:" + s); 


                  System.out.println("after :Connect");



                  OutputStream s1out = s.getOutputStream();


                  PrintWriter output = new PrintWriter(s1out, true);   
                  output.println("login user root");
                  output.flush();


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

                  System.out.println( in.readLine());



                  output.println("servershare");
                  output.flush();
                  System.out.println( "servershare");
                  String str = null;


             while((str = in.readLine())!= null) // hangs here
             {
                  System.out.println(str);

              }


               System.out.println( "share");
               output.println("share file1.txt file1.html, roopa ramesh");
                   str = null;
                   while((str = in.readLine())!= null && !str.equals(""))
                  System.out.println(str);

                  in.close();
                  output.close();
                  s.close();







          } catch (UnknownHostException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

          } catch (IOException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

          } 
     }
}
  • 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-26T12:03:08+00:00Added an answer on May 26, 2026 at 12:03 pm

    You have a deadlock :

    1. client sends login
    2. server reads login line and sends 200 OK, and waits for the next line
    3. client reads 200 OK
    4. client sends servershare
    5. server reads servershare line, sends 200 OK, and waits for the next line
    6. client reads 200 OK, and waits for the next line

    Both the client and the server are waiting for the next line from the other end. Your protocol isn’t correct.

    Moreover, you’re using the platform default encoding at server-side and client-side to read and write the messages. If the client and server don’t have the same default encoding, you’ll have a problem. You should wrap your streams with Input/Output stream writers and specify a specific encoding when constructing them.

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

Sidebar

Related Questions

I have been banging my head for hours trying to figure out why this
I have been banging my head against the wall trying to figure out this
I've been banging my head against the wall trying to figure out where I'm
I've been banging my head trying to figure some things out. So, I'm looking
Okay, I've been banging my head trying to figure out how to get around
I've been banging my head against the wall for hours trying to figure out
I have been banging my head around trying to come up with a query
I have been banging my head against the wall trying to get Exscript installed.
I've been banging my head for the last two days trying to figure this
Hello everyone, I have been banging my head really hard trying to solve this

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.