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 10 of 10

Thread: When you have a multi-threaded Server and Client program....

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default When you have a multi-threaded Server and Client program....

    ...how exactly do you ensure that the Server always sends the data after the Client has entered a wait state for the data?

    As in, if I have the Server write something and the Client read something from the server, how do I always ensure that the Server didn't write before the server began waiting to read?

    The lazy solution is to have the thread in the server sleep for 2000 ms or so before it sends across the data, so that you can pretty much guarantee that by the time those 2000 ms are up the Client would have reached the "ready to read data from the server" stage.

    But that's a crappy solution. How exactly is this kind of stuff handled?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: When you have a multi-threaded Server and Client program....

    how do I always ensure that the Server didn't write before the server(client??) began waiting to read?
    Do you have code that has a problem?
    What is keeping the client from using a thread that is ready to read?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: When you have a multi-threaded Server and Client program....

    Yes, but the code is kind of complicated. I'll try to give a brief version here.

    The Client is playing a game. He connects to the server, and the server executes the game and gives the client whatever information he needs to know about what's happening in the game.

    In this example, the Server is telling the Client that the game is over.

    After telling the Client that the game is over, the Server wants to tell the Client who won. He then sends "gameResults", which is a String saying who won, or if it was a draw, etc.

    So,
    1.) The client is waiting for some message from the server. He's just sitting there waiting.
    2.) Since the game is now over, the Server says "game over" to the server.
    3.) The client receives it, and, since the game is over, he now needs to know who won or whether it was a draw, or what.
    4.) The client begins waiting
    4.) The server sends the "gameResults" to the client

    We have two step fours. How do I ensure that the client begins waiting before the server sends the information?


    Server code
                            try
                            {
                                    outputToClient.writeBytes("Game over\n");
     
                                    try
                                    {
                                            Thread.sleep(2500);
     
                                    } catch (InterruptedException ex)
                                    {
                                            System.out.println(ex);
                                    }
     
                                    outputToClient.writeBytes(gameResults() + "\n");
     
                                    sock.close();
     
                            } catch (IOException ex)
                            {
                                    System.err.println(ex);
                            }

    Client code
                 while(gameInProgress)
    		{
    			try
    			{	
    				processMessage(d.readLine());
     
    			} catch (IOException ex)
    			{
    				System.err.println(ex);
    			}
    		}	
     
    	}
     
    	public void processMessage(String message)
    	{
    		if (message.equals("Valid move."))
    		{
    			System.out.println("Valid move, please wait.");
     
    			setSymbol(location, mySymbol); 
    			board.printBoard();
    		}
    		else if (message.equals("Invalid move."))
    		{
    			System.out.println(message);
    			myTurn = true;
    			board.printBoard();
    			location = userInput.nextDouble();
     
    			try
                            {
                                    outToServer.writeDouble(location);
     
                            } catch (IOException ex)
                            {
                                    System.err.println(ex);
                            }
     
    		}
    		else if (message.equals("Opponent moved"))
    		{
    			try
    			{
    				location = inpFromServer.readDouble();
     
    			} catch (IOException ex)
    			{
    				System.err.println(ex);
    			}
     
    			setSymbol(location, opponentSymbol);
     
    			board.printBoard();
     
    			System.out.println("Opponent moved. Your turn.");
     
    			location = userInput.nextDouble();
     
    			try
    			{
    				outToServer.writeDouble(location);
     
    			} catch (IOException ex)
    			{
    				System.err.println(ex);
    			}
    			myTurn = true;	
    		}
    		else if (message.equals("Game over"))
    		{
    			gameInProgress = false;
     
    			BufferedReader b = null;
     
    			try
    			{
    				b = new BufferedReader(new InputStreamReader(connection.getInputStream()));
     
    				board.printBoard();
     
    				System.out.println(b.readLine());
    				connection.close();
     
    			} catch (IOException ex)
    			{
    				System.err.println(ex);
    			}
    		}
    		else
    		{
    			System.out.println("We don't understand the server's message: ' " + message + " ' ");
    		}	
    	}

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: When you have a multi-threaded Server and Client program....

    How do I ensure that the client begins waiting before the server sends the information?
    Have a thread on the client has a thread waiting for the server's response?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: When you have a multi-threaded Server and Client program....

    I'm sorry...I don't understand what you mean.

    All I have is the client saying "System.out.println(b.readLine());"

    So, he's waiting for the Server.

    But that's useless if the Server sends the data before the Client reaches that line of code.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: When you have a multi-threaded Server and Client program....

    What I'm suggesting is the client should be waiting immediately after sending a message to the server.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: When you have a multi-threaded Server and Client program....

    I see what you're saying.

    Here, though, the Server is kind of sending two messages in a row.

    I suppose that's bad practice?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: When you have a multi-threaded Server and Client program....

    Why can't the client read the messages as fast as the server sends them?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: When you have a multi-threaded Server and Client program....

    Well....what if the Server sends something, then in the next line of code sends another message.

    Then the client receives the first message....has to do stuff with it...then, when it's done, the client is ready for the second message. But by then the server has already sent the message!!

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: When you have a multi-threaded Server and Client program....

    Computers are quite fast. Much faster than internet transmit times. I'm sure the client can copy the message somewhere and get done soon enough to be ready to read the next input. Also the OS buffers the data.

    Are you having messages lost?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] can someone help me in this server and multi client
    By wuliao123 in forum What's Wrong With My Code?
    Replies: 51
    Last Post: February 27th, 2013, 08:31 AM
  2. [SOLVED] My chat program's multi threaded server keeps on throwing exceptions when calling nextLine();
    By Chromejuice in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 2nd, 2012, 10:52 PM
  3. Threads in multi client-server application
    By KingOfClubs in forum Threads
    Replies: 1
    Last Post: June 11th, 2011, 12:10 AM
  4. Multi-Threaded Chat Server
    By TopdeK in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 7th, 2011, 10:12 AM
  5. Replies: 1
    Last Post: October 19th, 2009, 11:53 PM