Android的套接字读取方法返回-1 [英] Android socket read method return -1
问题描述
我在项目中使用王菲客户,并曾与插座一个奇怪的问题。
I used faye client in my project and had a strange problem with sockets.
在这个项目中,我们使用 SocketChannel.html#读取(java.nio.ByteBuffer中)
方法从的WebSocket
和有 1
FO有些道理的。
In this project we used SocketChannel.html#read(java.nio.ByteBuffer)
method for reading from WebSocket
and have
-1
fo some reason.
我检查socket对象,看到 isInputShutdown = FALSE
, isClosed返= FALSE
, isCreated = TRUE
。
I checked socket object and saw that isInputShutdown = false
, isClosed = false
, isCreated = true
.
code片断:
System.setProperty("java.net.preferIPv6Addresses", "false");
mTransportChannel = SocketChannel.open();
InetSocketAddress socketAdd = new InetSocketAddress("172.20.71.4", 9292);
boolean resolve = socketAdd.isUnresolved();
mTransportChannel.socket().connect(socketAdd, 6000);
Log.i("TEST", "Socket connected");
mTransportChannel.socket().setSoTimeout(6000);
mTransportChannel.socket().setTcpNoDelay(true);
mBuffer = new ByteBufferOutputStream(1000 + 14, 4 * 64 * 1024);
WebSocketMessage.ClientHandshake hs = new WebSocketMessage.ClientHandshake(
"172.20.71.4" + ":" + "9292");
hs.mPath = "/faye/services/chat-123456789-123456";
hs.mQuery = null;
hs.mSubprotocols = null;
sendClientHandshake(hs);
int written = mTransportChannel.write(mBuffer.getBuffer());
mFrameBuffer = ByteBuffer.allocateDirect(1000 + 14);
int len = mTransportChannel.read(mFrameBuffer);
Log.d("tset", Integer.toString(len));
/**
* Send WebSocket client handshake.
*/
private void sendClientHandshake(WebSocketMessage.ClientHandshake message) throws IOException {
// write HTTP header with handshake
String path;
if (message.mQuery != null) {
path = message.mPath + "?" + message.mQuery;
} else {
path = message.mPath;
}
mBuffer.write("GET " + path + " HTTP/1.1");
mBuffer.crlf();
mBuffer.write("Host: " + message.mHost);
mBuffer.crlf();
mBuffer.write("Upgrade: WebSocket");
mBuffer.crlf();
mBuffer.write("Connection: Upgrade");
mBuffer.crlf();
mBuffer.write("Sec-WebSocket-Key: " + newHandshakeKey());
mBuffer.crlf();
if (message.mOrigin != null && !message.mOrigin.equals("")) {
mBuffer.write("Origin: " + message.mOrigin);
mBuffer.crlf();
}
if (message.mSubprotocols != null && message.mSubprotocols.length > 0) {
mBuffer.write("Sec-WebSocket-Protocol: ");
for (int i = 0; i < message.mSubprotocols.length; ++i) {
mBuffer.write(message.mSubprotocols[i]);
mBuffer.write(", ");
}
mBuffer.crlf();
}
mBuffer.write("Sec-WebSocket-Version: 13");
mBuffer.crlf();
mBuffer.crlf();
}
感谢。
推荐答案
在服务器关闭连接它发生。在这种情况下,不会抛出异常,并且错误只能由返回值被检查 - 负值表示错误(丢失连接,也许别的东西)。下面是一个<一个href=\"https://github.com/tavendo/AutobahnAndroid/blob/master/Autobahn/src/de/tavendo/autobahn/WebSocketReader.java#L656\"相对=nofollow>在AutobahnAndroid这样一个测试的例子。
It happens when server closes the connection. In this case, no exception is thrown, and the error can be checked only by the return value - a negative value indicates an error (lost connection, perhaps something else). Here is an example of such a test in AutobahnAndroid.
这篇关于Android的套接字读取方法返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!