java - Client's BufferedInputStream.read() while loop never stops -
i edited code
i'm making file transfer programme java
i have send 21 files.
my code stops @ client's while loop in run()
(it doesn't print "file receive complete") <- see run()
in client
server's code
class sendfilethread extends thread { private serversocket filetransferserversocket; private socket filetransfersocket; private bufferedreader requestreader; private printwriter requestanswerer; private bufferedoutputstream filewriter; private int filetransferport = 12345; public sendfilethread() { seek { filetransferserversocket = new serversocket(filetransferport); filetransfersocket = filetransferserversocket.accept(); requestreader = new bufferedreader(new inputstreamreader(filetransfersocket.getinputstream())); filewriter = new bufferedoutputstream(filetransfersocket.getoutputstream()); } grab (ioexception e) { e.printstacktrace(); } } public void closetransferstream() { seek { requestanswerer.close(); requestreader.close(); filewriter.close(); filetransfersocket.close(); filetransferserversocket.close(); } grab (ioexception ioe) { ioe.printstacktrace(); } } public void sendfile(string filename) { seek { file file = new file(client_path + "/" + filename); bufferedinputstream filereader = new bufferedinputstream(new fileinputstream(file)); int packet; while((packet = filereader.read()) != -1) filewriter.write(packet); filewriter.flush(); filereader.close(); } grab (ioexception ioe) { system.out.println(ioe.getmessage()); } //system.out.print(filename + " send finish (" + count + " times)"); } public void listenforrequester() { seek { string input; while((input = requestreader.readline()) != null) { if(input.equals("request file")) { sendfile(requestreader.readline()); } else if(input.equals("end transfer")) break; else { system.out.println("something wrong"); } } } catch(ioexception ioe) { ioe.getstacktrace(); } } public void run() { listenforrequester(); closetransferstream(); } }
client's code
class receivefilethread extends thread { private socket filetransfersocket; private int filetransferport = 12345; private bufferedinputstream filereader; private printwriter filerequester; public receivefilethread() { seek { filetransfersocket = new socket(serverip, filetransferport); filerequester = new printwriter(filetransfersocket.getoutputstream(), true); filereader = new bufferedinputstream(filetransfersocket.getinputstream()); } grab (unknownhostexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } } public void closetransferstream() { seek { filerequester.close(); filereader.close(); filetransfersocket.close(); } grab (ioexception e) { e.printstacktrace(); } } public synchronized void requestfile(string filename) { filerequester.println("request file"); filerequester.println(filename); } public synchronized void sendendmsg() { filerequester.println("end transfer"); } public void run() { for(int = 0;i < filelist.size();i++) { string filename = (string)filelist.get(i); requestfile(filename); seek { bufferedoutputstream filewriter = new bufferedoutputstream(new fileoutputstream(new file(program_path + "/" + filename))); int packet = 0; while((packet = filereader.read()) > -1) filewriter.write(packet); system.out.println("file receive complete"); filewriter.close(); } grab (filenotfoundexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } } sendendmsg(); closetransferstream(); } }
it became 5 days error bothers me :(
could save me error?
closing socket's output stream close socket, in order send multiple files have create couple of changes.
server: before start send file, send file's length.
client: after receive file's length start read many bytes input stream , save them file, when you're done read next file's length.
java file-io file-transfer
No comments:
Post a Comment