windows - Java read bytes from Socket on Linux -
i'm trying send file windows machine raspberry-pi 2, , have client , server. client should able send zip file on network server on linux machine. know client , server work on windows, when run both client , server on windows , connect using 127.0.0.1 works perfectly. when sending pi, nothing gets sent on socket. suggestion?
server: package zipsd; import java.io.bufferedoutputstream; import java.io.bufferedreader; import java.io.file; import java.io.fileoutputstream; import java.io.inputstream; import java.io.inputstreamreader; import java.io.printstream; import java.net.serversocket; import java.net.socket; public class main { public static void main(string[] args) throws exception { if (args.length < 3) system.out.println("usage: zipsd <port> <directory> <password>"); else { int port = integer.parseint(args[0]); string directory = args[1]; string password = args[2]; system.out.println("zipsd: starting server on port " + port); system.out.println("zipsd: directory = " + directory); serversocket ss = new serversocket(port); system.out.println("zipsd: listening..."); while (true) { try { socket client = ss.accept(); system.out .println("zipsd: " + client.getinetaddress()); inputstream input = client.getinputstream(); bufferedreader in = new bufferedreader( new inputstreamreader(input)); printstream out = new printstream(client.getoutputstream()); string pwdattempt = in.readline(); if (pwdattempt != null) { if (!pwdattempt.equals(password)) { out.println("[server] zipsd: invalid password"); } else { out.println("[server] zipsd: authenticated"); string zipname = in.readline(); if (zipname != null) { file zipfile = new file(directory + "/" + zipname); try { fileoutputstream fos = new fileoutputstream( zipfile); bufferedoutputstream bos = new bufferedoutputstream( fos); byte[] data = new byte[1024 * 1024 * 50]; int count; while((count = input.read(data)) > 0) bos.write(data, 0, count); for(int = 0; < 200; i++) //to see if data gets sent, prints 0's :( system.out.println(data[i]); system.out.println("got zip file " + zipname); bos.flush(); fos.close(); bos.close(); out.close(); in.close(); client.close(); } catch (exception e) { out.println("[server] zipsd: error in transfer."); } } } } } catch (exception e) { e.printstacktrace(); } } } } } client: package zipsend; import java.io.bufferedinputstream; import java.io.bufferedreader; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstreamreader; import java.io.outputstream; import java.io.printstream; import java.net.socket; import java.nio.file.files; import java.util.zip.zipentry; import java.util.zip.zipoutputstream; public class main { public static class ziputils { public static void zipfolder(final file folder, final file zipfile) throws ioexception { zipfolder(folder, new fileoutputstream(zipfile)); } public static void zipfolder(final file folder, final outputstream outputstream) throws ioexception { try (zipoutputstream zipoutputstream = new zipoutputstream( outputstream)) { processfolder(folder, zipoutputstream, folder.getpath() .length() + 1); zipoutputstream.flush(); zipoutputstream.finish(); zipoutputstream.close(); } } private static void processfolder(final file folder, final zipoutputstream zipoutputstream, final int prefixlength) throws ioexception { (final file file : folder.listfiles()) { if (file.isfile()) { final zipentry zipentry = new zipentry(file.getpath() .substring(prefixlength)); zipoutputstream.putnextentry(zipentry); try (fileinputstream inputstream = new fileinputstream(file)) { byte[] buf = new byte[(int) file.length() + 1]; int read = 0; while ((read = inputstream.read(buf)) != -1) { zipoutputstream.write(buf, 0, read); } } zipoutputstream.flush(); zipoutputstream.closeentry(); } else if (file.isdirectory()) { processfolder(file, zipoutputstream, prefixlength); } } } } public static void main(string[] args) throws exception { if(args.length < 4) system.out.println("usage: zipsend <folder> <ip> <port> <password>"); else { string tozip = args[0]; string ip = args[1]; int port = integer.parseint(args[2]); string pwd = args[3]; file foldertozip = new file(tozip); if(!foldertozip.exists()) { system.out.println("[error] invalid folder name"); system.exit(1); } system.out.print("[info] connecting... "); socket s = new socket(ip, port); system.out.println("ok."); system.out.println("[info] authenticating... "); bufferedreader in = new bufferedreader(new inputstreamreader(s.getinputstream())); printstream out = new printstream(s.getoutputstream()); out.println(pwd); system.out.println(in.readline()); system.out.println(); system.out.print("[info] zipping " + tozip + "... "); file zipfile = new file(system.getproperty("user.dir") + "\\" + foldertozip.getname() + ".zip"); zipoutputstream zout = new zipoutputstream(new fileoutputstream(zipfile)); ziputils.processfolder(foldertozip, zout, foldertozip.getpath().length() + 1); zout.close(); system.out.println("ok."); //transfer file out.println(zipfile.getname()); byte[] data = new byte[(int)zipfile.length()]; fileinputstream fis = new fileinputstream(zipfile); bufferedinputstream bis = new bufferedinputstream(fis); system.out.println("[info] sending zip file... "); outputstream os = s.getoutputstream(); int count; while((count = bis.read(data)) > 0) { os.write(data, 0, count); } os.flush(); os.close(); fis.close(); bis.close(); s.close(); system.out.println("[info] done. sent " + files.size(zipfile.topath()) + " bytes."); zipfile.delete(); } } }
inputstream input = client.getinputstream(); bufferedreader in = new bufferedreader(new inputstreamreader(input));
your problem here. can't use multiple inputs on socket when 1 or more of them buffered. buffered input stream/reader read-ahead , 'steal' data other stream. need change protocol can use same stream life of socket @ both ends. example, use datainputstream
, readutf()
file name , read()
data: @ sender, use dataoutputstream
, writeutf()
filename , write()
data.
Comments
Post a Comment