Client/Server in java : Program gets stuck upon input of second file name -
Client/Server in java : Program gets stuck upon input of second file name -
i developing code send multiple file names server side , making sure server recieves contents , writes file in own folder. working when type first file name when type sec file name code kind of gets stuck.
here client code:-
package filetransfer; import java.io.bufferedreader; import java.io.file; import java.io.fileinputstream; import java.io.filereader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printstream; import java.io.printwriter; import java.net.socket; import java.net.unknownhostexception; public class echoclient { public static void main(string[] args) throws unknownhostexception { seek { socket clientsock=new socket("localhost",8888); //to read server bufferedreader br=new bufferedreader(new inputstreamreader(clientsock.getinputstream())); //to read //to write server printwriter pw=new printwriter(clientsock.getoutputstream(), true); //for user input bufferedreader userin=new bufferedreader(new inputstreamreader(system.in)); bufferedreader filecontent=null; string str=null; string filecontentline=null; while(true){ if((str=br.readline()).contains("file name")) //recieve echo server system.out.println(str); str=userin.readline(); //read user input filecontent=new bufferedreader(new filereader(str)); pw.println(str); while((filecontentline=filecontent.readline()) != null){ pw.println(filecontentline); } while((str=br.readline())!=null) system.out.println(str); pw.flush(); } }catch(ioexception e){ e.printstacktrace(); } }
}
and here server code
package filetransfer; import java.io.bufferedinputstream; import java.io.bufferedreader; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstreamreader; import java.io.outputstreamwriter; import java.io.printwriter; import java.net.serversocket; import java.net.socket; public class echoserver { public static void main(string[] args){ seek { serversocket serversock=new serversocket(8888); system.out.println("waiting client"); socket connectfromclient=serversock.accept(); file file=null; //reading info client bufferedreader input=new bufferedreader(new inputstreamreader(connectfromclient.getinputstream())); //will write client printwriter pr=new printwriter(new outputstreamwriter(connectfromclient.getoutputstream())); printwriter writetofile=null; //sending next statements client pr.println("connection established server! give file name"); pr.flush(); string response; while(true){ while((response=input.readline()) != null) { if(response==null) break; else{ system.out.println(response); if(response.contains(".txt")){ //file=new file("fromclient.txt"); file=new file("fromclient"+response); if(!file.exists()) file.createnewfile(); writetofile=new printwriter(file); } else{ //writetofile=new printwriter(file); writetofile.println(response); } pr.println("echo server -> " + response); //system.out.println("adding these contents file"); writetofile.flush(); pr.flush(); } } } } grab (ioexception e) { e.printstacktrace(); } }
}
it´s getting stuck in client in lines:
while ((str = br.readline()) != null) { system.out.println(str); }
waiting more server lines.
you have tell client when stop reading file info socket stream , jump next file. instance can send before content of file number of bytes going transfer, read number of bytes stream , go next file.
i post modifications of code example, thought want express. first total linenumber of file (i suppose text file), , send server, knows how many lines read before jumping next file. can changed utilize total size of file in bytes, instance. code no 100% correct, works, take example.
client:
import java.io.bufferedreader; import java.io.filereader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.socket; import java.net.unknownhostexception; public class echoclient { public static void main(final string[] args) throws unknownhostexception { seek { final socket clientsock = new socket("localhost", 8888); // read server final bufferedreader br = new bufferedreader(new inputstreamreader(clientsock.getinputstream())); // // read // write server final printwriter pw = new printwriter(clientsock.getoutputstream(), true); // user input final bufferedreader userin = new bufferedreader(new inputstreamreader(system.in)); bufferedreader filecontent = null; string str = null; string filecontentline = null; while (true) { system.out.println("print filename"); str = userin.readline(); // read user input filecontent = new bufferedreader(new filereader(str)); pw.println(str); // first count line number: int lineno = 0; while ((filecontentline = filecontent.readline()) != null) { lineno++; } filecontent.close(); // filecontent = new bufferedreader(new filereader(str)); pw.println(string.valueof(lineno)); while ((filecontentline = filecontent.readline()) != null) { pw.println(filecontentline); } (int = 0; < lineno; i++) { str = br.readline(); system.out.println(str); } pw.flush(); } } grab (final ioexception e) { e.printstacktrace(); } } }
server:
import java.io.bufferedreader; import java.io.file; import java.io.ioexception; import java.io.inputstreamreader; import java.io.outputstreamwriter; import java.io.printwriter; import java.net.serversocket; import java.net.socket; public class echoserver { public static void main(final string[] args) { seek { final serversocket serversock = new serversocket(8888); system.out.println("waiting client"); final socket connectfromclient = serversock.accept(); file file = null; // reading info client final bufferedreader input = new bufferedreader(new inputstreamreader(connectfromclient.getinputstream())); // write client final printwriter pr = new printwriter(new outputstreamwriter(connectfromclient.getoutputstream())); printwriter writetofile = null; // sending next statements client pr.println("connection established server! give file name"); pr.flush(); string response; while (true) { final string filename = input.readline(); file = new file("fromclient" + filename); if (!file.exists()) { file.createnewfile(); } writetofile = new printwriter(file); final string slineno = input.readline(); final int lineno = integer.parseint(slineno); (int = 0; < lineno; i++) { response = input.readline(); system.out.println(response); // writetofile=new printwriter(file); writetofile.println(response); pr.println("echo server -> " + response); // system.out.println("adding these contents file"); writetofile.flush(); pr.flush(); } writetofile.close(); } } grab (final ioexception e) { e.printstacktrace(); } } }
i hope helpful.
java file client-server
Comments
Post a Comment