-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEchoer.java
More file actions
49 lines (41 loc) · 1.3 KB
/
Echoer.java
File metadata and controls
49 lines (41 loc) · 1.3 KB
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
package com.dj;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Echoer extends Thread{
private Socket socket;
public Echoer(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
BufferedReader input = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
while (true) {
String echoString = input.readLine();
System.out.println("Received client input: " + echoString);
if (echoString.equals("exit")) {
break;
}
// try {
// Thread.sleep(15000);
// } catch (InterruptedException e) {
// System.out.println("Thread interrupted");
// }
output.println(echoString);
}
} catch (IOException e) {
System.out.println("Oops: " + e.getMessage());
} finally {
try {
socket.close();
} catch (IOException e) {
//Oh well
}
}
}
}