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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:56:29+00:00 2026-06-14T00:56:29+00:00

I am trying to have two people connect to a server and the server

  • 0

I am trying to have two people connect to a server and the server sets up a connection between them both. They are then able to speak to each other.
If you see the comment that says “HEREEEEEEEEEEEEEEEEEE”, I have a thread.sleep, and for some strange reason it works with the thread.sleep(), BUT if I take it out, only one person can send to their partner if there is no thread.sleep();

P.S. it works with a System.out.println too, not just thread.sleep

I have many more files to go with this, but it’s a pain to load them, and was hoping that this was a common problem that people knew about.

Any help is appreciated.

import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;

public class ConnectedClient extends Thread
{

  private Socket cSocket;       //Client's Socket
  private PrintWriter cout;     //write to Client
  private BufferedReader cin;   //Read from Client
  private String cip;           //Clients IP

  private Socket pSocket;       //Partner's Socket
  private PrintWriter pout;     //Write to partner
  private String pip;           //Partner's ip

  private Server srvr;          //Current server

  public ConnectedClient(Socket skt, Server s)
  {
    try
    {
      //Client information
      cSocket = skt;
      cout = new PrintWriter(cSocket.getOutputStream(), true);
      cin = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));
      //Partner's information
      pSocket = null;
      pip = null;
      pout = null;

      //Server and thread
      srvr = s;
      start();
    }
    catch(Exception E)
    {
      System.out.println(E);
    }
  }

  public void run()
  {
    String data;
    System.out.println("Connected to client");
    try
    {
      //Get the ip address and add it to server's table
      while(!cin.ready()){}
      cip = cin.readLine();
      srvr.addToIpTable(cip, cSocket);

      ////Waiting for another client//
      while(pSocket == null) {Thread.sleep(10);} // HEREEEEEEEEEEEEEEEEEEEEE
      System.out.println("Found a partner");
      pout = new PrintWriter(pSocket.getOutputStream(), true);
      System.out.println("I am here");
      cout.println("Done");

      while(true)
      {
        //Waits for a message
        while(!cin.ready()){}
        data = cin.readLine();

        //Outputs message to partner
        pout.println(data);
        pout.flush();
        //End when exit is typed
        if(data.equals("exit"))
          break;
      }

      //Cleanup
      cSocket.close();
      pSocket.close();
      cout.close();
      cin.close();
    }
    catch(Exception E)
    {
      System.out.println(E);
    }
  }


  public String getCip()
  {
    return cip;
  }

  public Socket getCSocket()
  {
    return cSocket;
  }

  public void setPartner(String partnerIp, Socket socket)
  {
    pip = partnerIp;
    pSocket = socket;
  }
}
  • 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-14T00:56:30+00:00Added an answer on June 14, 2026 at 12:56 am

    Try making pSocket volatile – during the busy wait it will be in cache, and never updated when the other thread changes it.

    You probably still do want a small sleep, to avoid 100% utilisation of a core while waiting.

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

Sidebar

Related Questions

I have two jQuery UI custom builds; they were made by different people from
I have two tables, Address & People. People has FK to Address. I'm trying
I am trying to have two submit buttons in my form - one accepts
I am trying to have two data series plotted in one graph as boxes
i have two tables i am trying to get information from. login table -
I have two PNGs that I am trying to combine into a PDF using
I have two very similar programs each trying to run two threads OddThread and
I have two arrayLists and I am trying to subtract one arrayList from another.
I have two arrays of data that I'm trying to amalgamate. One contains actual
I have two tables: hotels and hotel_rooms I'm trying to get a table with

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.