java - Wrong sending and receiving photos through socket -


this code of client (android application):

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 - photo 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 code server:

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())); 

practically 1/4 times when trying send photo on server , read imageio getting exception:

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()));. used debugger , revealed: practically 1/4 times sends 116k bytes, received 113k! don't understand possible. maybe have errors in code? me solve please. no ideas sorry!

you can't mix buffered , unbuffered streams on same socket. you're losing part of image in buffered reader. try using nothing datainputstream , dataoutputsream, , send filename via writeutf()/readutf().


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -