gpt4 book ai didi

java.net.SocketException : sendto failed: EPIPE (Broken pipe) on Android 异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:16:20 24 4
gpt4 key购买 nike

我正在尝试从 Android 客户端的服务器套接字读取数据。

以下是我使用的代码 fragment :

客户端(在 JAVA 中的 Android 上)

    DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {

if (client.socket == null || !client.socket.isConnected())
client.createSocket();

// Get Input/Output stream for socket
dataOutputStream = new DataOutputStream(client.socket.getOutputStream());
dataInputStream = new DataInputStream(client.socket.getInputStream());

// Here RS is simple JAVA class that stores information about payload and response length
Log.d("Send", "Sending payload for pair " + RS.getc_s_pair() + " " + RS.getResponse_len());

dataOutputStream.write(UtilsManager.serialize(RS.getPayload()));

// Notify waiting Queue thread to start processing next packet
if (RS.getResponse_len() > 0) {
Log.d("Response", "Waiting for response for pair " + RS.getc_s_pair() + " of " + RS.getResponse_len() + " bytes");

int totalRead = 0;

byte[] buffer = new byte[RS.getResponse_len()];
while (totalRead < RS.getResponse_len()) {
int bytesRead = dataInputStream.read(buffer, totalRead, RS.getResponse_len() - totalRead);

if (bytesRead < 0) {
throw new IOException("Data stream ended prematurely");
}
totalRead += bytesRead;
}
}

服务器(Python)

        # Here request_len is the expected request size
while buffer_len < response_set.request_len:
new_data = connection.recv( min(self.buff_size, response_set.request_len-buffer_len) )
if not new_data:
return False
buffer_len += len(new_data)

# Send required response
for response in response_set.response_list:
try:
connection.sendall(str(response.payload))
except:
return False

有时候,当我在线上从客户端发送有效负载时,我会遇到错误

       dataOutputStream.write(UtilsManager.serialize(RS.getPayload()));

我得到的错误是:

java.net.SocketException: sendto failed: EPIPE (Broken pipe)
at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:506)
at libcore.io.IoBridge.sendto(IoBridge.java:475)
at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)
at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)
at java.io.DataOutputStream.write(DataOutputStream.java:98)
at java.io.OutputStream.write(OutputStream.java:82)
at com.rgolani.replay.tcp.TCPClientThread.run(TCPClientThread.java:56)
at java.lang.Thread.run(Thread.java:856)
Caused by: libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)
at libcore.io.Posix.sendtoBytes(Native Method)
at libcore.io.Posix.sendto(Posix.java:151)
at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
at libcore.io.IoBridge.sendto(IoBridge.java:473)
... 7 more

我知道这有点令人困惑。 Python 代码不是我写的,有一个 python 客户端可以正常工作。我只在我的 JAVA 代码中收到此错误。

工作的 Python 客户端

    if self.sock is None:
self._connect_socket()

self.sock.sendall(tcp.payload)

buffer_len = 0
while tcp.response_len > buffer_len:
data = self.sock.recv( min(self.buff_size, tcp.response_len-buffer_len) )
buffer_len += len(data)

如果您需要更多信息,请告诉我。

谢谢。

最佳答案

如果您正在使用 setChunkedStreamingMode 然后删除它然后再看。我想我来晚了但这解决了我的问题。您可以使用固定长度的流模式。

关于java.net.SocketException : sendto failed: EPIPE (Broken pipe) on Android 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23357479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com