我正在尝试在两个Android手机(Nexus 4)上创建服务器和客户端应用程序。一个手机作为客户端,另一个作为服务器。但是在服务器端,我在读取传入数据时遇到EOF异常错误,而在客户端,我在向Socket outputStream写入一些字节时遇到IO异常。有人可以告诉我我做错了吗?
客户端代码
// Send only one byte to server
while (!Thread.currentThread().isInterrupted()) {
try {
Log.d(TAG,"Entering into the IO exception block");
byte[] image_data = new byte[3];
String send_string = new String(image_data);
// Create a Output Stream to send the bytes back to server
OutputStream out;
out = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
Log.d(TAG,"Created data output Stream");
dos.writeUTF(send_string);
/*
dos.writeInt(image_data.length);
dos.write(image_data, 0, image_data.length);
Log.d(TAG,"Writing the ByteArray into Socket ");
*/
out.close();
dos.flush();
dos.close();
s.close();
} catch (IOException e1) {
Log.d(TAG,"Exception while writing to Socket");
e1.printStackTrace();
}
}
服务器端代码:dis.readFully(ClientByteArray)
时出现异常 if (s == null)
s = ss.accept();
Log.d(TAG,"Socket server is closed " + ss.isClosed() +", Recieved Buffer Size = " + ss.getReceiveBufferSize());
Log.d(TAG,"Accepted Client socket at server");
byte[] ClientByteArray = new byte[ss.getReceiveBufferSize()];
InputStream in;
in = s.getInputStream();
DataInputStream dis = new DataInputStream(in);
dis.readFully(ClientByteArray);
Log.d(TAG,"Data recieved from Client, Bytes = " + ClientByteArray.length);
答案 0 :(得分:0)
您正在使用writeUTF()
进行撰写,因此您应该使用readUTF()
进行阅读。