forked from coderbruis/JavaSourceCodeLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBIO.java
More file actions
47 lines (45 loc) · 1.58 KB
/
Copy pathBIO.java
File metadata and controls
47 lines (45 loc) · 1.58 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
package com.learnjava.io.bio;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author lhy
*
* 在windows服务器下,可以使用telnet来合serversocket建立连接
*/
public class BIO {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(666);
System.out.println("Server started...");
while (true) {
System.out.println("socket accepting...");
Socket socket = serverSocket.accept();
new Thread(new Runnable() {
@Override
public void run() {
try {
byte[] bytes = new byte[1024];
InputStream inputStream = socket.getInputStream();
while (true) {
System.out.println("reading...");
int read = inputStream.read(bytes);
if (read != -1) {
System.out.println(new String(bytes, 0, read));
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
}
}