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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:31:28+00:00 2026-06-03T16:31:28+00:00

I have a class Game that uses a class TCPServer . I would like

  • 0

I have a class Game that uses a class TCPServer. I would like to seperate all the low level sending and receiving in TCPServer. So I give TCPServer a separate thread that constantly reads incoming messages. I would like to return these messages to Game. But I dont know how to do this. Game should also react on these messages bij responding by sending a message to the clients.

I think/know it is possible by moving the thread from TCPServer to Game but is this the best way to solve this? Isn’t it better to keep the 2 classes separate.

I have the following pseudocode.

public class Game{
   TCPServer tcpServer = new TCPServer();

   public void execute(){
       // do something
       Object o = new Object;
       send(o);

       if(receive(o)){  // WRONG
          // do something with the receive object
       }
   }

   private void send(Object o){
      tcpServer.send(o);
   }

   private Object receive(Object o){
      return tcpServer.receive(o);
}

public class TCPServer extends Thread{
   private ServerSocket serverSocket;
   Socket client = new Socket;

   public void run(){
     Object o = receive();
     // How do I get this object o in the Server class in function execute()?
   }

   private Object receive(){
      return serverSocket.listen();

   }

   public void send(Object o){
      client.send(o);
}
  • 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-06-03T16:31:34+00:00Added an answer on June 3, 2026 at 4:31 pm

    I think the right way to do this would be by using BlockingQueues. Instead of sending the messages in the current thread, Game would just add them to a sendQueue. The TCPServer would then have a receiving thread which would add things to the readQueue and another thread which wrote things from the sendQueue to the socket.

    I don’t quite understand your thread model right now. Typically you have a thread calling accept() on a ServerSocket and then forking a client handler thread. If Game is maintaining a number of these client connections then you’d have something like this:

    1. TCPServer has a thread which calls accept().
    2. When it gets a connection is creates a ClientHandler object and adds this to the collection of clients being handled by the game.
    3. Each ClientHandler has a sendQueue and receivedQueue — maybe each are LinkedBlockingQueue
    4. Each ClientHandler forks a reading thread receiving from the client and adding to the receivedQueue and a writing thread which is waiting on the sendQueue and writes to the client socket.

    There’s a lot of work here to get this right. How to properly shutdown both reader and writer threads when the client socket closes is going to be a challenge.

    Hope something here helps.

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

Sidebar

Related Questions

I'm creating a game that uses cards. I have an AppController class with one
In my game I have a concept of resource manager, a class that stores
I have a clojure project that uses the slick 2d game engine that I
i have written a standalone connect 4 game. next i would like to be
I have a class that defines my level. I have an XML file in
I have a Java game that uses networking, and I have a client (using
I have a class called game that extends View that is added to a
I have a class with the following declaration of the fields: public class Game
I am making a card game in ruby. I have the Game class, which
I have a class called Mouse (tracking button states in a game). I want

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.