From 705ab271321fa31202ae8d05aeb3a33eedad9374 Mon Sep 17 00:00:00 2001 From: Sebastian Rieger Date: Fri, 27 Oct 2017 21:05:15 +0200 Subject: [PATCH] initial version of multi-threaded tcp server --- .../socket/multithread/TCPServerMulti.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 VerteilteSysteme-Examples/src/verteiltesysteme/socket/multithread/TCPServerMulti.java diff --git a/VerteilteSysteme-Examples/src/verteiltesysteme/socket/multithread/TCPServerMulti.java b/VerteilteSysteme-Examples/src/verteiltesysteme/socket/multithread/TCPServerMulti.java new file mode 100644 index 0000000..1c213c7 --- /dev/null +++ b/VerteilteSysteme-Examples/src/verteiltesysteme/socket/multithread/TCPServerMulti.java @@ -0,0 +1,42 @@ +package verteiltesysteme.socket.multithread; + +import java.io.*; +import java.net.*; + +class TCPServerMulti implements Runnable { + Socket connectionSocket; + + TCPServerMulti(Socket connectionSocket) { + this.connectionSocket = connectionSocket; + } + + public static void main(String argv[]) throws Exception { + int tcpPort = 36037; + + // Server-Socket erzeugen + @SuppressWarnings("resource") + ServerSocket welcomeSocket = new ServerSocket(tcpPort); + + while (true) { + Socket connectionSocket = welcomeSocket.accept(); + new Thread(new TCPServerMulti(connectionSocket)).start(); + } + } + + public void run() { + String clientSentence; + String capitalizedSentence; + + BufferedReader inFromClient; + try { + inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); + DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); + + clientSentence = inFromClient.readLine(); + capitalizedSentence = clientSentence.toUpperCase() + '\n'; + outToClient.writeBytes(capitalizedSentence); + } catch (IOException e) { + e.printStackTrace(); + } + } +}