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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:50:02+00:00 2026-06-01T14:50:02+00:00

I’m working on a game with a event based structure with the main game

  • 0

I’m working on a game with a event based structure with the main game logic hosted on a server; currently it’s a very small featureset that only allows to host one game between exactly two participants. I’ve read on various questions about ServerSocket and none of them answers my question. I already took a look at

  • ServerSocket accept continues to block
  • ServerSocket.accept()
  • Java ServerSocket won't accept new connections until close() is called on the accepted socket
  • ServerSocket accept() method

In my project I utilize ObjectInputStream and ObjectOutputStream. Everything works as expected (receiving / sending both on server and client side), but after both sockets are registered, the accept method of the ServerSocket instance continues to block forever, even if the same code is invoked before. Perhaps it’s an issue that appears after communicating over a socket once?

My server log shows the following:

waiting for accept
accepting first socket
sending an event to socket1 for informing about waiting for the opponent
waiting for accept
accept second socket
sending responses to both sockets
waiting for accept (and blocking forever)

When the log says response events where sent, they were properly received and processed at the client side. The client side debug outputs show that the next event is definitely sent. Maybe it’s about not closing the client sockets (mentioned in the third linked question)? Anyway I can’t close the client sockets because further communication would be impossible.

Client side code

public void send(Event e) {
    try {
        ObjectOutputStream out = new ObjectOutputStream(
                socket.getOutputStream());
        out.writeObject(e);
        out.flush();
        log.debug("sending event... "+e);
    }
    catch(IOException ioe) {
        log.fatal("constructing oos failed", ioe);
    }
}

Server side code

@Override
public void run() {
    running = true;
    while(running) {
        try {
            Socket s = socket.accept();
            ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
            Event event = (Event) ois.readObject();

            try {
                Event[] response = controller.consume(event);
                ObjectOutputStream oos = new ObjectOutputStream(sockets[0].getOutputStream());
                oos.writeObject(response[0]);
                oos.flush();
                ObjectOutputStream oos2 = new ObjectOutputStream(sockets[1].getOutputStream());
                oos2.writeObject(response[1]);
                oos2.flush();
            }
            catch(...) {
                // multiple catch clauses for different exceptions
                // all just logging (nothing passes silently!)
            }
    }
}

For shortening, the method for assigning the two sockets to the Socket[] array was left out, but since there are no exceptions, keeping the socket works. Do you have any idea what could cause the described behavior? Thank you in advance.

  • 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-01T14:50:03+00:00Added an answer on June 1, 2026 at 2:50 pm

    The accept method only accepts new connections. Since you only have two clients attempting to connect to your server, it will hang indefinitely on your third invocation of accept.

    Side note: You don’t need to continuously create new ObjectInputStreams and ObjectOutputStreams. You can just create one of each for each Socket and keep references to them for reuse.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

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.