-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEchoThread.java
More file actions
45 lines (38 loc) · 994 Bytes
/
EchoThread.java
File metadata and controls
45 lines (38 loc) · 994 Bytes
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
package com.socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
public class EchoThread implements Runnable{
private Socket client=null;
public EchoThread(Socket client){
this.client=client;
}
@Override
public void run() {
// TODO Auto-generated method stub
PrintStream out=null;
BufferedReader buf=null;
try {
//得到客户端的输入信息
buf=new BufferedReader(new InputStreamReader(client.getInputStream()));
out=new PrintStream(client.getOutputStream());
boolean flag=true;
while(flag){ //客户端循环操作
String str=buf.readLine();
if(str==null || "".equals(str)){
flag=false; //如果为空,客户端操作结束。
}else {
if("bye".equals(str)){
flag=false;
}else {
out.println("ECHO:"+str); //向客户端回显信息
}
}
}
out.close();
client.close();
}catch (Exception e){
}
}
}