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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:39:03+00:00 2026-06-02T20:39:03+00:00

First of all I am not too sure how to explain that without sounding

  • 0

First of all I am not too sure how to explain that without sounding confusing so I’ll try to make it simple. I have a server app that “pairs” two clients together, as part of a two player game. The way that happens is that when the first client connects to the server, the socket connection is accepted, a new ClientProtocol thread is created with that client but not started. When another client connects to the server, he is added to the previous thread and then the thread is started. The thread knows two inputstream and two outputstream, one for each client. However when I try to read from either client or server side nothing happens. This works fine for a single client in a single thread. Here is some code:

Server side (main thread)

public static Queue<ContestProtocol> contesters;

public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = null;
    contesters = new LinkedList<ContestProtocol>();

    try {
        serverSocket = new ServerSocket(4444);
    } catch (IOException e) {
        System.err.println("Could not listen on port: 4444.");
        System.exit(-1);
    }

    while (true) {
        Socket socket = serverSocket.accept();

        ObjectInputStream ois;
        ObjectOutputStream oos;

        try {
            ois = new ObjectInputStream(socket.getInputStream());
            oos = new ObjectOutputStream(socket.getOutputStream());

            synchronized (contesters) {
                if (contesters.size() == 0) {
                    Contester contester1 = new contester(ois, oos);
                    contesters.add(new ContestProtocol(contester1));
                } else {
                    Contester contester2 = new Contester(ois, oos);
                    contesters.poll().hook(contester2).start();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

server side (ContestProtocol)

private Contester contester1, contester2;

public ContestProtocol(Contester contester1) {
    super("ContestProtocol");
}

public ContestProtocol hook(Contester contester2) {
    this.contester2 = contester2;
    return this;
}

public void run() {
    try {
        contester1.getOOS().writeInt(-1);
        contester2.getOOS().writeInt(-2);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

server side (contester):

public Contester(ObjectInputStream ois, ObjectOutputStream oos) {
    this.ois = ois;
    this.oos = oos;
}

public ObjectInputStream getOIS() {
    return ois;
}

public ObjectOutputStream getOOS() {
    return oos;
}

client side:

try {
            socket = new Socket(serverAddr, 4444);
            oos = new ObjectOutputStream(socket.getOutputStream());
            ois = new ObjectInputStream(socket.getInputStream());
            int selectedChampion = ois.readInt();
} catch (Exception e) {
                e.printStackTrace();
}

The ContestProtocol Thread finishes executing without problem but both clients hang on readInt(). I do not understand why.

  • 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-02T20:39:06+00:00Added an answer on June 2, 2026 at 8:39 pm

    This has to do mutex or semaphore issue in accessing the resources:
    The streams you are using- which in this case are ouput and input streams – are tring to read and write to the same resource.
    Because all these processes are happening on a continues one thread, their threads are interrupting one another, and creating wait processes on one another.
    One solution is that, you register event liseners on the stream objecs, so that whenever one is finished the other one only will start.

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

Sidebar

Related Questions

First of all I'm not sure this is even possible, however I need to
First of all I'm not sure if .exe window is the proper term. It's
I have a question that I think should not be too complex. I have
Not sure if im mis-understanding how EF Code first works or have set it
First of all: I am not able to find out the proper Title of
first of all: this is not the same as this . The ModelBackend has
First of all I am not a designer. so this question is may be
first of all I'm really not really friendly with ruby or with rails, so
First of all: This question is not directly programming related. However, the problem only
First of all, I'm not a javascript expert. I'm going crazy on trying to

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.