forked from kindsword/learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketServerTest.java
More file actions
58 lines (53 loc) · 1.72 KB
/
SocketServerTest.java
File metadata and controls
58 lines (53 loc) · 1.72 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
/*************************************************************************
> File Name: SocketServerTest.java
> Author:
> Mail:
> Created Time: 2017年07月28日 星期五 16时33分35秒
************************************************************************/
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class SocketServerTest {
public static void main(String[] args) {
ServerSocket ser;
try {
ser = new ServerSocket(8189);
while(true) {
Socket client = ser.accept();
ServerThread t = new ServerThread(client);
t.start();
System.out.println("Start Client:" + t.getId());
}
} catch(Exception ex) {
}
}
}
class ServerThread extends Thread {
private Socket client;
public ServerThread (Socket client) {
super();
this.client = client;
}
@Override
public void run() {
try {
InputStream inStream = client.getInputStream();
OutputStream outStream = client.getOutputStream();
Scanner in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outStream, true);
out.println("Thread Running:" + this.getId());
while(true) {
String value = in.nextLine();
out.println("Server:" + value);
//out.println("Thread Running:" + this.getId());
//Thread.sleep(5000);
if (value.equals("exit")) {
out.println("Bye !!!");
break;
}
}
client.close();
} catch(Exception ex) {
}
}
}