简体   繁体   English

br.readline()在br.read()工作时卡住了

[英]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. 我正在制作一个简单的ftp客户端/服务器程序,从客户端发出命令后,它会列出文件,告诉当前目录,下载文件。我的客户端代码工作正常,因为我已经在工作服务器上对其进行了测试。 However the server that i have designed gets stuck in the run() function on the line String message = br.readline(); 但是,我设计的服务器陷在String message = br.readline();行的run()函数中。 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. 如果相反我使用br.read(),那么它可以工作,但是我需要以字符串形式的命令来知道我必须下载哪个文件,而br.read()返回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()正在阻塞并且您正在发送数据,则您不是在发送换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 while(br.readLine()!= null)|窗口应用程序,java - while(br.readLine() != null)|Window application, java br.readLine()=“,”时生成ArrayIndexOutOfBoundsException - ArrayIndexOutOfBoundsException generated when br.readLine() = “,” Java缓冲阅读器,如何使用br.readLine(System.in)读取行并将其转换为char? - Java-Buffered Reader, How do you read a line using br.readLine(System.in) and convert it to char? 为什么这行char g =(char)(br.read()); 被跳过? - Why is this line char g = (char)(br.read()); getting skipped? 为什么服务器程序在“字符串文件= br.readLine()”行冻结? - Why does server program freeze on line 'String file = br.readLine()'? 如何检测文件已从br.readline()循环中删除 - how to detect file has been deleted from within br.readline() loop test1.java:10: 错误:未报告的异常 IOException; 必须被捕获或声明被抛出 char ch = (char)br.read(); ^ - test1.java:10: error: unreported exception IOException; must be caught or declared to be thrown char ch = (char)br.read(); ^ Java:无法从套接字读取,线程卡在readLine()上 - Java: cannot read from socket, thread gets stuck on readLine() 我的应用程序卡在mBufferIn.readLine()上 - My application gets stuck on mBufferIn.readLine() 执行readLine()时套接字卡住 - Socket gets stuck when doing readLine()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM