TCPClient.java 1.04 KB
Newer Older
riccardo presotto's avatar
riccardo presotto committed
1 2 3 4 5 6 7 8
import java.io.*; 
import java.net.*;

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

9
		/* Initialize input stream */
riccardo presotto's avatar
riccardo presotto committed
10 11 12
		BufferedReader inFromUser = 
			new BufferedReader(new InputStreamReader(System.in)); 

13
		/* Initialize a client socket, connected to the server */
riccardo presotto's avatar
riccardo presotto committed
14 15
		Socket clientSocket = new Socket("localhost", 6789); 

16
		/* Initialize output stream towards the socket */
riccardo presotto's avatar
riccardo presotto committed
17 18 19
		DataOutputStream outToServer = 
			new DataOutputStream(clientSocket.getOutputStream()); 

20
		/* Initialize input stream from the socket */
riccardo presotto's avatar
riccardo presotto committed
21 22 23 24
		BufferedReader inFromServer = 
			new BufferedReader(new
					InputStreamReader(clientSocket.getInputStream())); 

25
		/* Read an input line */
riccardo presotto's avatar
riccardo presotto committed
26 27
		sentence = inFromUser.readLine(); 

28
		/* Send the line to the server*/
riccardo presotto's avatar
riccardo presotto committed
29 30
		outToServer.writeBytes(sentence + '\n'); 

31
		/* Read response from the server (string ending with '\n') */
riccardo presotto's avatar
riccardo presotto committed
32 33 34 35 36 37 38
		modifiedSentence = inFromServer.readLine(); 

		System.out.println("FROM SERVER: " + modifiedSentence); 

		clientSocket.close(); 
	}
}