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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:54:50+00:00 2026-05-22T11:54:50+00:00

I have a problem with my Java program. It has a socket connection between

  • 0

I have a problem with my Java program. It has a socket connection between a server and many client. Here is the server (the part which concerns the problem):

private static ArrayList<ParallelServer> clientConnected = new ArrayList<ParallelServer>();


public Server(int port) {
    this.port = port;
    if (!startServer())
        JOptionPane.showMessageDialog(new JFrame(""),
                "Error!", "ERROR!",
                JOptionPane.ERROR_MESSAGE);
}

private boolean startServer() {
    try {
        server = new ServerSocket(port);
        loadDatabase();
    } catch (IOException ex) {
        ex.printStackTrace();
        return false;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return true;
}

public void runServer() {
    while (true) {
        try {
            client = server.accept();
            ParallelServer pServer = new ParallelServer(client);
            clientConnected.add(pServer);
            Thread thread = new Thread(pServer);
            thread.start();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}


public static void sendBroadcast(String username) throws IOException {
    for(int i = 0; i < clientConnected.size(); i++)
        clientConnected.get(i).sendAnswer("@change," + username);
}

The parallel server is:

    private Socket client;
    private InputStreamReader inputstreamreader;
    private BufferedReader bufferedreader;
    private PrintWriter printwriter;

    public ParallelServer(Socket client) {
        this.client = client;
    }

    public void run() {
        try {
            inputstreamreader = new InputStreamReader(client.getInputStream());
            bufferedreader = new BufferedReader(inputstreamreader);
            printwriter = new PrintWriter(client.getOutputStream(), true);
            String lineread = "";

            while (client.isConnected()) {
                lineread = bufferedreader.readLine();
                doCommand(lineread);
            }
        } catch (UnknownHostException unhe) {
        } catch (InterruptedIOException intioe) {
        } catch (IOException ioe) {
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void sendAnswer(String answer) throws IOException {
        printwriter = new PrintWriter(client.getOutputStream(), true);
        printwriter.println(answer);
        printwriter.flush();
    }

And here is the client:

private String serverurl = "localhost";
private int serverport = 7777;
private PrintWriter printwriter;
private InputStreamReader inputstreamreader;
private BufferedReader bufferedreader;
private Socket server;

public Client() {
    server = null;
    try {
        server = new Socket(serverurl, serverport);
        server.setSoTimeout(5000);
    } catch (UnknownHostException unhe) {
        System.out.println("UnknownHostException: " + unhe.getMessage());
    } catch (InterruptedIOException intioe) {
        System.out.println("Timeout while attempting to establish socket connection.");
    } catch (IOException ioe) {
        JOptionPane.showMessageDialog(new JFrame(),"Unable to reach the server!","ERROE!",JOptionPane.ERROR_MESSAGE);
    }
}

public String sendCommand(String command) throws IOException {
    if(server == null) {
        try {
            server = new Socket(serverurl, serverport);
            server.setSoTimeout(5000);
        } catch (UnknownHostException unhe) {
            System.out.println("UnknownHostException: " + unhe.getMessage());
        } catch (InterruptedIOException intioe) {
            System.out.println("Timeout while attempting to establish socket connection.");
        } catch (IOException ioe) {
            JOptionPane.showMessageDialog(new JFrame(),"Unable to reach the server!","ERROR!",JOptionPane.ERROR_MESSAGE);
        }
    }
    if(server != null) {
        printwriter = new PrintWriter(server.getOutputStream(), true);
        printwriter.println(command);
        printwriter.flush();
        inputstreamreader = new InputStreamReader(server.getInputStream());
        bufferedreader = new BufferedReader(inputstreamreader);

        return bufferedreader.readLine();
    }
    else
        return "@serverProblem";
}

The program is a simple online game with turns. Players’ turns are created with a queue and when a player passes his turn, the server send a broadcast message which say “Now it is ‘Player 1’ turn.” (for instance). My problem is that when a client receive the message, its like it add the answer “Now it is ‘Player 1’ turn.” to the next message it will receive. In my case: when a player passes his turn, he sends “@passTurn,username”. The ParallelServer class polls it from the queue, puts it at the bottom of the queue, sends the client “@ok” to tell it that the turn has changed successfully and tells the Server class to send the broadcast message. Then, when the same client will try do do a further action, it will consider “Now it is ‘Player 1’ turn.” as the answer the server has given to it. Instead, I would like that the server and the clients work as always and when the broadcast message is cought, the client is notified without any collateral effect.
What can I do?
Thanks.

  • 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-22T11:54:50+00:00Added an answer on May 22, 2026 at 11:54 am

    Your bi-directional message passing mechanism should look something like this:

    Server:

    Wait on any client InputStream
    if (broadcast) 
      broadcast_message()
    else 
      process_message()
    

    Client:

    Receiving Thread:
    Wait on server broadcast
    
    Sending Thread:
    Wait on messages to be sent to server from the User Input
    

    This should do the trick 🙂

    Hope it helps. Cheers!

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

Sidebar

Related Questions

I have a java server that has to communicate over a TCP socket to
I have a dummy Java Program, which I want to write in Clojure. It
I have a Java program which is being started via ProcessBuilder from another Java
I have a problem using the Java search function in Eclipse on a particular
I have a little Java problem I want to translate to Python. Therefor I
I have the following problem in my Data Structures and Problem Solving using Java
I am making an application with Java Swing and i have a problem. I
I'm having a small problem in Java. I have an interface called Modifiable. Objects
We have a java program that requires a large amount of heap space -
I have a Java program that runs on Linux and telnets into a remote

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.