How to transfer a zip file through socket programming in java? -
i new socket programming. did simple program transfer zip files creating empty zip , doesn't transfer files. can me please?
client.java
package filetransfer; import java.io.bufferedoutputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.net.socket; public class simpleclient { public final static int socket_port = 13267; public final static string server = "00.200.00.00"; public final static string file_to_received = "d:/projects/transferred.zip"; public final static int file_size = 6022386; public static void main (string [] args ) throws ioexception { int bytesread; int current = 0; fileoutputstream fos = null; bufferedoutputstream bos = null; socket sock = null; try { sock = new socket(server, socket_port); system.out.println("connecting..."); // receive file byte [] mybytearray = new byte [file_size]; inputstream = sock.getinputstream(); fos = new fileoutputstream(file_to_received); bos = new bufferedoutputstream(fos); bytesread = is.read(mybytearray,0,mybytearray.length); current = bytesread; { bytesread = is.read(mybytearray, current, (mybytearray.length-current)); if(bytesread >= 0) current += bytesread; } while(current < file_size); bos.write(mybytearray, 0 , current); bos.flush(); system.out.println("file " + file_to_received + " downloaded (" + current + " bytes read)"); } { if (fos != null) fos.close(); if (bos != null) bos.close(); if (sock != null) sock.close(); } } }
server.java
package filetransfer; import java.io.bufferedinputstream; import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.io.outputstream; import java.net.serversocket; import java.net.socket; public class simpleserver { public final static int socket_port = 13267; public final static string file_to_send = "c:/users/public/pictures/sample pictures.zip"; public static void main (string [] args ) throws ioexception { fileinputstream fis = null; bufferedinputstream bis = null; outputstream os = null; serversocket servsock = null; socket sock = null; try { servsock = new serversocket(socket_port); while (true) { system.out.println("waiting..."); try { sock = servsock.accept(); system.out.println("accepted connection : " + sock); // send file file myfile = new file (file_to_send); byte [] mybytearray = new byte [(int)myfile.length()]; fis = new fileinputstream(myfile); bis = new bufferedinputstream(fis); bis.read(mybytearray,0,mybytearray.length); os = sock.getoutputstream(); system.out.println("sending " + file_to_send + "(" + mybytearray.length + " bytes)"); os.write(mybytearray,0,mybytearray.length); os.flush(); system.out.println("done."); } { if (bis != null) bis.close(); if (os != null) os.close(); if (sock!=null) sock.close(); } } } { if (servsock != null) servsock.close(); } } }
kindly me fix this!!!
try read file in such way on client side:
socket s = servsock.accept(); inputstream in = s.getinputstream(); bufferedoutputstream bos = new bufferedoutputstream(new fileoutputstream("your_file")); int c=0; byte[] buff=new byte[2048]; while((c=in.read(buff))>0){ // read inputstream buffer // if read bos.write(buff, 0, c); } in.close(); bos.close();
do same on server side. inputstream
file , output socket. robust way copy streams.
Comments
Post a Comment