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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:01:49+00:00 2026-05-15T06:01:49+00:00

Guys could anyone tell me what’s wrong am I doing here? Below are four

  • 0

Guys could anyone tell me what’s wrong am I doing here? Below are four files Client, main, Server, main. I’m getting an error on Client side after trying to fromServer.readLine().
Error: Error in Client Software caused connection abort: recv failed

            package client;
            import java.io.*;
            import java.net.*;
            import java.util.Scanner;

            public class Client
            {
            private PrintWriter toServer;
            private BufferedReader fromServer;
            private Socket socket;
            public Client( )throws IOException
                {
                socket = new Socket("127.0.0.1",3000);
            }
            public void openStreams() throws IOException
                {

            //
            //        InputStream is = socket.getInputStream();
            //        OutputStream os = socket.getOutputStream();
            //        fromServer = new BufferedReader(new InputStreamReader(is));
            //        toServer = new PrintWriter(os, true);

                toServer = new PrintWriter(socket.getOutputStream(),true);
                fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            }
            public void closeStreams() throws IOException
                {
                fromServer.close();
                toServer.close();
                socket.close();
            }
            public void run()throws IOException
                {
                openStreams();
                String msg = "";
                Scanner scanner = new Scanner(System.in);
                toServer.println("Hello from Client.");
            //    msg = scanner.nextLine();
                while (msg != "exit")
                {
                    System.out.println(">");
            //        msg = scanner.nextLine();
                    toServer.println("msg");
                    String tmp = fromServer.readLine();
                    System.out.println("Server said: " + tmp);
                }
                closeStreams();
            }
            }



        package server;
        import java.net.*;
        import java.io.*;

        public class Server
        {
        private ServerSocket serverSocket;
        private Socket socket;
        private PrintWriter toClient;
        private BufferedReader fromClient;
        public void run() throws IOException
            {
            System.out.println("Server is waiting for connections...");
            while (true)
            {
                openStreams();
                processClient();
                closeStreams();
            }

        }
        public void openStreams() throws IOException
            {
            serverSocket = new ServerSocket(3000);
            socket = serverSocket.accept();
            toClient = new PrintWriter(socket.getOutputStream(),true);
            fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        }
        public void closeStreams() throws IOException
            {
            fromClient.close();
            toClient.close();
            socket.close();
            serverSocket.close();
        }
        public void processClient()throws IOException
            {
            System.out.println("Connection established.");
            String msg = fromClient.readLine();
            toClient.println("Client said " + msg);
        }
        }


    package client;

    import java.io.IOException;


    public class Main {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here

            try
            {
                 Client client = new Client();
                 client.run();
            }
            catch(IOException e)
            {
                 System.err.println("Error in Client " + e.getMessage());
            }
        }

    }



package server;

import java.io.IOException;


public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Server server = new Server();
        try
        {
        server.run();
        }
        catch(IOException e)
        {
            System.err.println("Error in Server " + e.getMessage());
        }
    }

}
  • 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-15T06:01:50+00:00Added an answer on May 15, 2026 at 6:01 am

    Your server side is all mixed up. You need another class, call it Connection, or ConnectedClient, that implements Runnable; takes a Socket as a constructor parameter; and does all the server-side I/O for a particular client. At present you have this all mixed up with your Server class. All that your Server class should do is create a ServerSocket, and enter a loop accepting Sockets and creating Connection objects and starting threads for them. The Socket object in the Server’s run() method is just a local object, and the associated input/output streams or readers/writers belong in the Connection class, not the Server class.

    So that way you can handle multiple clients simultaneously without them treading on each other’s toes.

    Your next issue is that readLine() returns null when the other end has closed the connection. You must test this first before doing any other processing every time you call readLine().

    Next, any time you get a SocketException or IOException other than SocketTimeoutException when using a Socket it is dead – close it, forget about that client and exit the thread if it happens in the server.

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

Sidebar

Related Questions

Hi guys could anyone tell me where im going wrong? The basic aim of
Could you guys recommend me a good db modeling tool? Mainly for SQL Server...
I could be not more sorry guys - the question was a very wrong
Ok guys, could you tell me is there a certain bug in current code:
Hi guys I was wondering if anyone could help me with the following: I
I was wondering if you guys could help me. Here are my .h: Class
Hey guys, please take a look at my code and see what's wrong here.
Hi Guys could you please help me refactor this so that it is sensibly
Hi guys wonder if you guys could help me I'm trying to compile a
Ok I give up and would really appreciate it if you guys could cast

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.