安卓中通过socket接收数据的java“连接重置”
我必须在两台安卓设备内通过WiFi发送和接收数据。 当我连接安卓服务器代码时,代码工作正常
问题是,当我从服务器向客户端发送确认时,它会在客户端发出异常。 我必须将数据从客户端发送到服务器,然后它应该将确认返回给客户端
W: java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
W: at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:592)
W: at libcore.io.IoBridge.recvfrom(IoBridge.java:556)
W: at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485)
W: at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
W: at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
W: at java.io.InputStream.read(InputStream.java:162)
W: at in.co.prosimerp.serverdemo.CleintActivity$MyClientTask.doInBackground(CleintActivity.java:103)
W: at in.co.prosimerp.serverdemo.CleintActivity$MyClientTask.doInBackground(CleintActivity.java:67)
W: at 安卓.os.AsyncTask$2.call(AsyncTask.java:292)
W: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W: at 安卓.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W: at java.lang.Thread.run(Thread.java:818)
W: Caused by: 安卓.system.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
W: at libcore.io.Posix.recvfromBytes(Native Method)
W: at libcore.io.Posix.recvfrom(Posix.java:185)
W: at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:250)
W: at libcore.io.IoBridge.recvfrom(IoBridge.java:553)
客户端代码:
socket = new Socket(dstAddress, dstPort);
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String number = "2";
String sendMessage = number + "\n";
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : "+sendMessage);
ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int bytesRead;
InputStream inputStream = socket.getInputStream();
//Exception at this point
while ((bytesRead = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}
服务器代码:
serverSocket = new ServerSocket(SocketServerPORT);
while (true) {
Socket socket = serverSocket.accept();
count++;
message += "A#" + count + " from " + socket.getInetAddress()
+ ":" + socket.getPort() + "\n";
OutputStream outputStream;
String msgReply = "Hello from Android, you are #" + count;
try {
outputStream = socket.getOutputStream();
PrintStream printStream = new PrintStream(outputStream);
printStream.print(msgReply);
printStream.close();
message += "replayed: " + msgReply + "\n";
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
msg.setText(message);
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
message += "Something wrong! " + e.toString() + "\n";
}}
# 1 楼答案
检查此代码。这个代码对我有用。nis和nos是输入和输出流
# 2 楼答案
您的客户端发送的消息从未被服务器读取。当服务器使用未读的挂起数据关闭套接字时,会导致连接重置,而不是正常关闭
# 3 楼答案
我得到了答案
使用available()是在不使用异步方法的情况下执行此操作的唯一方法