summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/SimpleEchoServer.java
blob: c4d53796a1f49546382278b9da2627a63625bd94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* SimpleEchoServer.java */

import java.net.*;
import java.io.*;

public class SimpleEchoServer
{
  public static void main(String[] args)
  {
    try {
      System.out.println("Warte auf Verbindung auf Port 7...");
      ServerSocket echod = new ServerSocket(7);
      Socket socket = echod.accept();
      System.out.println("Verbindung hergestellt");
      InputStream in = socket.getInputStream();
      OutputStream out = socket.getOutputStream();
      int c;
      while ((c = in.read()) != -1) {
        out.write((char)c);
        System.out.print((char)c);
      }
      System.out.println("Verbindung beenden");
      socket.close();
      echod.close();
    } catch (IOException e) {
      System.err.println(e.toString());
      System.exit(1);
    }
  }
}