public class SocketServer
1: import java.util.Scanner;
2: import java.io.InputStreamReader;
3: import java.io.DataOutputStream;
4: import java.io.PrintWriter;
5: import java.net.Socket;
6: import java.net.ServerSocket;
7:
8: public class SocketServer
9: {
10: public static void main(String[] args)
11: {
12: String s;
13: Scanner inputStream = null;
14: PrintWriter outputStream = null;
15: ServerSocket serverSocket = null;
16: try
17: {
18: // Wait for connection on port 6789
19: System.out.println("Waiting for a connection.");
20: serverSocket = new ServerSocket(6789);
21: Socket socket = serverSocket.accept();
22: // Connection made, set up streams
23: inputStream = new Scanner(new InputStreamReader(socket.getInputStream()));
24: outputStream = new PrintWriter(new DataOutputStream(socket.getOutputStream()));
25:
26: // Read a line from the client
27: s = inputStream.nextLine();
28:
29: // Output text to the client
30: outputStream.println("Well, ");
31: outputStream.println(s + " is a fine programming language!");
32: outputStream.flush();
33: System.out.println("Closing connection from " + s);
34: inputStream.close();
35: outputStream.close();
36: }
37: catch (Exception e)
38: {
39: // If any exception occurs, display it
40: System.out.println("Error " + e);
41: }
42: }
43: }