Client.java 1.29 KB
Newer Older
Luca Arrotta's avatar
Luca Arrotta committed
1 2 3 4 5 6 7 8 9 10
package iterative;

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

class Client {
    public static void main(String argv[]) throws Exception {
        String sentence;
        String modifiedSentence;

Michele Fiori's avatar
Michele Fiori committed
11
        /* Initialize input stream (from user keyboard) */
Luca Arrotta's avatar
Luca Arrotta committed
12 13 14 15 16 17 18 19
        BufferedReader inFromUser =
                new BufferedReader(new InputStreamReader(System.in));

		/* client socket initialization
			localhost: server address
			6789: server service port number */
        Socket clientSocket = new Socket("localhost", 6789);

Michele Fiori's avatar
Michele Fiori committed
20
        /* Initialize output stream towards the socket */
Luca Arrotta's avatar
Luca Arrotta committed
21 22 23
        DataOutputStream outToServer =
                new DataOutputStream(clientSocket.getOutputStream());

Michele Fiori's avatar
Michele Fiori committed
24
        /* Initialize input stream from the socket */
Luca Arrotta's avatar
Luca Arrotta committed
25 26 27 28
        BufferedReader inFromServer =
                new BufferedReader(
                        new InputStreamReader(clientSocket.getInputStream()));

Michele Fiori's avatar
Michele Fiori committed
29
        /* Read an input line */
Luca Arrotta's avatar
Luca Arrotta committed
30 31
        sentence = inFromUser.readLine();

Michele Fiori's avatar
Michele Fiori committed
32
        /* Send the line to the server*/
Luca Arrotta's avatar
Luca Arrotta committed
33 34
        outToServer.writeBytes(sentence + '\n');

Michele Fiori's avatar
Michele Fiori committed
35
        /* Read response from the server (string ending with '\n') */
Luca Arrotta's avatar
Luca Arrotta committed
36 37
        modifiedSentence = inFromServer.readLine();
        System.out.println("FROM SERVER: " + modifiedSentence);
Michele Fiori's avatar
Michele Fiori committed
38 39

        /* Close the socket */
Luca Arrotta's avatar
Luca Arrotta committed
40 41 42
        clientSocket.close();
    }
}