forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBIOClient.java
More file actions
43 lines (40 loc) · 1.32 KB
/
BIOClient.java
File metadata and controls
43 lines (40 loc) · 1.32 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
package JavaDemo.IOTest;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Date;
/**
* @Author MaoTian
* @Classname BIOClient
* @Description TODO
* @Date 下午9:23 2019/8/7
* @Version 1.0
* @Created by mao<[email protected]>
*/
public class BIOClient {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
int finalI = i;
new Thread(() -> {
try {
Socket socket = new Socket("127.0.0.1", 3333);
try {
socket.getOutputStream().write((new Date() + ": hello world :"+ finalI).getBytes());
int len;
byte[] data = new byte[1024];
InputStream inputStream = socket.getInputStream();
// 按字节流方式读取数据
while ((len = inputStream.read(data)) != -1) {
System.out.println(new String(data, 0, len));
}
Thread.sleep(5000);
} catch (Exception e) {
}finally {
socket.close();
}
} catch (IOException e) {
}
}).start();
}
}
}