java nio解決半包 粘包問題
轉載自:https://blog.csdn.net/nongfuyumin/article/details/78343999
NIO socket是非阻塞的通訊模式,與IO阻塞式的通訊不同點在於NIO的資料要通過channel放到一個快取池ByteBuffer中,然後再從這個快取池中讀出資料,由於服務端快取池大小限制以及網速不均勻等原因,會造成服務端讀取到緩衝池中的資料不完整,就形成了斷包問題,當快取池大小夠大的情況下又會發生一次讀取到快取池中的資料多於一個完整的資料包,這種情況因為無法分清資料包之間的界限,就形成了粘包問題。對於NIO的SocketChannel每次觸發OP_READ事件時,傳送端不一定僅僅寫入了一次,同理,傳送端如果一次傳送資料包過大,那麼傳送端的一次寫入也可能會被拆分成兩次OP_READ事件,所以OP_READ事件和傳送端的OP_WRITE事件並不是一一對應的。
一、斷包、粘包問題的重現
package org.weir.socket.socketPackage;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class NioSocketClient extends Thread {
private SocketChannel socketChannel;
private Selector selector = null;
private int clientId;
public static void main(String args[]) throws IOException {
NioSocketClient client = new NioSocketClient();
client.initClient();
client.start();
}
public NioSocketClient() {
}
public NioSocketClient(int clientId) {
this.clientId = clientId;
}
public void initClient() throws IOException {
InetSocketAddress inetSocketAddress = new InetSocketAddress(8888);
selector = Selector.open();
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.connect(inetSocketAddress);
synchronized (selector) {
socketChannel.register(selector, SelectionKey.OP_CONNECT);
}
}
public void run() {
while (true) {
try {
int key = selector.select();
if (key > 0) {
Set<SelectionKey> keySet = selector.selectedKeys();
Iterator<SelectionKey> iter = keySet.iterator();
while (iter.hasNext()) {
SelectionKey selectionKey = null;
synchronized (iter) {
selectionKey = iter.next();
iter.remove();
}
if (selectionKey.isConnectable()) {
finishConnect(selectionKey);
}
if (selectionKey.isWritable()) {
send(selectionKey);
}
if (selectionKey.isReadable()) {
read(selectionKey);
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void finishConnect(SelectionKey key) {
System.out.println("client finish connect!");
SocketChannel socketChannel = (SocketChannel) key.channel();
try {
socketChannel.finishConnect();
synchronized (selector) {
socketChannel.register(selector, SelectionKey.OP_WRITE);
key.interestOps(SelectionKey.OP_WRITE);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void read(SelectionKey key) throws IOException {
SocketChannel channel = (SocketChannel) key.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
int len = channel.read(byteBuffer);
if (len > 0) {
byteBuffer.flip();
byte[] byteArray = new byte[byteBuffer.limit()];
byteBuffer.get(byteArray);
System.out.println("client[" + clientId + "]" + "receive from server:");
System.out.println(new String(byteArray));
len = channel.read(byteBuffer);
byteBuffer.clear();
}
key.interestOps(SelectionKey.OP_READ);
}
public void send(SelectionKey key) {
SocketChannel channel = (SocketChannel) key.channel();
// byteBuffer.put(ss.getBytes());
for (int i = 0; i < 10; i++) {
String ss = i + "Server ,how are you? this is package message from NioSocketClient!";
ByteBuffer byteBuffer = ByteBuffer.wrap(ss.getBytes());
System.out.println("[client] send:{" + i + "}-- " + ss);
while (byteBuffer.hasRemaining()) {
try {
channel.write(byteBuffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// key.interestOps(SelectionKey.OP_READ);
try {
synchronized (selector) {
channel.register(selector, SelectionKey.OP_READ);
}
} catch (ClosedChannelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* int到byte[]
*
* @param i
* @return
*/
public static byte[] intToBytes(int value) {
byte[] result = new byte[4];
// 由高位到低位
result[0] = (byte) ((value >> 24) & 0xFF);
result[1] = (byte) ((value >> 16) & 0xFF);
result[2] = (byte) ((value >> 8) & 0xFF);
result[3] = (byte) (value & 0xFF);
return result;
}
}
package org.weir.socket.socketPackage;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class NioSocketServer extends Thread {
ServerSocketChannel serverSocketChannel = null;
Selector selector = null;
SelectionKey selectionKey = null;
public void initServer() throws IOException {
selector = Selector.open();
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().bind(new InetSocketAddress(8888));
selectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
}
public void run() {
while (true) {
try {
int selectKey = selector.select();
if (selectKey > 0) {
Set<SelectionKey> keySet = selector.selectedKeys();
Iterator<SelectionKey> iter = keySet.iterator();
while (iter.hasNext()) {
SelectionKey selectionKey = iter.next();
iter.remove();
if (selectionKey.isAcceptable()) {
accept(selectionKey);
}
if (selectionKey.isReadable()) {
read(selectionKey);
}
if (selectionKey.isWritable()) {
// write(selectionKey);
System.out.println();
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
serverSocketChannel.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
public void accept(SelectionKey key) {
try {
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
SocketChannel socketChannel = serverSocketChannel.accept();
System.out.println("is acceptable");
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void read(SelectionKey selectionKey) {
System.out.println("read事件");
try {
SocketChannel channel = (SocketChannel) selectionKey.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(100);
int len = channel.read(byteBuffer);
if (len > 0) {
byteBuffer.flip();
byte[] byteArray = new byte[byteBuffer.limit()];
byteBuffer.get(byteArray);
System.out.println("NioSocketServer receive from client:" + new String(byteArray));
}
selectionKey.interestOps(SelectionKey.OP_READ);
selectionKey.interestOps(SelectionKey.OP_READ);
} catch (IOException e) {
// TODO Auto-generated catch block
try {
serverSocketChannel.close();
selectionKey.cancel();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
public void write(SelectionKey selectionKey) {
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
String httpResponse = "HTTP/1.1 200 OK\r\n" + "Content-Length: 38\r\n" + "Content-Type: text/html\r\n" + "\r\n"
+ "<html><body>Hello World!</body></html>";
System.out.println("response from server to client");
try {
ByteBuffer byteBuffer = ByteBuffer.wrap(httpResponse.getBytes());
while (byteBuffer.hasRemaining()) {
socketChannel.write(byteBuffer);
}
selectionKey.cancel();
} catch (IOException e) {
try {
selectionKey.cancel();
serverSocketChannel.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* byte[]轉int
*
* @param bytes
* @return
*/
public static int byteArrayToInt(byte[] bytes) {
int value = 0;
// 由高位到低位
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) * 8;
value += (bytes[i] & 0x000000FF) << shift;// 往高位遊
}
return value;
}
public static void main(String args[]) throws IOException {
NioSocketServer server = new NioSocketServer();
server.initServer();
server.start();
}
}
執行這兩個類,結果如下:
由於server端的ByteBuffer大小為100,所以會發生粘包問題,當把server端的ByteBuffer大小改為50的情況下,執行結果如下
這種情況下就形成了斷包問題,接收到的資料都是不完整的資料
二、斷包、粘包問題的解決
解決思路是在封裝自己的包協議:包=包內容長度(4byte)+包內容
對於粘包問題先讀出包頭即包體長度n,然後再讀取長度為n的包內容,這樣資料包之間的邊界就清楚了。
對於斷包問題先讀出包頭即包體長度n,由於此次讀取的快取池長度小於n,這時候就需要先快取這部分的內容,等待下次read事件來時拼接起來形成完整的資料包。
由於讀取channel資料到ByteBuffer快取池時ByteBuffer的大小限制,client的一次write事件不一定一一對應server的read事件,所以需要一個全域性變數來快取這部分不完整的資料包。
程式碼如下:
package org.weir.socket.socketPackage;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class NioSocketServer extends Thread {
ServerSocketChannel serverSocketChannel = null;
Selector selector = null;
SelectionKey selectionKey = null;
// 快取一個read事件中一個不完整的包,以待下次read事件到來時拼接成完整的包
ByteBuffer cacheBuffer = ByteBuffer.allocate(100);
boolean cache = false;
public void initServer() throws IOException {
selector = Selector.open();
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().bind(new InetSocketAddress(8888));
selectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
}
public void run() {
while (true) {
try {
int selectKey = selector.select();
if (selectKey > 0) {
Set<SelectionKey> keySet = selector.selectedKeys();
Iterator<SelectionKey> iter = keySet.iterator();
while (iter.hasNext()) {
SelectionKey selectionKey = iter.next();
iter.remove();
if (selectionKey.isAcceptable()) {
accept(selectionKey);
}
if (selectionKey.isReadable()) {
read(selectionKey);
}
if (selectionKey.isWritable()) {
// write(selectionKey);
System.out.println();
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
serverSocketChannel.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
public void accept(SelectionKey key) {
try {
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
SocketChannel socketChannel = serverSocketChannel.accept();
System.out.println("is acceptable");
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 一個client的write事件不一定唯一對應server的read事件,所以需要快取不完整的包,以便拼接成完整的包
//包協議:包=包頭(4byte)+包體,包頭內容為包體的資料長度
public void read(SelectionKey selectionKey) {
System.out.println("read事件");
int head_length = 4;//資料包長度
byte[] headByte = new byte[4];
try {
SocketChannel channel = (SocketChannel) selectionKey.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(100);
int bodyLen = -1;
if (cache) {
cacheBuffer.flip();
byteBuffer.put(cacheBuffer);
}
channel.read(byteBuffer);// 當前read事件
byteBuffer.flip();// write mode to read mode
while (byteBuffer.remaining() > 0) {
if (bodyLen == -1) {// 還沒有讀出包頭,先讀出包頭
if (byteBuffer.remaining() >= head_length) {// 可以讀出包頭,否則快取
byteBuffer.mark();
byteBuffer.get(headByte);
bodyLen = byteArrayToInt(headByte);
} else {
byteBuffer.reset();
cache = true;
cacheBuffer.clear();
cacheBuffer.put(byteBuffer);
break;
}
} else {// 已經讀出包頭
if (byteBuffer.remaining() >= bodyLen) {// 大於等於一個包,否則快取
byte[] bodyByte = new byte[bodyLen];
byteBuffer.get(bodyByte, 0, bodyLen);
bodyLen = -1;
System.out.println("receive from clien content is:" + new String(bodyByte));
} else {
byteBuffer.reset();
cacheBuffer.clear();
cacheBuffer.put(byteBuffer);
cache = true;
break;
}
}
}
selectionKey.interestOps(SelectionKey.OP_READ);
} catch (IOException e) {
// TODO Auto-generated catch block
try {
serverSocketChannel.close();
selectionKey.cancel();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
public void write(SelectionKey selectionKey) {
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
String httpResponse = "HTTP/1.1 200 OK\r\n" + "Content-Length: 38\r\n" + "Content-Type: text/html\r\n" + "\r\n"
+ "<html><body>Hello World!</body></html>";
System.out.println("response from server to client");
try {
ByteBuffer byteBuffer = ByteBuffer.wrap(httpResponse.getBytes());
while (byteBuffer.hasRemaining()) {
socketChannel.write(byteBuffer);
}
selectionKey.cancel();
} catch (IOException e) {
try {
selectionKey.cancel();
serverSocketChannel.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* byte[]轉int
*
* @param bytes
* @return
*/
public static int byteArrayToInt(byte[] bytes) {
int value = 0;
// 由高位到低位
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) * 8;
value += (bytes[i] & 0x000000FF) << shift;// 往高位遊
}
return value;
}
public static void main(String args[]) throws IOException {
NioSocketServer server = new NioSocketServer();
server.initServer();
server.start();
}
}
package org.weir.socket.socketPackage;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class NioSocketClient extends Thread {
private SocketChannel socketChannel;
private Selector selector = null;
private int clientId;
public static void main(String args[]) throws IOException {
NioSocketClient client = new NioSocketClient();
client.initClient();
client.start();
}
public NioSocketClient() {
}
public NioSocketClient(int clientId) {
this.clientId = clientId;
}
public void initClient() throws IOException {
InetSocketAddress inetSocketAddress = new InetSocketAddress(8888);
selector = Selector.open();
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.connect(inetSocketAddress);
synchronized (selector) {
socketChannel.register(selector, SelectionKey.OP_CONNECT);
}
}
public void run() {
while (true) {
try {
int key = selector.select();
if (key > 0) {
Set<SelectionKey> keySet = selector.selectedKeys();
Iterator<SelectionKey> iter = keySet.iterator();
while (iter.hasNext()) {
SelectionKey selectionKey = null;
synchronized (iter) {
selectionKey = iter.next();
iter.remove();
}
if (selectionKey.isConnectable()) {
finishConnect(selectionKey);
}
if (selectionKey.isWritable()) {
send(selectionKey);
}
if (selectionKey.isReadable()) {
read(selectionKey);
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void finishConnect(SelectionKey key) {
System.out.println("client finish connect!");
SocketChannel socketChannel = (SocketChannel) key.channel();
try {
socketChannel.finishConnect();
synchronized (selector) {
socketChannel.register(selector, SelectionKey.OP_WRITE);
key.interestOps(SelectionKey.OP_WRITE);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void read(SelectionKey key) throws IOException {
SocketChannel channel = (SocketChannel) key.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
int len = channel.read(byteBuffer);
if (len > 0) {
byteBuffer.flip();
byte[] byteArray = new byte[byteBuffer.limit()];
byteBuffer.get(byteArray);
System.out.println("client[" + clientId + "]" + "receive from server:");
System.out.println(new String(byteArray));
len = channel.read(byteBuffer);
byteBuffer.clear();
}
key.interestOps(SelectionKey.OP_READ);
}
public void send(SelectionKey key) {
SocketChannel channel = (SocketChannel) key.channel();
for (int i = 0; i < 10; i++) {
String ss = i + "Server ,how are you? this is package message from NioSocketClient!";
int head = (ss).getBytes().length;
ByteBuffer byteBuffer = ByteBuffer.allocate(4 + head);
byteBuffer.put(intToBytes(head));
byteBuffer.put(ss.getBytes());
byteBuffer.flip();
System.out.println("[client] send:" + i + "-- " + head + ss);
while (byteBuffer.hasRemaining()) {
try {
channel.write(byteBuffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// key.interestOps(SelectionKey.OP_READ);
try {
synchronized (selector) {
channel.register(selector, SelectionKey.OP_READ);
}
} catch (ClosedChannelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* int到byte[]
*
* @param i
* @return
*/
public static byte[] intToBytes(int value) {
byte[] result = new byte[4];
// 由高位到低位
result[0] = (byte) ((value >> 24) & 0xFF);
result[1] = (byte) ((value >> 16) & 0xFF);
result[2] = (byte) ((value >> 8) & 0xFF);
result[3] = (byte) (value & 0xFF);
return result;
}
}
執行結果如下:
斷包、粘包問題解決了,接下來寫下關於ByteBuffer這個類的使用。
相關文章
- java nio訊息半包、粘包解決方案Java
- Netty粘包&半包解決方案Netty
- socket的半包,粘包與分包的問題
- Netty原始碼學習6——netty編碼解碼器&粘包半包問題的解決Netty原始碼
- TCP 粘包 - 拆包問題及解決方案TCP
- 詳說tcp粘包和半包TCP
- netty 解決粘包 和 分包的問題Netty
- 粘包問題原因和解決方法
- 粘包問題
- Netty解決粘包和拆包問題的四種方案Netty
- TCP粘包拆包問題TCP
- TCP協議粘包問題詳解TCP協議
- Netty2:粘包/拆包問題與使用LineBasedFrameDecoder的解決方案Netty
- Netty入門系列(2) --使用Netty解決粘包和拆包問題Netty
- Go TCP 粘包問題GoTCP
- 粘包拆包及解決方案
- Netty中使用MessagePack時的TCP粘包問題與解決方案NettyTCP
- Socket 粘包和分包問題
- 25. Socket與粘包問題
- Netty拾遺(七)——粘包與拆包問題Netty
- 深入學習Netty(5)——Netty是如何解決TCP粘包/拆包問題的?NettyTCP
- 修改labelme原始碼,解決粘連mask分離問題原始碼
- 結合RPC框架通訊談 netty如何解決TCP粘包問題RPC框架NettyTCP
- 粘包問題、socketserver模組實現併發Server
- 解決ProtocolHandler ["ajp-nio-8009"] java.net.BindException: Address already in use: bind問題ProtocolJavaIndexException
- Netty如何解決粘包拆包?(二)Netty
- 計算機網路 - TCP粘包、拆包以及解決方案計算機網路TCP
- 訊息粘包 和 訊息不完整 問題
- 01揹包問題的解決
- 【Socket】解決UDP丟包問題UDP
- JAVA | Java 解決跨域問題Java跨域
- 再聊t-io網路程式設計架構的基礎知識:半包和粘包程式設計架構
- Java解決跨域問題Java跨域
- TCP 粘包拆包TCP
- java解決數字黑洞問題Java
- JAVA程式設計題-用java解決兔子問題Java程式設計
- 詳解 Java NIOJava
- Netty - 粘包與拆包Netty