Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: start client actions only once all clients is connected

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default start client actions only once all clients is connected

    I was tasked to do a 2-4 player network card game. Before I jumped into doing the card game. I decided to try out
    an a simple program which is the clients taking turns to type in their textfield.

    I have a server that listens to 3 incoming client connections. once all 3 clients is being connected,

    Steps

    1) The first client will send the message first while the second and third client will wait for the first client to finish sending.

    2) After the first client finished sending the message, it will be second client turn to send the message and the third client will wait for the second client to send finish.

    3) After the third client finished sending the message, it will be third client turn to send the message and the first client will wait for the third client to send finish and (go back to step 1))

    **The first client can only start typing once all 3 clients is being connected**

    In my server class,I spawn a new Thread once a client is connected, I do a wait() on each time the client is being connected, I stored the thread in a arraylist as well. I have an id to keep track how many clients is currently connected. once the id reaches 3, I used notifyall() to notify all the threads and set the first position of my arraylist as my currentThread so that I can keep track which is the currentThread running.

    I started 3 clients. and I typed the textfield of the first client but my server isn't receiving any messages.

            class Server implements Runnable {
            private List<SpawnNewThread> sntList = new ArrayList<SpawnNewThread>();
            private SpawnNewThread currentThread;
            private int id = 0;
     
    	 	@Override
    		public void run() {
    			try {
    				ServerSocket sSocket = new ServerSocket(4444);
    				while(true) {
    					Socket socket = sSocket.accept();
    					id++;
    					SpawnNewThread snt = new SpawnNewThread(socket);
                        /*wait for 2 more clients to connect before the first client can enter a text
                        in the textField*/ 
    					snt.wait();	
                        sntList.add(snt);
    					if(id == 3) {
                            //notify all threads if 3 clients is being connected
    				        notifyAll();
                            //set the first thread in the sntList to first thread
                            currentThread = sntList.get(0); 
    					}
     
    					snt.start();
    			     }
     
    			} catch(IOException exception) {
    				System.out.println("Error: " + exception);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}

    I think my logic is wrong and I really need help on how to implemented a turn-based program.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,098
    My Mood
    Mellow
    Thanks
    244
    Thanked 754 Times in 740 Posts

    Default Re: start client actions only once all clients is connected

    Try crawling before running. Get a server and single client sending, receiving, and responding to messages. Once you've gotten that, add a second client that does the same but also ensure that the server associates correctly the messages it receives with the client that sent them. Add the restriction that the two clients must take turns, each sending after the other - a counting exercise might be interesting. Then add a third client and devise a scheme that only allows one client to send at a time and in a specific order. A "token ring" concept comes to mind with the clients requiring a token to send, and the server passes the token to the clients in order, according to its communication rules.

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: start client actions only once all clients is connected

    I am able to do the part whereby "a second client that does the same but also ensure that the server associates correctly the messages it receives with the client that sent them". Now for the restrictions, Instead of using a counting approach, my idea was to set the List<SpawnNewThread> sntList = new Arraylist<SpawnNewThread>() of position 0 to currentThread.

    so I will have like

    SpawnNewThread currentThread;
    currentThread = sntList.get(0);

    I will then create a a method boolean isYourTurn(). it will run through the loop
    of the arrayList and check if the position of the arrayList matches if the currentThread

    public boolean isYourTurn() {
        boolean whoseTurn = false;
        for(int i =0; i< sntList.size(); i++ ) {
           if(currentThread == sntList.get(i)) {
               whoseTurn = true;
           }
          return whoseTurn;
        }
    }

    I will then pass this boolean variable back to my client and inform which
    client textfield should be disabled.

    I am currently still testing it out but meanwhile can anyone advise me whether I am on the right track?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,098
    My Mood
    Mellow
    Thanks
    244
    Thanked 754 Times in 740 Posts

    Default Re: start client actions only once all clients is connected

    Your approach sounds reasonable, maybe more complicated than it needs to be. Can't you simply "shut down" the last client to respond and then "turn on" the next client whose turn it has become? I imagine a simple state machine to decide which client is on, which are off, and to manage or trigger the transitions, but that could be overkill.

Similar Threads

  1. Client broadcast message to all clients????
    By Ibrahim in forum Java Networking
    Replies: 6
    Last Post: June 8th, 2014, 12:26 PM
  2. Replies: 15
    Last Post: May 9th, 2014, 01:00 PM
  3. Replies: 0
    Last Post: May 8th, 2014, 03:38 PM
  4. Multiple clients and one server. How to send data from one client to the others
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 1
    Last Post: March 21st, 2012, 08:29 PM
  5. help with keeping client connected to server
    By frozen java in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 19th, 2011, 02:29 PM