03: import java.util.*;
04: class TCPServer {
05: public static void main(String argv[]) {
06: String clientSentence;
07: String capitalizedSentence;
08: ServerSocket welcomeSocket = null;
09: Socket connectionSocket = null;
10: Scanner inFromClient = null;
11: DataOutputStream outToClient = null;
12: try {
13: welcomeSocket = new ServerSocket(6789);
14: }
15: catch (IOException e) {
16: System.out.println("Cannot create a welcome socket");
17: System.exit(1);
18: }
19: while(true) {
20: try {
21: System.out.println("The server is waiting ");
22: connectionSocket = welcomeSocket.accept();
23: inFromClient = new Scanner(connectionSocket.getInputStream());
24: outToClient =
25: new DataOutputStream(connectionSocket.getOutputStream());
26: clientSentence = inFromClient.nextLine();
27: capitalizedSentence = clientSentence.toUpperCase() + '\n';
28: outToClient.writeBytes(capitalizedSentence);
29: }
30: catch (IOException e) {
31: System.out.println("Error cannot create this connection");
32: }
33: finally {
34: try {
35: if (inFromClient != null)
36: inFromClient.close();
37: if (outToClient != null)
38: outToClient.close();
39: if (connectionSocket != null)
40: connectionSocket.close();
41: }
42: catch (IOException e) {
43: e.printStackTrace();
44: }
45: }
46: }
47: }
48: }
49: