public class SocketClient
1: //SocketClient.java
2:
3: import java.util.Scanner;
4: import java.io.InputStreamReader;
5: import java.io.DataOutputStream;
6: import java.io.PrintWriter;
7: import java.net.Socket;
8:
9: public class SocketClient
10: {
11: public static void main(String[] args)
12: {
13: String s;
14: Scanner inputStream = null;
15: PrintWriter outputStream = null;
16: try
17: {
18: //Connect to server on same machine, port 6789
19: Socket clientSocket = new Socket("localhost",6789);
20: //Set up streams to send/receive data
21: inputStream = new Scanner(new
22: InputStreamReader(clientSocket.getInputStream()));
23: outputStream = new PrintWriter(new
24: DataOutputStream(clientSocket.getOutputStream()));
25:
26: //Send "Java" to the server
27: outputStream.println("Java");
28: outputStream.flush(); //Sends data to the stream
29:
30: //Read everything from the server until no more lines
31: //and output it to the screen
32: while (inputStream.hasNextLine())
33: {
34: s = inputStream.nextLine();
35: System.out.println(s);
36: }
37: inputStream.close();
38: outputStream.close();
39: }
40: catch (Exception e)
41: {
42: //If any exception occurs, display it
43: System.out.println("Error: " + e);
44: }
45: }
46: }
47: