Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3ecfc45

Browse filesBrowse files
committed
BIO底层原理
1 parent a395910 commit 3ecfc45
Copy full SHA for 3ecfc45

File tree

Expand file treeCollapse file tree

2 files changed

+99
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+99
-0
lines changed
Open diff view settings
Collapse file
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.learnjava.io.bio;
2+
3+
import java.io.InputStream;
4+
import java.net.ServerSocket;
5+
import java.net.Socket;
6+
7+
/**
8+
* @author lhy
9+
*
10+
* 在windows服务器下,可以使用telnet来合serversocket建立连接
11+
*/
12+
public class BIO {
13+
public static void main(String[] args) throws Exception {
14+
ServerSocket serverSocket = new ServerSocket(666);
15+
System.out.println("Server started...");
16+
while (true) {
17+
System.out.println("socket accepting...");
18+
Socket socket = serverSocket.accept();
19+
new Thread(new Runnable() {
20+
@Override
21+
public void run() {
22+
try {
23+
byte[] bytes = new byte[1024];
24+
InputStream inputStream = socket.getInputStream();
25+
while (true) {
26+
System.out.println("reading...");
27+
int read = inputStream.read(bytes);
28+
if (read != -1) {
29+
System.out.println(new String(bytes, 0, read));
30+
} else {
31+
break;
32+
}
33+
}
34+
} catch (Exception e) {
35+
e.printStackTrace();
36+
} finally {
37+
try {
38+
socket.close();
39+
} catch (Exception e) {
40+
e.printStackTrace();
41+
}
42+
}
43+
}
44+
}).start();
45+
}
46+
}
47+
}
Collapse file

‎note/Netty/IO底层原理.md‎

Copy file name to clipboard
+52Lines changed: 52 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## 从linux kernel内核出发,IO底层原理
2+
3+
### 1. BIO
4+
5+
```
6+
import java.io.InputStream;
7+
import java.net.ServerSocket;
8+
import java.net.Socket;
9+
10+
/**
11+
* @author lhy
12+
*
13+
* 在windows服务器下,可以使用telnet来合serversocket建立连接
14+
*/
15+
public class BIO {
16+
public static void main(String[] args) throws Exception {
17+
ServerSocket serverSocket = new ServerSocket(666);
18+
System.out.println("Server started...");
19+
while (true) {
20+
System.out.println("socket accepting...");
21+
Socket socket = serverSocket.accept();
22+
new Thread(new Runnable() {
23+
@Override
24+
public void run() {
25+
try {
26+
byte[] bytes = new byte[1024];
27+
InputStream inputStream = socket.getInputStream();
28+
while (true) {
29+
System.out.println("reading...");
30+
int read = inputStream.read(bytes);
31+
if (read != -1) {
32+
System.out.println(new String(bytes, 0, read));
33+
} else {
34+
break;
35+
}
36+
}
37+
} catch (Exception e) {
38+
e.printStackTrace();
39+
} finally {
40+
try {
41+
socket.close();
42+
} catch (Exception e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
}
47+
}).start();
48+
}
49+
}
50+
}
51+
```
52+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.