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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:23:29+00:00 2026-05-22T17:23:29+00:00

I got simple socket based client – server application, each connection gets own thread.

  • 0

I got simple socket based client – server application, each connection gets own thread.
Currently scheme is something like this:

  • Board – is the object to share between the clients, serialized, Plain Old Java object
  • ActiveSessions – all the connections are added into list
  • BroadCaster – when the board has changed, send the board to other clients

Problem is that each thread connects and recevies the board object but when it’s sent again it sends again the same object, but on the serverside the object behaves correctly.

public static void main(String[] args) {
    Board gameBoard = new Board();
    ActiveSessions sessions = new ActiveSessions();
    Broadcaster broadcaster = new Broadcaster(sessions, gameBoard);

    try {
        ServerSocket socket = new ServerSocket(1234);
        // Timeout after what no more new connections are not accepted.
        socket.setSoTimeout(30 * 1000); 
        logger.info("Server started on port " + socket.getLocalPort());

        while (true) {
            SessionHandler session = new SessionHandler(socket.accept(), gameBoard, broadcaster);
            sessions.addSession(session);
            session.start();
        }

    } catch (SocketTimeoutException e1) {
        logger.info("No more new connecions are accpeted, start game or end");
        gameBoard.setGameState(GameState.PLAYING);
        logger.info("Set the gamestate to " + gameBoard.getGameState());
    } catch (IOException e) {
        logger.info("I/O error " + e.getMessage());
    }

SessionHandler, each connection has own thread
package server;

    import game.Board;
    import game.Turn;

    import java.io.EOFException;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.Socket;
    import java.net.SocketException;
    import java.util.logging.Logger;

    public class SessionHandler extends Thread {

        private Board gameBoard;
        private Socket socket;
        private Broadcaster broadcaster;
        private boolean firstConnect = true;

        private ObjectOutputStream out;
        private ObjectInputStream in;
        private static final Logger logger = Logger.getLogger(SocketServer.class.getName());

        public SessionHandler(Socket socket) {
            this.socket = socket; 
        }

        public SessionHandler(Socket accept, Board gameBoard, Broadcaster broadcaster) {
            this(accept);
            this.gameBoard = gameBoard;
            this.broadcaster = broadcaster;
        }

        @Override
        public void run() {
            try {
                out = new ObjectOutputStream(socket.getOutputStream());
                in = new ObjectInputStream(socket.getInputStream());

                while (true) {
                    Turn turn = (Turn) in.readObject();
                    if (turn != null) {
                        if (firstConnect) {
                            gameBoard.addPlayer(turn.getWhoseTurn());
                            firstConnect = false;
                        }

                        // Add the turn to game board and make validation
                        gameBoard.increaseTurns();
                        broadcaster.send();
                    }
                    System.out.println("Turns made " + gameBoard.getTurns() + " players " + gameBoard.getPlayers() + " dice score " + turn.getDiceScore());
                }
            } catch (EOFException e1) {
                logger.warning("Problem reading the object output");
            } catch (SocketException e) {
                if ("Connection reset".equals(e.getMessage())) {
                    System.out.println("Client disconnected, performing cleanup");
                } else {
                    logger.warning("Connection between client lost " + Thread.currentThread());
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(-1);
            }
        }

        public void sendTheGameBoard() {
            try {
                out.writeObject(this.gameBoard);
                out.flush();
            } catch (IOException e) {
                logger.warning("Problem with sending game board object " + e.getMessage());
            }
        }
    }
    class Broadcaster {

    private ActiveSessions activeSessions;

    public Broadcaster(ActiveSessions aa, Board board) {
        this.activeSessions = aa;
    }

    public void send() {
        // Broadcast board forever
            synchronized (activeSessions) {
                Iterator<SessionHandler> active = activeSessions.iterator();

                while (active.hasNext()) {
                    SessionHandler session = active.next();

                    if (!session.isAlive()) {
                        active.remove();
                        session.interrupt();
                    } else { 
                        session.sendTheGameBoard();
                    }
                }
            }

    }
}
  • 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-22T17:23:30+00:00Added an answer on May 22, 2026 at 5:23 pm

    Read http://java.sun.com/javase/technologies/core/basic/serializationFAQ.jsp#handle. The ObjectOutputStream has a cache in order to avoid sending the same object multiple times. You must reset the stream to send a copy again.

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

Sidebar

Related Questions

My simple communication between C++ client and C# server got stuck after a message
I've got a simple application that shows pictures dragged onto it. I'd like the
I've written a simple server application which will run distributed on several machines. My
I'm programming a very simple socket server following this sample code . The server
I'm writing a client-server application to be used in a computer lab and act
I have implement the simple TCP server and TCP client classes which can send
Got some simple code Int32[] tmpInt = new Int32[32]; long lStart = DateTime.Now.Ticks; Thread
I've got a simple java program, socket, AWT and jUnit dependencies. I've built it
I've set up a simple client/server system, but for some reason the client won't
I've got a C++ server that acts as a mirror. What gets in gets

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.