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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:16:55+00:00 2026-05-26T05:16:55+00:00

I want to create a simple server client application, but i think there is

  • 0

I want to create a simple server client application, but i think there is something wrong with the IO Streams. There is no GUI so the chat shall happen through the console( you can open 2 eclipse to test it or whatever IDE you are using).

here is my server code:

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

public class Server {
    ServerSocket serverSocket;
    Socket connection; // connection-to-client
    ObjectOutputStream output;
    ObjectInputStream input;

    public void run() {
        try {
            serverSocket = new ServerSocket(6000, 100);
        } catch (IOException e) {
            System.err.println("Invalid port number");
        }
        while (true) {
            try {
                waitForConnection();
                getIOStreams();
                processConnection();
            } finally {
                closeConnection();
            }
        }
    }

    public void closeConnection() {
        try {
            input.close();
            output.close();
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void waitForConnection() {
        System.out.println("Server is ready to accept conenctions");
        try {
            connection = serverSocket.accept(); // code will stop here until a
                                                // connection occurs
            System.out.println("Conenction established with the client");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void getIOStreams() {
        try {
            output = new ObjectOutputStream(connection.getOutputStream());
            output.flush(); // send header information to the client, which
                            // contains info required to create the input stream
                            // object
            input = new ObjectInputStream(connection.getInputStream());
            System.out.println("Server established I/O streams");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void processConnection() {
        sendData("Connection established with the server");
        String inputMessage = "";
        new Runnable() {
            Scanner sc = new Scanner(System.in);

            public void run() {
                while (true) {
                    sendData(sc.nextLine());
                }
            }
        };
        do {

            try {
                inputMessage = (String) input.readObject();
                System.out.println(inputMessage);
            } catch (ClassNotFoundException e) {
                System.err.println("Object of an unknown type was recieved");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } while (inputMessage.equals("QUIT"));
    }

    public void sendData(String s) {
        try {
            output.writeObject(s);
            output.flush();
            System.out.println("Server: " + s);
        } catch (IOException e) {
            System.err.println("Error writting the message");
        }
    }
}

and this is my client code

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {
    ServerSocket serverSocket;
    Socket clientSocket;
    ObjectOutputStream output;
    ObjectInputStream input;
    String serverAddress = "127.0.0.1";

    public void run() {
        connect2Server();
        getIOStreams();
        processConnection();
        closeConnection();
    }

    public void connect2Server() {
        System.out.println("Trying to connect to the server");
        try {
            clientSocket = new Socket(serverAddress, 6000);
        } catch (UnknownHostException e) {
            System.err.println("Server unavailable");
            System.exit(1);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void getIOStreams() {
        try {
            output = new ObjectOutputStream(clientSocket.getOutputStream());
            output.flush();
            input = new ObjectInputStream(clientSocket.getInputStream());
            System.out.println("Client established I/O Stream");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void processConnection() {
        sendData("Connection established with the client");
        String inputMessage = "";
        new Runnable() {
            Scanner sc = new Scanner(System.in);

            public void run() {
                String outputMessage = "";
                do {
                    outputMessage = sc.nextLine();
                    sendData("Client: " + outputMessage);
                } while (outputMessage.equals("QUIT"));
            }
        };
        while (true) {

            try {
                inputMessage = (String) input.readObject();
                System.out.println(inputMessage);
            } catch (ClassNotFoundException e) {
                System.err.println("Object of an unknown type was recieved");
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    }

    public void sendData(String s) {
        try {
            output.writeObject(s);
            output.flush();
            System.out.println("Client: " + s);
        } catch (IOException e) {
            System.err.println("Error writting the message");
        }
    }

    public void closeConnection() {
        try {
            output.close();
            input.close();
            clientSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

to run server or client just write in main client/server.run();
please tell me what is my error and how to fix it 🙂

  • 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-26T05:16:55+00:00Added an answer on May 26, 2026 at 5:16 am

    Several points:

    1) incorrect conditions for end of input loop:

    while (inputMessage.equals("QUIT")); // Server#processConnection 
    
    while (outputMessage.equals("QUIT")) // Client#processConnection 
    

    these should be negated (“!”).

    2) you should start you System.in reading threads:

    new Thread() { // instead of `Runnable`
       ...
    }.start();
    

    3) you should break listening loop on server for some exceptions, like EOFException which means that client was disconnected.

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

Sidebar

Related Questions

I want to to create a simple client/server chat application. The idea is that
I want to create one simple application for sending request from iphone(client) by using
I want to create a simple http proxy server that does some very basic
I want to create an extremely simple web server for development purposes in Ruby
I am Java and Maven newbie and I want to create a simple client
I want to create light object data-package to pass between client and server applications.
I want to create a simple long polling server in node.js where communication will
I'm trying to create a simple chat application for iOS. It works for receiving
I want to create a simple box with a header bar containing a title
I want to create a simple bit of JS code that creates an image

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.