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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:16:33+00:00 2026-05-15T15:16:33+00:00

I am trying to make a simple Client-Server application but when I execute the

  • 0

I am trying to make a simple Client-Server application but when I execute the code I get an exception that the client cannot get the socket’s input stream.Please, take a look to my code and try to help.Thanks:)

P.S:Sorry for the messy code.Execute it as two different applications.

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;


public class SimpleClient //implements Runnable 
{
   private ObjectOutputStream output; // output stream to server
   private ObjectInputStream input; // input stream from server
   private String message = ""; // message from server
   private String chatServer="localhost"; // host server for this application
   private Socket client; // socket to communicate with server

   public void runClient() 
   {
      try // connect to server, get streams, process connection
      {
         connectToServer(); // create a Socket to make connection
         getStreams(); // get the input and output streams
         processConnection(); // process connection
      } // end try
      catch(IOException ioe)
      {
          ioe.printStackTrace();
      }
      finally 
      {
        // closeConnection(); // close connection
      } // end finally
   }

    private void closeConnection()
    {
        try 
      {
         output.close(); // close output stream
         input.close(); // close input stream
         client.close(); // close socket
      } // end try
      catch ( IOException ioException ) 
      {
         ioException.printStackTrace();
      } // end catch
   } // end method closeConnection


   private void connectToServer() throws IOException
   {      
      System.out.println( "Attempting connection\n" );

      // create Socket to make connection to server
      client = new Socket( InetAddress.getByName( chatServer ), 12345 );

      // display connection information
      System.out.println( "Connected to: " + 
         client.getInetAddress().getHostName() );
   } // end method connectToServer



   private void getStreams() throws IOException
   {
      // set up output stream for objects
      output = new ObjectOutputStream( client.getOutputStream() );         
      output.flush(); // flush output buffer to send header information

      // set up input stream for objects
      input = new ObjectInputStream( client.getInputStream() );     
      System.out.println( "\nGot I/O streams\n" );
   } // end method getStreams

    private void processConnection() throws IOException
    {

     do { 
         try // read message and display it
         {
            message = ( String ) input.readObject(); // read new message
            System.out.println( "\n" + message ); // display message
         } // end try
         catch ( ClassNotFoundException classNotFoundException ) 
         {
            System.out.println( "\nUnknown object type received" );
         } // end catch

      } while ( !message.equals( "SERVER>>> TERMINATE" ) );
   } // end method processConnection

    public String toString()
{
    return "client connected to "+chatServer;
}



public static void main(String []args)
{
  SimpleClient c= new SimpleClient();
  c.runClient();

}



    }

//————-servers starts here

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;


import java.util.ArrayList;

public class SimpleServer //implements Runnable
{
   private ObjectOutputStream output; // output stream to client
   private ObjectInputStream input; // input stream from client
   private ServerSocket server; // server socket
   private Socket connection; // connection to client  
   private int counter=1; //counts connections
   ArrayList<SimpleClient> list;

 /***
  * Runs forever and takes care of alla
  */
   public void runServer()
   {
       try // set up server to receive connections; process connections
      {
         server = new ServerSocket( 12345, 100 ); // create ServerSocket

         while ( true ) 
         {
            try 
            {
               waitForConnection(); // wait for a connection
               getStreams(); // get input & output streams
               processConnection(); // process connection
            } // end try
            catch ( Exception Exception ) 
            {
               System.out.println( "\nSomething bad happened" );
            } // end catch
            finally 
            {
               closeConnection();
               counter++; //  close connection

            } // end finally
   }
       }

      catch ( IOException ioException ) 
      {
         System.out.println("An io error occured while creating a server-socket");
         ioException.printStackTrace();
      } // end catch
   } // end method runServer

   /***
    * Waits for a client's request for connection
    */
   public void waitForConnection()
   {
        try
        {
            connection = server.accept();
            System.out.println("Client with IP and hostname "+connection.getInetAddress().getHostName());
        } catch (IOException ex)
        {
            System.out.println("An error makes connection impossible");
            ex.printStackTrace();
        }

   }


   /***
    * Makes the interconnection of client's and server's stream's
    */
   public void getStreams()
   {
       try
       {

       output=(ObjectOutputStream) connection.getOutputStream();
       input=(ObjectInputStream) connection.getInputStream();
       output.flush();
       System.out.println("Got IO Streams, hell yeah ");

       }

       catch(IOException ioe)
       {
           System.out.println("Cannot get streams");
           ioe.printStackTrace();
       }

   }

   /***
    * Terminates connection
    */
   public void closeConnection()
   {
        try
        {
            output.flush();
            output.close();
            input.close();
        } catch (IOException ex)
        {
            ex.printStackTrace();
        }
   }

   /***
    * Receives messages from client
    */
    public void processConnection()
    {
        String message="Connection successful";

        do // process messages sent from client
      { 
         try // read message and display it
         {
            message = ( String ) input.readObject(); // read new message
            System.out.println("CLIENT>>> "+message);

         } // end try
         catch ( ClassNotFoundException classNotFoundException ) 
         {
            System.out.println( "\nUnknown object type received" );
         } 

         catch(IOException ioe)
         {
             System.out.println("Cannot receive message from clint");
             ioe.printStackTrace();
         }

      } while ( !message.equals( "CLIENT>>> TERMINATE" ) );


    }

    /***
     * Sends data to client
     * @param message
     */
    public void sendData(String message)
    {

        try // send object to client
      {

         output.writeObject( "SERVER>>> " + message );
         output.flush(); // flush output to client
         System.out.println("SERVER>>> " + message);
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }
    }


    public String toString()
    {
        return "this is a simple server with a total of "+counter+" connections";
    }



   public static void main(String []args)
{
  SimpleServer c= new SimpleServer();
  c.runServer();

}


}
  • 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-15T15:16:34+00:00Added an answer on May 15, 2026 at 3:16 pm

    Your server is just casting the objects returned by Socket.getOutputStream() and Socket.getInputStream() to object streams. Instead, it must construct the object streams in the same way that your client does by using “new ObjectInputStream()” and “new ObjectOutputStream()”. Remember to flush the server’s ObjectOutputStream.

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

Sidebar

Ask A Question

Stats

  • Questions 443k
  • Answers 443k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer git reset --hard origin/otherbranchname Assumes: You are on the branch… May 15, 2026 at 6:13 pm
  • Editorial Team
    Editorial Team added an answer Use get_bloginfo() not bloginfo() - get_bloginfo() returns the value as… May 15, 2026 at 6:13 pm
  • Editorial Team
    Editorial Team added an answer When two databases have so radically different schemas you should… May 15, 2026 at 6:13 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.