java - Sends files from client to server -


in below codes,i trying let server-side select file client , sends it, work done on server side. program works running server first wait client run, make connection, server sends place of file client-side outreader.write("b://ghassar/ghassar.txt"); client reads location , sends file. have run debug , once server reads code string filename = inreader.readline(); stops , gets in loop, can me solve problem

/ server.java  /*  * server waits connection established client  *  */  import java.io.*; import java.net.*;  class server {     public static void main(string args[])throws exception     {         system.out.println("server running...");          /* listen on port 5555 */          serversocket server = new serversocket(222);          /* accept sk */          socket sk = server.accept();          system.out.println("server accepted client");         // recive client         inputstream input = sk.getinputstream();         //read coming         bufferedreader inreader = new bufferedreader(new inputstreamreader(sk.getinputstream()));         //write client         bufferedwriter outreader = new bufferedwriter(new outputstreamwriter(sk.getoutputstream()));             outreader.write("b://ghassar/ghassar.txt");             outreader.flush();         /* read filename */         string filename = inreader.readline();          if (filename.equals("ghassar.txt") ){              /* reply client ready status */              outreader.write("ready\n");             outreader.flush();         }          /* create new file in libya directory using filename */         fileoutputstream wr = new fileoutputstream(new file("b://libya/ "+ filename));          byte[] buffer = new byte[sk.getreceivebuffersize()];          int bytesreceived = 0;          while((bytesreceived = input.read(buffer))>0)         {             /* write file */            wr.write(buffer,0,bytesreceived);         }     } }   import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*;  public class client  {     public static void main(string args[]){            client clientform = new client();         clientform.action();      }     public void action() {           try{              /* try connect server on localhost, port 5555 */              socket sk = new socket("localhost", 222);             outputstream output = sk.getoutputstream();              /* steam send staff*/              outputstreamwriter outputstream = new outputstreamwriter(sk.getoutputstream());              /* steam recive staff */              bufferedreader inreader = new bufferedreader(new inputstreamreader(sk.getinputstream()));                 string serverstatus = inreader.readline(); // read first line                string filename =  serverstatus;//filedlg.getselectedfile().getabsolutepath();                outputstream.write("ghassar.txt");                outputstream.flush();              /* if server ready, send file */              if ( serverstatus.equals("ready") ){                  fileinputstream file = new fileinputstream(filename);                  byte[] buffer = new byte[sk.getsendbuffersize()];                  int bytesread = 0;                  while((bytesread = file.read(buffer))>0)                 {                     output.write(buffer,0,bytesread);                 }                  output.close();                 file.close();                 sk.close();                system.out.println("file sent");              }         }         catch (exception ex){             /* catch errors */                  system.out.println("not file sent");          }     } } 

i suggest use printwriter instead of bufferedwriter. there no need call flush after each line , use println() method along auto-flush feature add new line well.

unlike printstream class, if automatic flushing enabled done when 1 of println, printf, or format methods invoked, rather whenever newline character happens output.

these methods use platform's own notion of line separator rather newline character.

there no need append \n in message itself.

sample code:

// here true means auto flush when `println()` method called printwriter w=new printwriter(new outputstreamwriter(sk.getoutputstream()),true); w.println(message); 

possible cause in code:

the problem may @ below lines \n not added in end of string in write() method before flushing , @ client side method readline() used read it.

bufferedwriter outreader = new bufferedwriter(new outputstreamwriter(sk.getoutputstream())); outreader.write("b://ghassar/ghassar.txt");  outreader.flush(); 

note: can use bufferedwriter#newline() instead of \n writes line separator. line separator string defined system property line.separator, , not single newline ('\n') character.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -