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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:59:45+00:00 2026-05-25T23:59:45+00:00

I’m having a problem with a little game I’m designing in my class. The

  • 0

I’m having a problem with a little game I’m designing in my class.
The problem is that I got two clients connected to a server. (client1 and client2) They are each running a game, which in the end, closes the window. As the game window is a JDialog, it will then, when it’s closed, send a message, through a socket, to the server, telling it that it’s done. I want the server to know which of the two clients were completed first. They are reporting through a PrintWriter on the sockets’ OutputStream.
What I did was this:

    in1 = new BufferedReader(new InputStreamReader(client.getInputStream()));
    in2 = new BufferedReader(new InputStreamReader(client2.getInputStream()));
    try {
        in1.readLine();
    } catch (IOException ex) {
        Logger.getLogger(gameServer.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        in2.readLine();
    } catch (IOException ex) {
        Logger.getLogger(gameServer.class.getName()).log(Level.SEVERE, null, ex);
    }

Problem is that it waits for the first input, before it even starts listening on the second. How can I make it listen on both at the same time? Or solve my problem some other way.
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-25T23:59:45+00:00Added an answer on May 25, 2026 at 11:59 pm

    Server connection should work like this:

    Server gameServer = new Server();
    
    ServerSocket server;
    try {
        server = new ServerSocket(10100);
        // .. server setting should be done here
    } catch (IOException e) {
        System.out.println("Could not start server!");
        return ;
    }
    
    while (true) {
        Socket client = null;
        try {
            client = server.accept();
            gameServer.handleConnection(client);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    In hanleConnection() you start a new thread and run the communication for this client in the created thread. Then the server can accept a new connection (in the old thread).

    public class Server {
        private ExecutorService executor = Executors.newCachedThreadPool();
    
        public void handleConnection(Socket client) throws IOException {    
            PlayerConnection newPlayer = new PlayerConnection(this, client);
            this.executor.execute(newPlayer);
        }
    
        // add methods to handle requests from PlayerConnection
    }
    

    The PlayerConnection class:

    public class PlayerConnection implements Runnable {
    
        private Server parent;
    
        private Socket socket;
        private DataOutputStream out;
        private DataInputStream in;
    
        protected PlayerConnection(Server parent, Socket socket) throws IOException {
            try {
                socket.setSoTimeout(0);
                socket.setKeepAlive(true);
            } catch (SocketException e) {}
    
            this.parent = parent;
            this.socket = socket;
    
            this.out    = new DataOutputStream(socket.getOutputStream());;
            this.in     = new DataInputStream(socket.getInputStream());
        }
    
        @Override
        public void run() {                 
            while(!this.socket.isClosed()) {                        
                try {
                    int nextEvent = this.in.readInt();
    
                    switch (nextEvent) {
                        // handle event and inform Server
                    }
                } catch (IOException e) {}
            }
    
            try {
                this.closeConnection();
            } catch (IOException e) {}
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
We're building an app, our first using Rails 3, and we're having to build
i got an object with contents of html markup in it, for example: string

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.