Java Network problem
<p>Im trying to solve an exercise on JAVA IO . The problem i get is that the messages are not coming on the correct order. For example this is what happens:</p>

<p>1.Run server</p>

<p>2.Run Client
3>Type Password</p>

<p>4>>two</p>

<p>5>>three</p>

<p>6>Wrong password</p>

<p>7>>1</p>

<p>8>Wrong password</p>

<p>9>>Trial</p>

<p>10>Waiting for sentence</p>

<p>11>>T</p>

<p>12>TRIAL</p>

<p>Your IP address is :/
Your Socket is : 4351
Current date is : 2011/05/18 15:45:13 .</p>

<p>So for some reason the messeges are not on right order.
This is the code :</p>

<pre><code>import java.io.*;
import java.net.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


class TCPServer {

    public TCPServer() {
    }

    public static void main(String args[])
            throws Exception {
        String clientSentence;
        String capitalisedSentence;
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();

        ServerSocket welcomeSocket = new ServerSocket(4351);

        while (true) {

            Socket clientSocket = welcomeSocket.accept();
            BufferedReader inFromClient =
                    new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            DataOutputStream outToClient =
                    new DataOutputStream(clientSocket.getOutputStream());

            boolean correctPass = false;
            while (!correctPass) {
                if (getPassword(clientSocket,inFromClient,outToClient)) {
                    correctPass = true;
                }
            }


            outToClient.writeBytes("Waiting for sentence"+"\n"); 

            clientSentence = inFromClient.readLine();

            capitalisedSentence = clientSentence.toUpperCase();

            outToClient.writeBytes(capitalisedSentence + "\n"
                    + "Your IP address is :" + clientSocket.getInetAddress() + "\n"
                    + "Your Socket is : " + clientSocket.getLocalPort() + "\n"
                    + "Current date is : " + dateFormat.format(date) + "\n");

        }
    }

    private static boolean getPassword(Socket clientSocket,BufferedReader inFromClient,DataOutputStream outToClient) throws Exception {
        boolean passed = false;
        outToClient.writeBytes("Type password." + "\n");
        while (!passed) {
            String password = inFromClient.readLine();
            if (password.equals("1")) {
                passed = true;

            } else {
                outToClient.writeBytes("Wrong Password" + "\n");
            }

        }
        return true;
    }
}

import java.io.*;
import java.net.*;


class TCPClient {

    public static void main(String args[])
            throws Exception {
        String sentence;
        String modifiedSentence = "";


        BufferedReader inFromUser =
                new BufferedReader(new InputStreamReader(System.in));


        Socket clientSocket = new Socket("localhost", 4351);

        DataOutputStream outToServer =
                new DataOutputStream(clientSocket.getOutputStream());

        BufferedReader inFromServer =
                new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        do{
        while(inFromServer.ready()){
            System.out.println(inFromServer.readLine());
        }
        sentence = inFromUser.readLine();
        outToServer.writeBytes(sentence + "\n");


        }while(true);

        // close the socket and the connection
    }
}
</code></pre>
