java - Object Input Stream only getting one file over network? -
i have created basic client server send image files in specified directory on network. code worked last week came today , seems getting 1 file on server side, though client prints out has sent image files in directory. may in client code think on server side. appreciated , if have more efficient solution, happy change code necessary. code below:
imageserver
package com.encima.network.server; import java.io.*; import java.net.*; public class imageserver{ serversocket ss; socket s; objectoutputstream oos; int port = 4440; public imageserver() throws ioexception { try { ss = new serversocket(port); system.out.println("server started on port: " + port); } catch(ioexception e) { system.out.println("serevr: port-" + port + " not available, exiting."); system.exit(0); } system.out.println("server: waiting client connection..."); while(true) { try { s = ss.accept(); new imagehandler(s); } catch (ioexception e) { e.printstacktrace(); } } } public static void main(string[] args) throws ioexception { imageserver = new imageserver(); } } imagehandler package com.encima.network.server; import java.awt.image.bufferedimage; import java.io.fileoutputstream; import java.io.objectinputstream; import java.net.socket; import javax.imageio.imageio; public class imagehandler implements runnable { socket s; int count = 0; public imagehandler(socket socket) { s = socket; thread t = new thread(this); t.start(); } @override public void run() { try { objectinputstream ois = new objectinputstream(s.getinputstream()); fileoutputstream fos = new fileoutputstream("image" + system.nanotime() + ".jpg"); count++; //bufferedimage in = imageio.read(ois); //imageio.write(in, "jpg", fos); int ch = 0; while(true) { ch = ois.read(); if(ch == -1) { break; } fos.write(ch); } fos.flush(); } catch (exception e) { e.printstacktrace(); } } } finally, imageclient package com.encima.network.client; import java.awt.image.bufferedimage; import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.io.objectoutputstream; import java.net.socket; import javax.imageio.imageio; import com.encima.network.imagefilter; public class imageclient { socket s; string ip = "localhost"; int port = 4440; objectoutputstream oos; public imageclient(file[] files) throws ioexception, classnotfoundexception, interruptedexception { try { s = new socket(ip, port); system.out.println("client connected server via " + ip + " on port 80"); } catch (exception e) { system.out.println("client: cannot find host: " + ip + ". exiting."); system.exit(0); } oos = new objectoutputstream(s.getoutputstream()); for(file f: files) { sendfile(f); } oos.close(); //system.out.println("written image " + + " of " + files.length); } public static void main(string[] args) throws ioexception, classnotfoundexception, interruptedexception { file dir = new file("/users/christophergwilliams/dropbox/phd/projects/phd/year 1/gsn/images"); file[] files = dir.listfiles(new imagefilter()); imageclient ic = new imageclient(files); } public void sendfile(file file) throws ioexception { fileinputstream fis = new fileinputstream(file); //bufferedimage b = imageio.read(file); //imageio.write(b, "jpg", oos); int ch = 0; while(true) { ch = fis.read(); if(ch == -1) { break; } oos.write(ch); } oos.flush(); system.out.println("image sent"); } }
i aware lot of code read through appreciate can on this!
i may wrong but, sake of efficiency , network traffic, beneficial send images zip client server?
why using objectinputstream
@ all? you're not reading or writing serialized objects - raw binary data. use whatever inputstream
provided, , read that.
anyway, that's not big problem. big problem you're writing several files 1 stream, no indication of 1 file meant finish , next 1 meant start. how expecting split multiple files up? options:
- use delimiter between files (very ugly - you'd have potentially escape data looked delimiter went along)
- prefix each file length
- send each file on different connection
(you're reading , writing single byte @ time. use overloads of read/write accept byte arrays.)
Comments
Post a Comment