Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>See with binary data you have change the readers as they are capable of only characters and do not work with byte stream. moreover readline means read till end of line and in binary files ('\n') does not make too much sense. <br/> This is from documentation of printWriter</p> <p><code>It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.</code></p> <p>What you would want now is to use byte arrays and write them as chunks like this :</p> <pre><code>import java.io.*; import java.net.*; import java.util.*; public class ft2server { public static void main(String args[])throws IOException { ServerSocket ss=null; try { ss=new ServerSocket(8085); } catch(IOException e) { System.out.println("couldn't listen"); System.exit(0); } Socket cs=null; try { cs=ss.accept(); System.out.println("Connection established"+cs); } catch(Exception e) { System.out.println("Accept failed"); System.exit(1); } BufferedOutputStream put=new BufferedOutputStream(cs.getOutputStream()); BufferedReader st=new BufferedReader(new InputStreamReader(cs.getInputStream())); String s=st.readLine(); String str = "/home/milind/Desktop/"; String path = str + s; System.out.println("The requested file is path: "+path); System.out.println("The requested file is : "+s); File f=new File(path); if(f.isFile()) { FileInputStream fis=new FileInputStream(f); byte []buf=new byte[1024]; int read; while((read=fis.read(buf,0,1024))!=-1) { put.write(buf,0,read); put.flush(); } //d.close(); System.out.println("File transfered"); cs.close(); ss.close(); } } } </code></pre> <p>The client</p> <pre><code>import java.io.*; import java.net.*; import java.util.*; public class ft2client { public static void main(String srgs[])throws IOException { Socket s=null; BufferedInputStream get=null; PrintWriter put=null; try { s=new Socket("127.0.0.1",8085); get=new BufferedInputStream(s.getInputStream()); put=new PrintWriter(s.getOutputStream(),true); String f; int u; System.out.println("Enter the file name to transfer from server:"); DataInputStream dis=new DataInputStream(System.in); f=dis.readLine(); put.println(f); File f1=new File(f); String str = "/home/milind/"; FileOutputStream fs=new FileOutputStream(new File(str,f1.toString())); byte jj[]=new byte[1024]; while((u=get.read(jj,0,1024))!=-1) { fs.write(jj,0,u); } fs.close(); System.out.println("File received"); s.close(); }catch(Exception e) { e.printStackTrace(); System.exit(0); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload