objectinputstream - Java streams with EOFException -
i wrote client - server program, shares data @ server side got eofexception
after reciving data. tried fix on own hard find own errors.
the error caused line: message command =(message) serinputstream.readobject();
here output server:
java.io.eofexception @ java.io.objectinputstream$blockdatainputstream.peekbyte(objectinputstream.java:2577) @ java.io.objectinputstream.readobject0(objectinputstream.java:1315) @ java.io.objectinputstream.readobject(objectinputstream.java:369) @ transfer.serwerus.run(serwerus.java:42)
server code:
import java.io.*; import java.net.*; public class serwerus implements runnable { public inputstream is; public fileoutputstream fos; public bufferedoutputstream bos; public objectoutputstream seroutputstream; public objectinputstream serinputstream; serversocket socket; private string clientmessage, clientfilename; private int clientfilesize; public serwerus() { try { socket = new serversocket(6060); system.out.println("server started...."); } catch(ioexception e) { system.err.println("error: " + e); e.printstacktrace(); } } @override public void run() { try { socket sock = socket.accept(); system.out.println("client accepted"); seroutputstream = new objectoutputstream(sock.getoutputstream()); serinputstream = new objectinputstream(sock.getinputstream()); while (true) { message command =(message) serinputstream.readobject(); system.out.println("after readobject"); if (command.getcommand().startswith("fileu")) { system.out.println("name = " + command.getfilename() + ", size= " + command.getfilesize()); seroutputstream.writeobject(new message("bring", "", 0)); //recivedata(socket, command.getfilename(), command.getfilesize()); } else if(command.getcommand().startswith("wait")) { system.out.println("hohoho"); recivedata(sock, command.getfilename(), command.getfilesize()); } else if(command.getcommand().startswith("quit")) { system.exit(1); } else { system.out.println("unknow"); } } } catch(classnotfoundexception | ioexception ex) { ex.printstacktrace(); } { try { serinputstream.close(); seroutputstream.close(); socket.close(); } catch (ioexception e) { system.err.println("fallen on closing socket.\n error: " + e); } } } public void senddata(socket sock, string filepath) throws exception { file myfile = new file (filepath); system.out.println("file name = " + myfile.getname() + " file len = " + (int)myfile.length()); byte [] mybytearray = new byte [(int)myfile.length()]; fileinputstream fis = new fileinputstream(myfile); bufferedinputstream bis = new bufferedinputstream(fis); bis.read(mybytearray,0,mybytearray.length); outputstream os = sock.getoutputstream(); system.out.println("sending..."); os.write(mybytearray,0,mybytearray.length); os.flush(); sock.close(); system.out.println("sending finished"); } public void recivedata(socket sock, string filepath, int filesize) { system.out.println("recive in progress, filesize = " + filesize); int bytesread = 0, current = 0; byte[] array = new byte[filesize]; try { = sock.getinputstream(); fileoutputstream fos = new fileoutputstream(filepath); bos = new bufferedoutputstream(fos); { system.out.println(bytesread); bytesread = is.read(array); current += bytesread; } while(bytesread > -1); bos.write(array, 0 , current); bos.flush(); bos.close(); fos.close(); // sock.close(); system.out.println("reciveing finished"); } catch (ioexception ex) { ex.printstacktrace(); } } public static void main (string [] args ) throws ioexception { new thread(new serwerus()).start(); } }
client code:
import java.io.*; import java.net.*; public class clientus implements runnable { inputstream is; fileoutputstream fos; bufferedoutputstream bos; objectoutputstream clioutputstream; objectinputstream cliinputstream; socket socket; file actfile; private string servermessage, serverfilename; private int serverfilesize; public clientus() { try { socket = new socket("localhost", 6060); clioutputstream = new objectoutputstream(socket.getoutputstream()); cliinputstream = new objectinputstream(socket.getinputstream()); file file = new file(<filepath>); actfile = file; } catch (exception e) { system.err.println("error: " + e); e.printstacktrace(); } } @override public void run() { try { clioutputstream.writeobject(new message("fileu", actfile.getpath(), (int) actfile.length())); clioutputstream.flush(); //while (true) //{ message command =(message) cliinputstream.readobject(); if (command.getcommand().startswith("file")) { string name = command.getfilename(); int size = command.getfilesize(); system.out.println("name = " + command.getfilename() + ", size= " + command.getfilesize()); if(size != 0 && !"".equals(name)) { //recivedata(socket, 0); } } else if(command.getcommand().startswith("bring")) { clioutputstream.writeobject(new message("wait", "d:\\kp2\\serwer\\dupa.txt",(int) actfile.length())); clioutputstream.flush(); try { senddata(socket, actfile.getpath()); //this.socket.close(); } catch (exception ex) { system.err.println("error with: senddata()"); } } else if(command.getcommand().startswith("quit")) { system.exit(1); } else { system.out.println("command unknown"); } //} } catch(classnotfoundexception | ioexception ex) { ex.printstacktrace(); } { try { socket.close(); clioutputstream.close(); cliinputstream.close(); } catch (ioexception e) { system.err.println("fallen on closing socket.\n error: " + e); } } } public void senddata(socket sock, string filepath) throws exception { byte [] mybytearray = new byte [(int) new file(filepath).length()]; fileinputstream fis = new fileinputstream(filepath); bufferedinputstream bis = new bufferedinputstream(fis); bis.read(mybytearray,0,mybytearray.length); outputstream os = sock.getoutputstream(); system.out.println("sending..."); os.write(mybytearray,0,mybytearray.length); fis.close(); bis.close(); os.close(); system.out.println("sending finished"); } public void recivedata(socket sock, string filename, int filesize) { int bytesread, current = 0; byte[] array = new byte[filesize+1]; try { = sock.getinputstream(); fos = new fileoutputstream(<file_path>); bos = new bufferedoutputstream(fos); bytesread = is.read(array,0,array.length); current = bytesread; { bytesread = is.read(array, current, (array.length - current)); if(bytesread >= 0) current += bytesread; } while(bytesread > -1); bos.write(array, 0 , current); bos.flush(); long end = system.currenttimemillis(); //system.out.println("send time: " + (end - start)); bos.close(); sock.close(); system.out.println("reciveing finished"); } catch (ioexception ex) { ex.printstacktrace(); } } public static void main (string [] args ) throws ioexception { new thread(new clientus()).start(); } }
can help?
you using both objectoutputstream
, socket's own outputstream
send data on, , getting out of sync. when send raw data directly on socket aren't sending length first, receiver doesn't know how many bytes belong transmission. in fact reads until eos, next time call objectinputstream.readobject()
naturally gets eofexception. fix this:
- use
objectinputstream
,objectoutputstream
everything. - before sending file, send length, via
writelong().
- at receiver, when receiving file, first length, via
readlong(),
read many bytesobjectinputstream
, copy them file.
Comments
Post a Comment