Minimalist version
Recent ready to learn a wave netty, but their own are not familiar with the Java NIO, BIO, AIO, so the first to become familiar with the familiar.
Kankan first to document: All the About Sockets
This document is too long-winded. . Sometimes they did not know where to start, just to find a video tutorial or a sister lecture, which I can not sleepy ah, shining on hand to knock again.
What @Test @ Sl4j gave me orgy going, main methods and sout is king
Man of few words said, directly on the code and operating results.
Server code:
public static void main(String[] args) {
final int PORT = 8899;
ServerSocket serverSocket = null;
BufferedWriter writer = null;
try {
serverSocket = new ServerSocket(PORT);
System.out.println("服务器已启动!正在监听端口"+PORT);
while (true){
Socket socket = serverSocket.accept();
System.out.println("客户端 "+socket.getPort()+" 已连接");
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String message = reader.readLine();
if (message != null){
System.out.println("收到一条来自客户端 "+socket.getPort()+" 发送的消息:"+message);
}
//加了\n readLine才能生效
writer.write("你才是" + message + "\n");
writer.flush(); //清理缓冲区
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(writer != null){
try {
writer.close();
System.out.println("服务器挂掉了。。再见");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Client code:
public static void main(String[] args) {
final String Host = "127.0.0.1";
final int Port = 8899;
BufferedWriter writer = null;
try {
Socket socket = new Socket(Host,Port);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
String message = console.readLine();
writer.write(message+"\n");
writer.flush();
String responseMsg = reader.readLine();
System.out.println("收到了来自服务器的回复:" + responseMsg);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(writer != null){
try {
writer.close();
System.out.println("客户端关闭了连接");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Server code to run, this time the service started successfully! :
Then run the client code, the Service and the client connection is successful!:
The client sends a message:
Multi-threaded version
This tutorial is a multi-threaded version, in fact, the server-side code can be reused in the extraction part separately as an inner class inheritance Thread, then the while loop, whenever there is a client visit, will open a thread dedicated to handle the client's request
In fact, the minimalist version of the code above, can also be more fully replicated several Client, and run at the same time, Server-side code in accordance with the results of one order of receipt of the request of the client and returns the corresponding, while loop never tired, unless power outage
to sum up
This is the legend of BIO, and to use it, I feel a simple ratio, as the underlying principle, nothing more than TCP / IP that set (although I do not very familiar with ..)
Next will gradually deepened.
typora of PicGo plug-in has somehow failed to upload pictures of the process is no longer a silky, are not available anywhere reasons, some affect mood. .
Such is life, never knowing a piece of chocolate is bitter sweet.