java socket programming to send and receive file between two machines -
i trying communicate between 2 machines using socket programming.
what need both machines should able send , receive files. code pasting below not showing error server side program seems running indefinitely, i.e., not terminating. got stuck on line marked comment stuck here.
in code, initially, server sending file named "file.txt" , client receiving , saving file name "copy.txt". later client sending file named "file2.txt" , server receiving , saving name "copy2.txt".
can please tell me error , suggest improvements?
//server side code import java.net.*; import java.io.*; public class server { public static void main (string [] args ) throws ioexception { //sending file started serversocket serversocket = new serversocket(16167); socket socket = serversocket.accept(); system.out.println("accepted connection : " + socket); file transferfile = new file ("/users/abhishek/desktop/file.txt"); byte [] bytearray = new byte [(int)transferfile.length()]; fileinputstream fin = new fileinputstream(transferfile); bufferedinputstream bin = new bufferedinputstream(fin); bin.read(bytearray,0,bytearray.length); outputstream os = socket.getoutputstream(); system.out.println("sending files..."); os.write(bytearray,0,bytearray.length); os.flush(); system.out.println("file transfer complete"); //socket.close(); //sending comleted //receiving file started int filesize=1022386; int bytesread=0; int currenttot = 0; byte [] bytearray1 = new byte [filesize]; inputstream = socket.getinputstream(); fileoutputstream fos = new fileoutputstream("/users/abhishek/desktop/copy2.txt"); //fos.flush(); bufferedoutputstream bos = new bufferedoutputstream(fos); //bos.flush(); system.out.println("not moving ahead!!!");//program stucked here bytesread = is.read(bytearray1,0,bytearray1.length); currenttot = bytesread; system.out.println("current"+currenttot); { bytesread = is.read(bytearray1, currenttot, (bytearray1.length-currenttot)); if(bytesread >= 0) currenttot += bytesread; system.out.println("current"+currenttot); } while(bytesread > -1); system.out.println("outside current"+currenttot); bos.write(bytearray1, 0 , currenttot); bos.flush(); //receiving complete system.out.println("receving file completed"); socket.close(); } } //client side code import java.net.*; import java.io.*; public class client { public static void main (string [] args ) throws ioexception { int filesize=1022386; int bytesread=0; int currenttot = 0; socket socket = new socket("localhost",16167); byte [] bytearray = new byte [filesize]; inputstream = socket.getinputstream(); fileoutputstream fos = new fileoutputstream("/users/abhishek/desktop/copy.txt"); bufferedoutputstream bos = new bufferedoutputstream(fos); bytesread = is.read(bytearray,0,bytearray.length); currenttot = bytesread; { bytesread = is.read(bytearray, currenttot, (bytearray.length-currenttot)); if(bytesread >= 0) currenttot += bytesread; } while(bytesread > -1); system.out.println("current"+currenttot); bos.write(bytearray, 0 , currenttot); bos.flush(); bos.close(); system.out.println("receiving first file completed!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); //sending file system.out.println("sending second file started!"); file transferfile = new file ("/users/abhishek/desktop/file2.txt"); byte [] bytearray2 = new byte [(int)transferfile.length()]; fileinputstream fin = new fileinputstream(transferfile); bufferedinputstream bin = new bufferedinputstream(fin); bin.read(bytearray2,0,bytearray2.length); outputstream os = socket.getoutputstream(); os.flush(); os.write(bytearray2,0,bytearray2.length); os.flush(); system.out.println("sending second file completed!"); //sending complete socket.close(); } }
does system.out.println("not moving ahead!!!");//program stucked here
line execute? if so, problem inputstream.read() functions blobking functions; stop execution of program ("block") until able complete.
from javadoc inputstream:
reads len bytes of data input stream array of bytes. attempt made read many len bytes, smaller number may read. number of bytes read returned integer. this method blocks until input data available, end of file detected, or exception thrown.
since aren't getting exception, means when call .read(), there no data available read, , program sits around waiting data read (that never arrives). should check client program sending data in first place.
Comments
Post a Comment