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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:10:28+00:00 2026-05-31T17:10:28+00:00

I am very sorry to ask such a basic question but I am new

  • 0

I am very sorry to ask such a basic question but I am new to threads and socket programming. I am trying to write a multithreaded socket program but it always gives erros. When I debugged it, I could not find why it gives that stupid error . Can you please help me where I am wrong ?

MY CLIENT CLASS :

public class Client {

    public void run() throws IOException{
        Socket s = new Socket("localhost",9999);
        PrintStream ps = new PrintStream(s.getOutputStream());
        ps.println(readFromConsole());
    }

    public String readFromConsole(){
        String result ;
        Scanner in = new Scanner(System.in);
        result = in.nextLine();
        return result;
    }
    public static void main(String[] args) throws Exception {
        Client c = new Client();
        c.run();
    }
}

MY SERVER CLASS :

public class Server {
    ServerSocket ss;
    public void run() throws IOException{
        ss = new ServerSocket(9999);
        while(true){
            TestForThread t;
            try {
                t = new TestForThread(ss.accept());
                Thread testThread = new Thread(t);
                testThread.start();
            } catch(IOException e){
                e.printStackTrace();
            }
        }
        //BufferedReader br =
        //new BufferedReader(new InputStreamReader(sForAccept.getInputStream()));
        //String temp = br.readLine();
        //System.out.println(runCommand(temp));
    }
    protected void finalize() {
        // Objects created in run method are finalized when
        // program terminates and thread exits
        try {
            ss.close();
        } catch (IOException e) {
            System.out.println("Could not close socket");
            System.exit(-1);
        }
    }

    public static void main(String[] args) throws Exception{
        Server s = new Server();
        s.run();
    }
}

MY THREAD CLASS::

public class TestForThread implements Runnable {

    private Socket client;
    public TestForThread(Socket c){
        client = c;
    }

    public void run() {
        String line;
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            in = new BufferedReader(new InputStreamReader(client
                .getInputStream()));
            out = new PrintWriter(client.getOutputStream(), true);
        } catch (IOException e) {
            System.out.println("in or out failed");
            System.exit(-1);
        }

        while (true) {
            try {
                line = in.readLine();
                String commandResult = runCommand(line);
                System.out.println(commandResult);
            } catch (IOException e) {
                System.out.println("Read failed");
                System.exit(-1);
            }
        }
    }

    public String runCommand(String command) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(command); // you might need the full path
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        String answer = "";
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            answer = answer + line + "\n";
        }
        return answer;
    }
}

Thank you very much.

  • 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-31T17:10:29+00:00Added an answer on May 31, 2026 at 5:10 pm

    In your code, you use BufferedReader to wrap the InputStream from Socket, and call readLine() to get string sent by the client. According to http://docs.oracle.com/javase/1.5.0/docs/api/java/io/BufferedReader.html#readLine(),
    readLine() will wait for ‘\n’ or ‘\r’. The problem lies in Client.readFromConsole(), because it doesn’t return a string that contains a line-termination character.

    By adding ‘\n’ or ‘\r’ in the string returned by Client.readFromConsole() method, something like:

    public String readFromConsole() {
        String result ;
        Scanner in = new Scanner(System.in);
        result = in.nextLine();
        return result+System.getProperty("line.separator");
    }
    

    the Server won’t go down after the first request anymore.

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

Sidebar

Related Questions

I'm very sorry to ask such an easy question for you. I can use
I am very new to this and i am sorry to ask this question.
I´m sorry but I have to ask a very simple question. My problem is
I am very sorry if I ask a wrong question but I really need
Sorry, I know this is a very lame question to ask and not of
Sorry for a very generic sounding question. let's say #include <sys/socket.h> #include <sys/types.h> #include
Sorry if this question is very easy, but I can't find the answer anywhere.
Sorry for a long question and not a very descriptive title, but my problem
This is a very basic question, but I'd quite like an explanation of why
I am new to IOS, sorry in advance if I ask a stupid question.

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.