blob: d0d93e0dc79c281f9c23b58410d52b3624ed75ee (
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
/* EchoServer.java */
import java.net.*;
import java.io.*;
public class EchoServer
{
public static void main(String[] args)
{
int cnt = 0;
try {
System.out.println("Warte auf Verbindungen auf Port 7...");
ServerSocket echod = new ServerSocket(7);
while (true) {
Socket socket = echod.accept();
(new EchoClientThread(++cnt, socket)).start();
}
} catch (IOException e) {
System.err.println(e.toString());
System.exit(1);
}
}
}
class EchoClientThread
extends Thread
{
private int name;
private Socket socket;
public EchoClientThread(int name, Socket socket)
{
this.name = name;
this.socket = socket;
}
public void run()
{
String msg = "EchoServer: Verbindung " + name;
System.out.println(msg + " hergestellt");
try {
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
out.write((msg + "\r\n").getBytes());
int c;
while ((c = in.read()) != -1) {
out.write((char)c);
System.out.print((char)c);
}
System.out.println("Verbindung " + name + " wird beendet");
socket.close();
} catch (IOException e) {
System.err.println(e.toString());
}
}
}
|