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.

44 lines
1.7 KiB

  1. /* Angelehnt an Kurose/Ross Computernetzwerke (bis 5e Java, ab 6e Python) */
  2. package verteiltesysteme.socket.reuse;
  3. import java.io.*;
  4. import java.net.*;
  5. class TCPServerReuse {
  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();
  13. // erlaubt das Wiederverwenden von Sockets, die f�r die individuellen Clients genutzt werden, auch wenn der Port nocht im Status "TIME_WAIT" ist
  14. welcomeSocket.setReuseAddress(true);
  15. welcomeSocket.bind(new InetSocketAddress(tcpPort));
  16. System.out.println("TCP Server started. Waiting for incoming requests... (reuseAddress="+welcomeSocket.getReuseAddress()+", timeout="+welcomeSocket.getSoTimeout() +", buffersize="+welcomeSocket.getReceiveBufferSize()+")");
  17. while (true) {
  18. Socket connectionSocket = welcomeSocket.accept();
  19. System.out.println("Received request from client " + connectionSocket.getInetAddress() + ":" + connectionSocket.getPort() + " generating response...");
  20. BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
  21. DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
  22. // Try/Catch hinzugef�gt, nachdem bei Einsatz Amazon AWS (Healthcheck des ELB) clientSentence null war
  23. try {
  24. clientSentence = inFromClient.readLine();
  25. if (clientSentence != null) {
  26. capitalizedSentence = clientSentence.toUpperCase() + '\n';
  27. outToClient.writeBytes(capitalizedSentence);
  28. }
  29. } catch (IOException ioe) {
  30. ioe.printStackTrace();
  31. }
  32. connectionSocket.close();
  33. }
  34. }
  35. }