Hello everyone, my
方圆
knowledge here is pretty simple
table of Contents
1. BIO blocking model
Briefly describe the response process of the server and the client in the BIO model
- The server
serverSocket
must first and端口
proceed绑定
- After the binding is completed, execute
accept方法
and wait for the client to connect. This method is阻塞式调用
, that is, to wait for the client's connection response, do nothing, wait forever, (the blocked are InputStream.read(), OutputStream. write(), these two will always wait for the client's response) - The client creates
Socket
an object,绑定
the serverip地址
and端口号
, with the server connection - The server receives the client's connection request, the accept method gets it
客户端的socket信息
, and the connection is successful - Server and client create their own
io流
, realize全双工通信
- Anytime afterwards
结束连接
2. Simple practical demonstration
2.1 Server
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
final int DEFAULT_PORT = 8888;
final String QUIT = "quit";
ServerSocket serverSocket = null;
try {
//绑定端口号
serverSocket = new ServerSocket(DEFAULT_PORT);
System.out.println("服务器已经启动,绑定端口号:" + DEFAULT_PORT);
while (true){
//等待客户端的连接
Socket socket = serverSocket.accept();
System.out.println("客户端" + socket.getPort() + ":" + "已经连接");
//获取io流
BufferedReader reader = new BufferedReader(
new InputStreamReader(socket.getInputStream())
);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())
);
//读取客户发送的信息
String msg = null;
while ((msg = reader.readLine()) != null) {
System.out.println("客户端" + socket.getPort() + ":" + msg);
//回复消息
writer.write( msg + " ok" +"\n");
writer.flush();
System.out.println("服务器:" + msg + " ok");
if(msg.equals(QUIT)){
System.out.println("客户端" + socket.getPort() + ":断开连接" );
break;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(serverSocket != null){
try {
serverSocket.close();
System.out.println("服务器Socket关闭");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.2 Client
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) {
final int DEFAULT_SERVER_PORT = 8888;
final String DEFAULT_ADDRESS = "127.0.0.1";
final String QUIT = "quit";
Socket socket = null;
BufferedWriter writer = null;
try {
//创建Socket
socket = new Socket(DEFAULT_ADDRESS,DEFAULT_SERVER_PORT);
//创建io流
writer = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())
);
BufferedReader reader = new BufferedReader(
new InputStreamReader(socket.getInputStream())
);
//等待用户输入信息
while (true) {
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
String msg = consoleReader.readLine();
//向服务器发送消息
writer.write(msg + "\n");
writer.flush();
System.out.println("客户端"+ ":" + msg);
String line = reader.readLine();
System.out.println("服务器:" + line);
//退出判断
if(msg.equals(QUIT)){
break;
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(writer != null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.3 Response results
- Client
- server
3. Deepen understanding
Hurry up and order!