Transfer of image over network via TCP Socket in JAVA isn't working? -
i have following code. seems streaming content accurately. however, image not being captured correctly. new java. basically, c, c++, linux programmer. wondering problem reading buffer line line. missing here?
here socket server code -
import java.io.*; import java.net.*; public class imagesocketserver { public static void main(string args[]) throws ioexception { imagesocketserver imageserver = new imagesocketserver(); imageserver.run(); } private void run() throws ioexception { // todo auto-generated method stub serversocket serversock = new serversocket(1025); socket sock = serversock.accept(); inputstream imagetoshare = new bufferedinputstream(new fileinputstream("/export/home/joshis1/lizard.png")); printstream imagesend = new printstream( sock.getoutputstream()); imagesend.print(imagetoshare); } }
here socket client code -
import java.io.*; import java.net.*; public class imagesocketclient { public static void main(string args[]) throws ioexception { imagesocketclient imageclient = new imagesocketclient(); imagesocketclient.run(); } private static void run() throws unknownhostexception, ioexception { // todo auto-generated method stub bufferedwriter bufwriter = null; bufwriter = new bufferedwriter(new filewriter( "/export/home/joshis1/file1.png")); socket sock = new socket("localhost", 1025); inputstreamreader ir = new inputstreamreader(sock.getinputstream()); bufferedreader br = new bufferedreader(ir); string data; while ((data = br.readline()) != null) { system.out.println("shreyas got data"); bufwriter.write(data); } bufwriter.close(); } }
i see source image of size -
$ ls -l lizard.png -rw-rw-r-- 1 joshis1 joshis1 19071522 may 29 15:46 lizard.png , destination image wrongly copied - $ ls -l file1.png -rw-rw-r-- 1 joshis1 joshis1 34 may 29 17:38 file1.png
first of imagesend.print(imagetoshare);
sends on string representation of inputstream
, explains small content of file. you'll want create loop reads imagetoshare
(although might want name better, it's not image, it's stream) , writes data outputstream (search around quintessential read-write loop).
secondly, you're using printstream
used write character data outputstream
. want use bufferedoutputstream
that.
Comments
Post a Comment