2012-09-21 54 views
-2

我有一個Android客戶端和多線程Java服務器。這個服務器最初是用Python編寫的,效果很好,但現在我用Java重新編寫了它,但它似乎沒有工作。以下是我的服務器代碼。值得一提的是,Python的實現並不是多線程的,但我不認爲我需要爲此改變客戶端。的Java線程TCP服務器套接字

import java.net.*; 
import java.io.*; 
import org.apache.commons.io.FileUtils; 

public class MultiServerThread extends Thread { 
    private Socket socket = null; 

    public MultiServerThread(Socket socket) { 
     super("MultiServerThread"); 
     this.socket = socket; 
    } 

    @Override 
    public void run() { 

     try { 
      String path = "C:/Users/LandClan/Desktop/cubikal"; 
      int count = 0; 
      DataOutputStream dos = new DataOutputStream(
        new BufferedOutputStream(socket.getOutputStream())); 
      DataInputStream dis = new DataInputStream(new BufferedInputStream(
        socket.getInputStream())); 

      File[] files = new File(path).listFiles(); 
      for (File file : files) { 
       String filename = file.getName(); 
       String extension = filename.substring(
         filename.lastIndexOf(".") + 1, filename.length()); 
       if ("png".equals(extension)) { 
        count += 1; 
       } 

      } 
      System.out.println("Sending " + count + " files"); 
      dos.writeInt(count); 
      byte[] temp = new byte[1024]; 
      int n = 0; 
      for (File file : files) { 
       String filename = file.getName(); 
       String extension = filename.substring(
         filename.lastIndexOf(".") + 1, filename.length()); 
       if ("png".equals(extension)) { 
        FileInputStream fis = new FileInputStream(file); 
        BufferedInputStream bis = new BufferedInputStream(fis); 
        byte fileContent[] = new byte[(int) file.length()]; 
        bis.read(fileContent); 
        int dataLength = fileContent.length; 
        dos.writeInt(dataLength); 
        System.out.println(filename + " is " + dataLength 
          + " bytes long"); 
        while ((dataLength > 0) 
          && (n = bis.read(temp, 0, 
            (int) Math.min(temp.length, dataLength))) != -1) { 
         dos.write(temp, 0, n); 
         dos.flush(); 
         dataLength -= n; 
        } 
        // System.out.println("Sent file "+filename); 
        fis.close(); 
       } 
      } 
      for (File file1 : files) { 
       String filename = file1.getName(); 
       String extension = filename.substring(
         filename.lastIndexOf(".") + 1, filename.length()); 
       if ("txt".equals(extension)) { 
        FileInputStream fis = new FileInputStream(file1); 
        BufferedInputStream bis = new BufferedInputStream(fis); 
        byte fileContent[] = new byte[(int) file1.length()]; 
        bis.read(fileContent); 
        int dataLength = fileContent.length; 
        dos.writeInt(dataLength); 
        System.out.println("file is " + dataLength + "long"); 
        while ((dataLength > 0) 
          && (n = bis.read(temp, 0, 
            (int) Math.min(temp.length, dataLength))) != -1) { 
         dos.write(temp, 0, n); 
         dos.flush(); 
         dataLength -= n; 
        } 
        // System.out.println("Sent file"); 
        fis.close(); 
       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

這裏是服務器

package server; 

import java.net.*; 
import java.io.*; 

public class Server { 
    /** 
    * @param args 
    *   the command line arguments 
    */ 
    public static void main(String[] args) throws IOException { 
     ServerSocket serverSocket = null; 
     boolean listening = true; 

     try { 
      serverSocket = new ServerSocket(4447); 
     } catch (IOException e) { 
      System.err.println("Could not liten on port: 4447."); 
      System.exit(-1); 
     } 

     while (listening) { 
      new MultiServerThread(serverSocket.accept()).start(); 
     } 
     serverSocket.close(); 
    } 
} 
+0

如果你告訴我們你想要它做的事情,這將有助於?而不是閱讀你的代碼,並嘗試理解。 – roni

+0

它應該是從服務器發送圖片和文本文件給客戶 – user1661396

+0

就可以說明問題,而不是工作太一般。 – roni

回答

0

是DataOutputStream似乎從來就沒關閉的第一部分,和DataInputStream類是從未使用過的。

擺脫DataInputStream類的,並確保您關閉DataOutputStream類,當你用它完成。

的一個好方法是將finally{}塊添加到您的try-catch,雖然你需要聲明try-catch外的DataOutputStream類,因此在finally可見。

DataOutputStream dos = null; 
try 
{ 
    DataOutputStream dos = new DataOutputStream(
       new BufferedOutputStream(socket.getOutputStream())); 
    // do stuff 
} 
catch(IOException e) 
{ 
    //stacktrace etc 
} 
finally 
{ 
    if (dos != null) dos.close(); 
} 

這種東西總是在Java中有點難看,但即將到來的版本可能做的更好......