java Socket無法完全接收返回內(nèi)容的解決方案
最近在使用Socket通訊時,遇到了接收內(nèi)容不全(返回內(nèi)容 = 4字節(jié)報文長度 + 內(nèi)容主體)的問題:客戶端發(fā)送請求數(shù)據(jù),服務(wù)器明明返回了73個字節(jié)內(nèi)容,但客戶端有時能全部接收,但有時卻只能返回4個字節(jié)。
一開始是懷疑服務(wù)器返回有問題,但使用調(diào)試工具連續(xù)測試了很多次,結(jié)果顯示:服務(wù)器的確每次都返回了73個字節(jié)內(nèi)容。那很明顯了,問題出現(xiàn)在客戶端代碼上。
錯誤現(xiàn)象

再來看看調(diào)試工具結(jié)果:

讓我們來看看客戶端代碼,調(diào)用方法如下:(該方法適用于返回報文前兩個字節(jié)表示長度的情況:2字節(jié)報文長度 + 內(nèi)容主體)
public static void test() {
SocketClient client = new SocketClient();
// 建立socket對象
int iret = client.connect("192.168.1.105", 1234);
if (iret == 0) {
// 發(fā)送數(shù)據(jù)
client.write("helloworld".getBytes());
// 接收數(shù)據(jù)
byte data[] = client.read();
if ((data != null) && (data.length != 0)) {
// 處理接收結(jié)果
Utils.print("響應(yīng)報文字節(jié)數(shù)組---->" + Arrays.toString(data));
}
}
}
SocketClient.java源碼:
public class SocketClient {
// 存儲接收數(shù)據(jù)
private byte m_buffer[] = new byte[0x10000];
private Socket m_socket;
private InputStream m_inputstream;
private OutputStream m_outputstream;
private BufferedInputStream m_bufferedinputstream;
private BufferedOutputStream m_bufferedoutputstream;
private boolean connected;
public int connect(String host, int port) {
try {
SocketAddress socketAddress = new InetSocketAddress(host, port);
m_socket = new Socket();
m_socket.connect(socketAddress, 5000);
m_socket.setSoTimeout(60000);
m_inputstream = m_socket.getInputStream();
m_bufferedinputstream = new BufferedInputStream(m_inputstream);
m_outputstream = m_socket.getOutputStream();
m_bufferedoutputstream = new BufferedOutputStream(m_outputstream);
} catch (Exception e) {
return -1;
}
connected = true;
return 0;
}
/**
* 發(fā)送請求數(shù)據(jù)
*
* @param data
* @param start
* @param end
* @return
*/
public int write(byte data[]) {
if (data == null || data.length == 0 || !connected) {
return 0;
}
try {
m_bufferedoutputstream.write(data, 0, data.length);
m_bufferedoutputstream.flush();
} catch (Exception e) {
return -1;
}
return 0;
}
/**
* 讀取返回數(shù)據(jù)
*
* @return
*/
public byte[] read() {
if (!connected) {
return null;
}
int len = -1;
try {
// 長度不正確,有時返回4,有時返回73
len = m_bufferedinputstream.read(m_buffer, 0, 0x10000);
} catch (Exception e) {
len = 0;
}
if (len != -1) {
return null;
} else {
byte ret[] = new byte[len];
for (int i = 0; i < len; i++) {
ret[i] = m_buffer[i];
}
return ret;
}
}
}
通過代碼調(diào)試,發(fā)現(xiàn)問題出現(xiàn)在inputsream.read方法上,java API對其描述如下:
int java. io. BufferedInputStream.read( byte[] buffer, int offset, int byteCount) throws IOException
Reads at most byteCount bytes from this stream and stores them in byte array buffer starting at offset offset. Returns the number of bytes actually read or -1 if no bytes were read and the end of the stream was encountered. If all the buffered bytes have been used, a mark has not been set and the requested number of bytes is larger than the receiver's buffer size, this implementation bypasses the buffer and simply places the results directly into buffer.
Overrides: read(...) in FilterInputStream
Parameters:
buffer the byte array in which to store the bytes read.
offset the initial position in buffer to store the bytes read from this stream.
byteCount the maximum number of bytes to store in buffer.
Returns:
the number of bytes actually read or -1 if end of stream.
Throws:
IndexOutOfBoundsException - if offset < 0 or byteCount < 0, or if offset + byteCount is greater than the size of buffer.
IOException - if the stream is already closed or another IOException occurs.
引起錯誤原因在于
客戶端在發(fā)送數(shù)據(jù)后,過快地執(zhí)行read操作,而這時服務(wù)端尚未完全返回全部內(nèi)容,因此只能讀到部分字節(jié)。于是換了個思路:
public class SocketClient {
private Socket m_socket;
private InputStream m_inputstream;
private OutputStream m_outputstream;
private BufferedInputStream m_bufferedinputstream;
private BufferedOutputStream m_bufferedoutputstream;
private boolean connected;
public int connect(String host, int port) {
try {
SocketAddress socketAddress = new InetSocketAddress(host, port);
m_socket = new Socket();
m_socket.connect(socketAddress, 5000);
m_socket.setSoTimeout(60000);
m_inputstream = m_socket.getInputStream();
m_bufferedinputstream = new BufferedInputStream(m_inputstream);
m_outputstream = m_socket.getOutputStream();
m_bufferedoutputstream = new BufferedOutputStream(m_outputstream);
} catch (Exception e) {
return -1;
}
connected = true;
return 0;
}
/**
* 發(fā)送請求數(shù)據(jù)
*
* @param data
* @param start
* @param end
* @return
*/
public int write(byte data[]) {
if (data == null || data.length == 0 || !connected) {
return 0;
}
try {
m_bufferedoutputstream.write(data, 0, data.length);
m_bufferedoutputstream.flush();
} catch (Exception e) {
return -1;
}
return 0;
}
/**
* 讀取返回數(shù)據(jù)
*
* @return
*/
public byte[] read() {
if (!connected) {
return null;
}
try {
return readStream(m_bufferedinputstream);
} catch (Exception e) {
return null;
}
}
/**
* @功能 讀取流
* @param inStream
* @return 字節(jié)數(shù)組
* @throws Exception
*/
public static byte[] readStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = inStream.read(buffer)) != -1) {
outSteam.write(buffer, 0, len);
}
outSteam.close();
inStream.close();
return outSteam.toByteArray();
}
public static void test() {
SocketClient client = new SocketClient();
// 建立socket對象
int iret = client.connect("192.168.1.105", 1234);
if (iret == 0) {
// 發(fā)送數(shù)據(jù)
client.write("helloworld".getBytes());
// 接收數(shù)據(jù)
byte data[] = client.read();
if ((data != null) && (data.length != 0)) {
// 處理接收結(jié)果
Utils.print("響應(yīng)報文字節(jié)數(shù)組---->" + Arrays.toString(data));
}
}
}
}
測試通過.....
可參考以下解決思路
protected byte[] readMessage(BufferedInputStream is) throws IOException {
// MyLog.d(TAG,"=======>readMessage--inputStream=" );
int offset = 0;
int messageStartOffset = -1;
int wait = 0;
int messageEndOffset = -1;
int findStartOffset = -1;
while(messageEndOffset==-1||(messageEndOffset+2)>offset){
if(is.available()==0){
try {
Thread.sleep(MESSAGE_WAIT_INTERVAL);
wait += MESSAGE_WAIT_INTERVAL;
} catch (InterruptedException ex) {
}
if(wait>=MESSAGE_OVERTIME){
//超時錯誤
throw new RuntimeException(EXCEPTION_TIMEOUT);
}
continue;
}
offset += is.read(messageBuffer, offset, is.available());//讀出數(shù)據(jù)
TestMessage.showBytes(messageBuffer, 0, offset, "MESSAGE");
if(messageStartOffset==-1){ //未找到報文頭
if(findStartOffset<0)
findStartOffset = 0;
messageStartOffset = findStartOffset(messageBuffer, findStartOffset, offset);//查找報文頭
MyLog.e(TAG, "messageStartOffset="+messageStartOffset);
if(messageStartOffset>=0){//找到報文頭
if(messageStartOffset<2){
//報文錯誤
throw new RuntimeException(EXCEPTION_MSG_PARSE_ERROR);
}else{
int iMessageLength = ((messageBuffer[messageStartOffset-2]&0xff)<<8)+
(messageBuffer[messageStartOffset-1]&0xff);
// MyLog.e(TAG, "iMessageLength="+iMessageLength);
int ignoreInvalidLength = messageStartOffset-4;
messageEndOffset = iMessageLength + ignoreInvalidLength;
// MyLog.e(TAG, "messageStartOffset="+messageStartOffset);
MyLog.e(TAG, "messageEndOffset="+messageEndOffset);
如果想要讓程序保證讀取到count個字節(jié),最好用以下代碼:
int count = 100;
byte[] b = new byte[count];
int readCount = 0; // 已經(jīng)成功讀取的字節(jié)的個數(shù)
while (readCount < count) {
readCount += inStream.read(b, readCount, count - readCount);
}
這樣就能保證讀取100個字節(jié),除非中途遇到IO異?;蛘叩搅藬?shù)據(jù)流的結(jié)尾情況!
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中的權(quán)重算法(如Dubbo的負(fù)載均衡權(quán)重)詳解
這篇文章主要介紹了Java中的權(quán)重算法(如Dubbo的負(fù)載均衡權(quán)重)詳解,負(fù)載均衡,其含義就是指將負(fù)載進(jìn)行平衡、分?jǐn)偟蕉鄠€操作單元上進(jìn)行運行,例如FTP服務(wù)器、Web服務(wù)器、企業(yè)核心應(yīng)用服務(wù)器和其它主要任務(wù)服務(wù)器等,從而協(xié)同完成工作任務(wù),需要的朋友可以參考下2023-08-08
Spring Boot+Nginx實現(xiàn)大文件下載功能
相信很多小伙伴,在日常開放中都會遇到大文件下載的情況,大文件下載方式也有很多,比如非常流行的分片下載、斷點下載;當(dāng)然也可以結(jié)合Nginx來實現(xiàn)大文件下載,在中小項目非常適合使用,這篇文章主要介紹了Spring Boot結(jié)合Nginx實現(xiàn)大文件下載,需要的朋友可以參考下2024-05-05
解決MyBatis Mapper的XML文件SQL語句無法自動提示問題(親測有效)
這篇文章主要給大家介紹了如何解決MyBatis Mapper的XML文件SQL語句無法自動提示的問題,文中有詳細(xì)的原因分析,以及通過圖文介紹的解決方案,需要的朋友可以參考下2023-10-10
SpringBoot+RabbitMQ實現(xiàn)消息可靠傳輸詳解
消息的可靠傳輸是面試必問的問題之一,保證消息的可靠傳輸主要在生產(chǎn)端開啟?comfirm?模式,RabbitMQ?開啟持久化,消費端關(guān)閉自動?ack?模式。本文將詳解SpringBoot整合RabbitMQ如何實現(xiàn)消息可靠傳輸,需要的可以參考一下2022-05-05
java基礎(chǔ)學(xué)習(xí)JVM中GC的算法
這篇文章主要介紹了java基礎(chǔ)學(xué)習(xí)JVM中GC的算法,通過圖文加深對GC算法思路的理解。2017-11-11

