Brandon Ling
Brandon Ling

Reputation: 3919

Weird Thread occurence with ChatClient

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;
  }
}

Upvotes: 3

Views: 147

Answers (1)

Luna
Luna

Reputation: 1488

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.

Upvotes: 5

Related Questions