0

我正在尝试在两部安卓手机(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);
4

1 回答 1

0

你正在用 写作writeUTF(),所以你应该用 阅读readUTF()

于 2013-06-07T01:46:07.890 回答