Skip to content

Commit cb1c0e2

Browse files
committed
上传文件
1 parent 7b2a0ee commit cb1c0e2

File tree

4 files changed

+248
-0
lines changed

4 files changed

+248
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package base.io.bio;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.io.PrintWriter;
7+
import java.net.Socket;
8+
import java.util.Random;
9+
10+
/**
11+
* @author zhangluping on 2018/8/22.
12+
*/
13+
public class BIOClient {
14+
/**
15+
* 默认的端口号
16+
*/
17+
private static int DEFAULT_SERVER_PORT = 12345;
18+
private static String DEFAULT_SERVER_IP = "127.0.0.1";
19+
20+
public static void send(String expression) {
21+
send(DEFAULT_SERVER_PORT, expression);
22+
}
23+
24+
public static void send(int port, String expression) {
25+
System.out.println("算术表达式为:" + expression);
26+
Socket socket = null;
27+
BufferedReader in = null;
28+
PrintWriter out = null;
29+
try {
30+
socket = new Socket(DEFAULT_SERVER_IP, port);
31+
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
32+
out = new PrintWriter(socket.getOutputStream(), true);
33+
out.println(expression);
34+
System.out.println("___结果为:" + in.readLine());
35+
} catch (Exception e) {
36+
e.printStackTrace();
37+
} finally {
38+
//一下必要的清理工作
39+
if (in != null) {
40+
try {
41+
in.close();
42+
} catch (IOException e) {
43+
e.printStackTrace();
44+
}
45+
in = null;
46+
}
47+
if (out != null) {
48+
out.close();
49+
out = null;
50+
}
51+
if (socket != null) {
52+
try {
53+
socket.close();
54+
} catch (IOException e) {
55+
e.printStackTrace();
56+
}
57+
socket = null;
58+
}
59+
}
60+
}
61+
62+
//测试主方法
63+
public static void main(String[] args) throws InterruptedException {
64+
//运行客户端
65+
new Thread(new Runnable() {
66+
@SuppressWarnings("static-access")
67+
@Override
68+
public void run() {
69+
while(true){
70+
char operators[] = {'+','-','*','/'};
71+
Random random = new Random(System.currentTimeMillis());
72+
//随机产生算术表达式
73+
String expression = random.nextInt(10)+""+operators[random.nextInt(4)]+(random.nextInt(10)+1);
74+
BIOClient.send(expression);
75+
try {
76+
Thread.currentThread().sleep(random.nextInt(1000));
77+
} catch (InterruptedException e) {
78+
e.printStackTrace();
79+
}
80+
}
81+
}
82+
}).start();
83+
}
84+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package base.io.bio;
2+
3+
import java.io.IOException;
4+
import java.net.ServerSocket;
5+
import java.net.Socket;
6+
7+
/**
8+
* @author zhangluping on 2018/8/22.
9+
*/
10+
public class BIOServer {
11+
/**
12+
* 默认的端口号
13+
*/
14+
private static int DEFAULT_PORT = 12345;
15+
/**
16+
* 单例的ServerSocket
17+
*/
18+
private static ServerSocket server;
19+
20+
/**
21+
* 根据传入参数设置监听端口,如果没有参数调用以下方法并使用默认值
22+
*
23+
* @throws IOException
24+
*/
25+
public static void start() throws IOException {
26+
//使用默认值
27+
start(DEFAULT_PORT);
28+
}
29+
30+
/**
31+
* 这个方法不会被大量并发访问,不太需要考虑效率,直接进行方法同步就行了
32+
*
33+
* @param port
34+
* @throws IOException
35+
*/
36+
public synchronized static void start(int port) throws IOException {
37+
if (server != null) {
38+
return;
39+
}
40+
try {
41+
//通过构造函数创建ServerSocket
42+
//如果端口合法且空闲,服务端就监听成功
43+
server = new ServerSocket(port);
44+
System.out.println("服务器已启动,端口号:" + port);
45+
//通过无线循环监听客户端连接
46+
//如果没有客户端接入,将阻塞在accept操作上。
47+
while (true) {
48+
Socket socket = server.accept();
49+
//当有新的客户端接入时,会执行下面的代码
50+
//然后创建一个新的线程处理这条Socket链路
51+
new Thread(new BIOServerHandler(socket)).start();
52+
}
53+
} finally {
54+
//一些必要的清理工作
55+
if (server != null) {
56+
System.out.println("服务器已关闭。");
57+
server.close();
58+
server = null;
59+
}
60+
}
61+
}
62+
63+
public static void main(String[] args) throws InterruptedException {
64+
//运行服务器
65+
new Thread(new Runnable() {
66+
@Override
67+
public void run() {
68+
try {
69+
BIOServer.start();
70+
} catch (IOException e) {
71+
e.printStackTrace();
72+
}
73+
}
74+
}).start();
75+
}
76+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package base.io.bio;
2+
3+
import base.io.util.Calculator;
4+
5+
import java.io.BufferedReader;
6+
import java.io.IOException;
7+
import java.io.InputStreamReader;
8+
import java.io.PrintWriter;
9+
import java.net.Socket;
10+
11+
/**
12+
* 客户端线程
13+
* 用于处理一个客户端的Socket链路
14+
*
15+
* @author zhangluping on 2018/8/22.
16+
*/
17+
public class BIOServerHandler implements Runnable {
18+
private Socket socket;
19+
20+
public BIOServerHandler(Socket socket) {
21+
this.socket = socket;
22+
}
23+
24+
@Override
25+
public void run() {
26+
BufferedReader in = null;
27+
PrintWriter out = null;
28+
try {
29+
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
30+
out = new PrintWriter(socket.getOutputStream(), true);
31+
String expression;
32+
String result;
33+
while (true) {
34+
//通过BufferedReader读取一行
35+
//如果已经读到输入流尾部,返回null,退出循环
36+
//如果得到非空值,就尝试计算结果并返回
37+
if ((expression = in.readLine()) == null) {
38+
break;
39+
}
40+
System.out.println(Thread.currentThread().getName()+ "服务器收到消息:" + expression);
41+
try {
42+
result = Calculator.cal(expression).toString();
43+
} catch (Exception e) {
44+
result = "计算错误:" + e.getMessage();
45+
}
46+
out.println(result);
47+
}
48+
} catch (Exception e) {
49+
e.printStackTrace();
50+
} finally {
51+
//一些必要的清理工作
52+
if (in != null) {
53+
try {
54+
in.close();
55+
} catch (IOException e) {
56+
e.printStackTrace();
57+
}
58+
in = null;
59+
}
60+
if (out != null) {
61+
out.close();
62+
out = null;
63+
}
64+
if (socket != null) {
65+
try {
66+
socket.close();
67+
} catch (IOException e) {
68+
e.printStackTrace();
69+
}
70+
socket = null;
71+
}
72+
}
73+
}
74+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package base.io.util;
2+
3+
import javax.script.ScriptEngine;
4+
import javax.script.ScriptEngineManager;
5+
import javax.script.ScriptException;
6+
7+
8+
public final class Calculator {
9+
private final static ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript");
10+
11+
public static Object cal(String expression) throws ScriptException {
12+
return jse.eval(expression);
13+
}
14+
}

0 commit comments

Comments
 (0)