Browse Source

initial version of multi-threaded tcp server

master
Sebastian Rieger 7 years ago
parent
commit
705ab27132
  1. 42
      VerteilteSysteme-Examples/src/verteiltesysteme/socket/multithread/TCPServerMulti.java

42
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();
}
}
}
Loading…
Cancel
Save