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.

46 lines
1.2 KiB

  1. package verteiltesysteme.socket.multithread;
  2. import java.io.BufferedReader;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8. class TCPServerMulti implements Runnable {
  9. Socket connectionSocket;
  10. TCPServerMulti(Socket connectionSocket) {
  11. this.connectionSocket = connectionSocket;
  12. }
  13. public static void main(String argv[]) throws Exception {
  14. int tcpPort = 36037;
  15. // Server-Socket erzeugen
  16. @SuppressWarnings("resource")
  17. ServerSocket welcomeSocket = new ServerSocket(tcpPort);
  18. while (true) {
  19. Socket connectionSocket = welcomeSocket.accept();
  20. new Thread(new TCPServerMulti(connectionSocket)).start();
  21. }
  22. }
  23. public void run() {
  24. String clientSentence;
  25. String capitalizedSentence;
  26. BufferedReader inFromClient;
  27. try {
  28. inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
  29. DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
  30. clientSentence = inFromClient.readLine();
  31. capitalizedSentence = clientSentence.toUpperCase() + '\n';
  32. outToClient.writeBytes(capitalizedSentence);
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. }