You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.3 KiB

6 years ago
6 years ago
  1. /* Angelehnt an Kurose/Ross Computernetzwerke (bis 5e Java, ab 6e Python) */
  2. package verteiltesysteme.socket.simple;
  3. import java.io.*;
  4. import java.net.*;
  5. class TCPServer {
  6. public static void main(String argv[]) throws Exception {
  7. int tcpPort = 36037;
  8. String clientSentence;
  9. String capitalizedSentence;
  10. // Server-Socket erzeugen
  11. @SuppressWarnings("resource")
  12. ServerSocket welcomeSocket = new ServerSocket(tcpPort);
  13. System.out.println("TCP Server started. Waiting for incoming requests...");
  14. while (true) {
  15. Socket connectionSocket = welcomeSocket.accept();
  16. System.out.println("Received request from client " + connectionSocket.getInetAddress() + ":" + connectionSocket.getPort() + " generating response...");
  17. BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
  18. DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
  19. // Try/Catch hinzugef�gt, nachdem bei Einsatz Amazon AWS (Healthcheck des ELB) clientSentence null war
  20. try {
  21. clientSentence = inFromClient.readLine();
  22. if (clientSentence != null) {
  23. capitalizedSentence = clientSentence.toUpperCase() + '\n';
  24. outToClient.writeBytes(capitalizedSentence);
  25. }
  26. } catch (IOException ioe) {
  27. ioe.printStackTrace();
  28. }
  29. connectionSocket.close();
  30. }
  31. }
  32. }