I just send parts of the image through socket using Java -
i used code here below , got half of picture sent, tell me reason? code size , read size complex me couldn't figure out.
public class send { public static void main(string[] args) throws exception { socket socket = new socket("localhost", 13085); outputstream outputstream = socket.getoutputstream(); bufferedimage image = imageio.read(new file("c:\\users\\jakub\\pictures\\test.jpg")); bytearrayoutputstream bytearrayoutputstream = new bytearrayoutputstream(); imageio.write(image, "jpg", bytearrayoutputstream); byte[] size = bytebuffer.allocate(4).putint(bytearrayoutputstream.size()).array(); outputstream.write(size); outputstream.write(bytearrayoutputstream.tobytearray()); outputstream.flush(); system.out.println("flushed: " + system.currenttimemillis()); thread.sleep(120000); system.out.println("closing: " + system.currenttimemillis()); socket.close(); } } public class receive { public static void main(string[] args) throws exception { serversocket serversocket = new serversocket(13085); socket socket = serversocket.accept(); inputstream inputstream = socket.getinputstream(); system.out.println("reading: " + system.currenttimemillis()); byte[] sizear = new byte[4]; inputstream.read(sizear); int size = bytebuffer.wrap(sizear).asintbuffer().get(); byte[] imagear = new byte[size]; inputstream.read(imagear); bufferedimage image = imageio.read(new bytearrayinputstream(imagear)); system.out.println("received " + image.getheight() + "x" + image.getwidth() + ": " + system.currenttimemillis()); imageio.write(image, "jpg", new file("c:\\users\\jakub\\pictures\\test2.jpg")); serversocket.close(); } } as @ejp commented,the possible problem in receive part,the inputstream.read(byte[] bytearray) can not load bytearray. , if use datainputstream dis=new datainputstream(inputstream) call dis.readfully(bytearray) load bytearray. though couldn't figure out reason why inputstream won't work. 1 show reason, appreciated. corrected code follow:
public class send { public static void main(string[] args) throws exception { socket socket = new socket("localhost", 13085); outputstream outputstream = socket.getoutputstream(); bufferedimage image = imageio.read(new file("test.jpg")); bytearrayoutputstream bytearrayoutputstream = new bytearrayoutputstream(); imageio.write(image, "jpg", bytearrayoutputstream); byte[] size = bytebuffer.allocate(4).putint(bytearrayoutputstream.size()).array(); outputstream.write(size); outputstream.write(bytearrayoutputstream.tobytearray()); outputstream.flush(); system.out.println("flushed: " + system.currenttimemillis()); thread.sleep(120000); system.out.println("closing: " + system.currenttimemillis()); socket.close(); } } public class receive { public static void main(string[] args) throws exception { serversocket serversocket = new serversocket(13085); socket socket = serversocket.accept(); inputstream inputstream = socket.getinputstream(); datainputstream datainputstream=new datainputstream(inputstream); system.out.println("reading: " + system.currenttimemillis()); byte[] sizear = new byte[4]; datainputstream.read(sizear); int size = bytebuffer.wrap(sizear).getint(); byte[] imagear = new byte[size]; datainputstream.readfully(imagear); bufferedimage image = imageio.read(new bytearrayinputstream(imagear)); system.out.println("received " + image.getheight() + "x" + image.getwidth() + ": " + system.currenttimemillis()); imageio.write(image, "jpg", new file("received.jpg")); serversocket.close(); } }
get rid of byte array streams , imageio direct socket streams.
the problem assuming read() fills buffer, if above rid of read() anyway.
sending length pointless you're closing them socket afterwards anyway. end of stream sufficient delimit image.
get rid of sleep well. don't need sleeps in network code.
Comments
Post a Comment