Hello. There is a client code:
Socket mSocket = new Socket(); mSocket.connect(new InetSocketAddress("123.456.789.0", 50), 10000); BufferedReader in = new BufferedReader(new InputStreamReader(mSocket.getInputStream())); OutputStream out = mSocket.getOutputStream(); out.write("image number 3".getBytes("UTF-8")); out.flush(); FileInputStream mFileInputStream = new FileInputStream(mFile); // mFile - файл с картинкой while (true) { byte[] i3 = new byte[65536]; int i4 = mFileInputStream.read(i3, 0, 65536); if (i4 < 0) { mFileInputStream.close(); break; } else { out.write(i3, 0, i4); out.flush(); } } And server code:
BufferedReader in = new BufferedReader(new InputStreamReader(this.in, "UTF-8")); String i1 = in.readLine(); ByteArrayOutputStream i3 = new ByteArrayOutputStream(); while (true) { try { byte[] i4 = new byte[1024]; int i5 = this.in.read(i4, 0, 1024); if (i5 < 0) { throw new Exception(); } else { i3.write(i4, 0, i5); i3.flush(); } } catch (Exception e1) { i3.close(); break; } } BufferedImage mBufferedImage = ImageIO.read(new ByteArrayInputStream(i3.toByteArray())); Approximately every 4th attempt to send a photo to the server and the error is read out:
java.lang.NullPointerException
at com.lnproduction.ru.gks.server.I1 $ WebClient.run (I1.java:101)
at java.lang.Thread.run (Unknown Source)
Line 101: BufferedImage mBufferedImage = ImageIO.read(new ByteArrayInputStream(i3.toByteArray())); . I tried to use the debugger, and it turned out that 116K bytes are being sent, but a little less, for example, approximately 113K bytes. I do not understand how this is possible. Can I have an error somewhere in the code? The fact is that the client is the application on the phone, and the server is the program on the computer. Help me fix it. I spent half a day over this error, my head was spinning.