第一个命令后Socket被关闭

时间:2014-02-19 23:38:28

标签: java sockets networking ftp

我正在制作一个简单的ftp客户端/服务器程序,它从客户端命令列出文件,告诉当前目录,下载文件。

我想创建一个能够与多个客户端连接的服务器,并且能够一次处理多个命令。我使用了线程,但我的代码在执行第一个命令后出现以下错误。

socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.DataInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Myserver$Connecthandle.run(Myserver.java:123)
at Myserver.main(Myserver.java:35)
java.net.SocketException: socket closed

这是我的服务器代码:

public class Myserver {
static final int PortNumber = 120;
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.dir"));
     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.start();
           }
     }
     catch (IOException e) {
     System.out.println(e);
     }
}

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

         // 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();
         }

         // Works fine
         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));
             int coun = 0;
             String lin;
             while ((lin = br.readLine()) != null) {
                 coun++;
             }
             GoingOut.writeBytes("resp," + fileName + "," + String.valueOf(coun) + "\n");
             @SuppressWarnings("resource")
            BufferedReader sr = new BufferedReader(new FileReader(nfile));
             String line;
             while ((line = sr.readLine()) != null) {
                 GoingOut.writeBytes(line+"\n");
                 GoingOut.flush();
             }
             GoingOut.close();
             br.close();
            }
             } catch (IOException e) {
                 System.out.println("Unable to send!");
             }
         }

         public void start() {
             DataInputStream comingin = null;
            try {
                comingin = new DataInputStream(clientsocket.getInputStream());
            } catch (IOException e2) {
                e2.printStackTrace();
            }
             InputStreamReader isr = null;
            try {
                isr = new InputStreamReader(comingin, "UTF-8");
            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
            }
             BufferedReader br = new BufferedReader(isr);
             while (true) {
             try {
             String message = br.readLine(); 
             if (message.contains("pwd")) {
                 currentdirectory();
             } else if (message.contains("list")) {
                 listfiles();
             } else if (message.contains("get")) {
                 String fileName = new String(message.substring(8, message.length()));
                 sendfiles(fileName);
             } else if (message.contains("exit")) {
                 System.exit(0);
             }
             } catch (Exception e) {
                 e.printStackTrace();
             } finally {
                 try {
                    clientsocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
             }
             }
         }
     }

} 我将所有方法都移到了start()中,因此我只需要声明dataoutputstream一次,但现在只要我使用Outgoing.writeBytes

就会给出一个NULLPOINTEREXCEPTION

更新:

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

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

         public void run() {
             DataInputStream comingin = null;
            try {
                comingin = new DataInputStream(clientsocket.getInputStream());
            } catch (IOException e2) {
                e2.printStackTrace();
            }
             InputStreamReader isr = null;
            try {
                isr = new InputStreamReader(comingin, "UTF-8");
            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
            }
             BufferedReader br = new BufferedReader(isr);
             while (true) {
             try {
             String message = br.readLine(); 
             if (message.contains("pwd")) {
                 String cd = Directory.toString();
                 String cdd = "resp," + cd;
                 System.out.println(cdd);
                 DataOutputStream GoingOut = new DataOutputStream(clientsocket.getOutputStream());
                 GoingOut.writeBytes(cdd);
                 GoingOut.flush();
             } else if (message.contains("list")) {
                 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();
             } else if (message.contains("get")) {
                 String fileName = new String(message.substring(8, message.length()));
                 try {
                     File nfile = new File(fileName);
                     DataOutputStream GoingOut = new DataOutputStream(clientsocket.getOutputStream());
                     if ( (! nfile.exists()) || nfile.isDirectory() ) {
                       GoingOut.writeBytes("file not present");
                    } else {
                     BufferedReader wr = new BufferedReader(new FileReader(nfile));
                     int coun = 0;
                     String lin;
                     while ((lin = wr.readLine()) != null) {
                         coun++;
                     }
                     GoingOut.writeBytes("resp," + fileName + "," + String.valueOf(coun) + "\n");
                     @SuppressWarnings("resource")
                    BufferedReader sr = new BufferedReader(new FileReader(nfile));
                     String line;
                     while ((line = sr.readLine()) != null) {
                         GoingOut.writeBytes(line+"\n");
                         GoingOut.flush();
                     }
                     GoingOut.close();
                     br.close();
             } 
                 } catch (IOException e) {
                     System.out.println("Unable to send!"); 
                 }
             } else if (message.contains("exit")) {
                 System.exit(0);
             }
             } catch (Exception e) {
                 e.printStackTrace();
             } finally {
                 try {
                    clientsocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
             }
             }
         }
     }
}

1 个答案:

答案 0 :(得分:0)

关闭套接字的输入流或输出流将关闭另一个流和套接字。我不明白为什么你需要在这里关闭任何东西,直到客户断开连接或你把他安排出去。