|
|
@ -26,19 +26,9 @@ public class BioServer {
|
|
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
while (true) {
|
|
|
|
Socket socket = serverSocket.accept();
|
|
|
|
Socket socket = serverSocket.accept();
|
|
|
|
InputStream inputStream = socket.getInputStream();
|
|
|
|
new Thread(() -> {
|
|
|
|
byte[] bytes = new byte[1024];
|
|
|
|
handle(socket);
|
|
|
|
int len = inputStream.read(bytes);
|
|
|
|
}).start();
|
|
|
|
String msg = new String(bytes, 0, len);
|
|
|
|
|
|
|
|
System.out.println("服务端接收到客户端的信息:" + msg);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OutputStream outputStream = socket.getOutputStream();
|
|
|
|
|
|
|
|
outputStream.write("客户端,你好!".getBytes());
|
|
|
|
|
|
|
|
outputStream.flush();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
outputStream.close();
|
|
|
|
|
|
|
|
inputStream.close();
|
|
|
|
|
|
|
|
socket.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
@ -57,4 +47,44 @@ public class BioServer {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void handle(Socket socket) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
InputStream inputStream = null;
|
|
|
|
|
|
|
|
OutputStream outputStream = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
inputStream = socket.getInputStream();
|
|
|
|
|
|
|
|
byte[] bytes = new byte[1024];
|
|
|
|
|
|
|
|
int len = inputStream.read(bytes);
|
|
|
|
|
|
|
|
String msg = new String(bytes, 0, len);
|
|
|
|
|
|
|
|
System.out.println("服务端接收到客户端的信息:" + msg);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
outputStream = socket.getOutputStream();
|
|
|
|
|
|
|
|
outputStream.write("客户端,你好!".getBytes());
|
|
|
|
|
|
|
|
outputStream.flush();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException exception) {
|
|
|
|
|
|
|
|
exception.printStackTrace();
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if(outputStream != null) {
|
|
|
|
|
|
|
|
outputStream.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(inputStream != null) {
|
|
|
|
|
|
|
|
inputStream.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(socket != null) {
|
|
|
|
|
|
|
|
socket.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|