Java sending files through sockets -


i want send files other information through sockets. using following code

 public void receivefile(socket socket,int filesize,string filename) throws ioexception {     //after receiving file send ack     system.out.println("waiting ");  // int filesize=70; // filesize temporary hardcoded  long start = system.currenttimemillis(); int bytesread; int current = 0; // localhost testing system.out.println("connecting...");  // receive file byte [] mybytearray  = new byte [filesize]; inputstream = socket.getinputstream(); fileoutputstream fos = new fileoutputstream(filename); bufferedoutputstream bos = new bufferedoutputstream(fos); bytesread = is.read(mybytearray,0,mybytearray.length); current = bytesread; system.out.println("recv..."+mybytearray.length); {    bytesread =       is.read(mybytearray, current, (mybytearray.length-current));    system.out.println(bytesread);    if(bytesread > 0) current += bytesread; } while(bytesread > 0);  bos.write(mybytearray, 0 , current); bos.flush(); long end = system.currenttimemillis(); system.out.println(end-start); bos.close(); system.out.println(" file received"); } 

after receiving file, have receive other strings. when try read input stream, getting contents of file. how flush contents of file inputstream.

bufferedreader infromserver =             new bufferedreader(new inputstreamreader(                 webserversocket.getinputstream()));  receivefile(webserversocket,filesize,filename);     while(true)     {         msg = infromserver.readline(); //here receive contents of file again      system.out.println(msg);     } 

pass socket's inputstream receivefile-method, instead of socket itself:

inputstream = webserversocket.getinputstream(); bufferedreader infromserver = new bufferedreader(new inputstreamreader(is));  receivefile(webserversocket,filesize,filename); 

the problem lies, believe, in fact have 2 inputstreams same socket made @ same point in time (before data has been read). point same stream reading 1 not move other well, after reading inputstreamba 1 marked in position 15 (for example) while inpustream still in poisition 0, @ beginning of stream.

(edit:) ofcourse, have use inputstream in receivefile method instead of getting 1 socket. solution inputstream socket after call receive file, in

receivefile(webserversocket,filesize,filename);  bufferedreader infromserver =             new bufferedreader(new inputstreamreader(webserversocket.getinputstream())); 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -