使用LocalServerSocket

使用LocalServerSocket

是否有人对异常“重试”有更多有用的信息?

我正在使用LocalServerSocketLocalSocket在应用之间发送位图:

输出:

socket = new LocalSocket();
socket.connect(new LocalSocketAddress(SOCKET_NAME));
fos = new DataOutputStream(socket.getOutputStream());
...

public void onEvent() {
    fos.writeInt(width);
    fos.writeInt(height);
    fos.writeInt(newBuffer.length);
    fos.write(newBuffer);
}

输入:
server = new LocalServerSocket(SOCKET_NAME);
socket = server.accept();
socket.setSoTimeout(60);

while(true) {

    int width = fis.readInt(); // IO Exception being thrown here
    int height = fis.readInt();
    int length = fis.readInt();
    byte[] bytes = new byte[length];
    fis.read(bytes);
}

[为清晰起见,删除了尝试/捕获等内容]
04-18 09:19:11.664: W/System.err(1268): java.io.IOException: Try again
04-18 09:19:11.664: W/System.err(1268):     at android.net.LocalSocketImpl.readba_native(Native Method)
04-18 09:19:11.664: W/System.err(1268):     at android.net.LocalSocketImpl.access$400(LocalSocketImpl.java:29)
04-18 09:19:11.664: W/System.err(1268):     at android.net.LocalSocketImpl$SocketInputStream.read(LocalSocketImpl.java:92)
04-18 09:19:11.664: W/System.err(1268):     at libcore.io.Streams.readFully(Streams.java:81)
04-18 09:19:11.664: W/System.err(1268):     at java.io.DataInputStream.readInt(DataInputStream.java:124)
04-18 09:19:11.664: W/System.err(1268):     at com.test.util.BitmapSendingUtils$BitmapReceiver$1.run(BitmapSendingUtils.java:105)

最佳答案

您看到的异常可能是与EAGAIN错误等效的Java。参见例如this answer

您应该处理该异常,然后再次尝试失败的IO操作。

10-05 23:57