简体   繁体   中英

br.readline() gets stuck while br.read() works

I am making a simple ftp client/server program which on command from the clients lists files, tells the current directory, downloads files My client code works fine since i have already tested it with a working server. However the server that i have designed gets stuck in the run() function on the line String message = br.readline(); If instead i use the br.read(), then it works but i need command in form of a string to know which file i have to download whereas br.read() returns int. Here's my code, i have used threading.

public class Myserver {
static final int PortNumber = 108;
static ServerSocket MyService;
static Socket clientSocket = null;
/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    File directory;
    directory = new File(System.getProperty("user.home"));
     try {
           MyService = new ServerSocket(PortNumber);
           String cd = directory.toString();
           System.out.println(cd);
           System.out.println("Listening on " + PortNumber);
           while(true) {
           clientSocket = MyService.accept();
           Connecthandle a = new Connecthandle(clientSocket, directory);
           a.run();
           }
     }
     catch (IOException e) {
     System.out.println(e);
     }
}

     static class Connecthandle extends Thread {
         File Directory;
         Socket clientsocket;

         // Constructor for class
         Connecthandle(Socket clients, File dir) {
             clientsocket = clients;
             Directory = dir;
         }

         // Works Fine
         void listfiles() throws IOException {
             String []Listfile = Directory.list();
             String send = "";
             for (int j = 0; j < Listfile.length; j++) {
                 send = send + Listfile[j] + ",";
             }
             DataOutputStream GoingOut = new   DataOutputStream(clientsocket.getOutputStream());
             GoingOut.writeBytes(send);
             GoingOut.flush();
             GoingOut.close();
         }
         // Works Fine
         void currentdirectory() throws IOException {
             String cd = Directory.toString();
             String cdd = "resp," + cd;
             System.out.println(cdd);
             DataOutputStream GoingOut = new DataOutputStream(clientsocket.getOutputStream());
             GoingOut.writeBytes(cdd);
             GoingOut.flush();
             GoingOut.close();
             System.exit(0);
         }

         void sendfiles(String fileName) {
             try {
             File nfile = new File(fileName);
             DataOutputStream GoingOut = new DataOutputStream(clientsocket.getOutputStream());
             if ( (! nfile.exists()) || nfile.isDirectory() ) {
               GoingOut.writeBytes("file not present");
            } else {
             BufferedReader br = new BufferedReader(new FileReader(nfile));
             String line;
             while ((line = br.readLine()) != null) {
                 line = br.readLine();
                 GoingOut.writeBytes(line+"\n");
             }
             GoingOut.flush();
             GoingOut.close();
             br.close();
            }
             } catch (IOException e) {
                 System.out.println("Unable to send!");
             }
         }

         @SuppressWarnings("deprecation")
        public void run() {
             try {
             DataInputStream comingin = new DataInputStream(clientsocket.getInputStream());
             InputStreamReader isr  = new InputStreamReader(comingin, "UTF-8");
             BufferedReader br = new BufferedReader(isr);
             System.out.println("here");
             // if (br.ready())
             String message = br.readLine(); // Code gets stuck here, if i use br.read() it works, but i need string output.
             if (message.equals("listfiles\n")) {
                 listfiles();
             } else if (message.equals("pwd")) {
                 currentdirectory();
             } else if (message.contains("getfile,")) {
                 String fileName = new String(message.substring(8, message.length()));
                 sendfiles(fileName);
             }
             } catch (Exception e) {
                 e.printStackTrace();
             }
             finally {
                 try {
                     clientsocket.close();
                 } catch (IOException e) {}
             }
         }
     }

}

如果readLine()正在阻塞并且您正在发送数据,则您不是在发送换行符。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM