-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerThread.java
More file actions
60 lines (53 loc) · 1.29 KB
/
ServerThread.java
File metadata and controls
60 lines (53 loc) · 1.29 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
50
51
52
53
54
55
56
57
58
59
60
import java.io.*;
import java.net.*;
public class ServerThread extends ServerSocket{
private static final int SERVER_PORT = 10000;
static int num = 0;
public ServerThread() throws IOException{
super(SERVER_PORT);
try{
while(true){
Socket socket = accept();
new CreateServerThread(socket);
}
}catch (IOException e) {
}finally{
close();
}
}
class CreateServerThread extends Thread{
private Socket client;
private BufferedReader in;
private PrintWriter out;
public CreateServerThread(Socket s) throws IOException{
client = s;
in = new BufferedReader(new InputStreamReader(client.getInputStream(),"GB2312"));
out = new PrintWriter(client.getOutputStream(),true);
out.println("--Welcome--");
start();
}
public void run(){
try{
String line = in.readLine();
while(!line.equals("bye")){
String msg = createMessage(line);
System.out.println(msg);
out.println(msg);
line = in.readLine();
}
out.println("--see you,bye!--");
client.close();
}catch (IOException e) {
}
}
private String createMessage(String line){
return line+" "+(++num);
//System.out.println(line);
}
}
public static void main(String[] args)throws IOException {
new ServerThread();
}
//下面测试tag
git tag tagname
}