2013-07-26 17 views
1

我正在一個客戶端服務器文件傳輸項目上,我已經差不多完成了我在localhost上的測試,但今天出現下面的錯誤,這裏是客戶端和服務器的源代碼:java文件傳輸套接字寫錯誤

客戶端

public class Client { 

    public static void main(String args[]) { 
     String receiverIP = null; 
     int serverPort = 0; 
     receiverIP = args[0]; 
     serverPort = Integer.parseInt(args[1]); 
     String fileToSend = args[2]; 
     byte[] aByte = new byte[1]; 
     int bytesR; 
     Socket clientSocket = null; 
     BufferedOutputStream bos = null; 
     InputStream is = null; 

     try { 
      clientSocket = new Socket(receiverIP, serverPort); 
      bos = new BufferedOutputStream(clientSocket.getOutputStream());   
      is = clientSocket.getInputStream(); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     }  

     if (is != null) {   
      FileOutputStream fos = null; 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      try { 

        File myFile = new File(fileToSend); 
        System.out.println("The file chosen is being sent..."); 
        byte[] mybytearray = new byte[(int) myFile.length()]; 
        FileInputStream fis = null; 

        try { 
         fis = new FileInputStream(myFile); 
        } catch (FileNotFoundException ex) { 
         ex.printStackTrace(); 
        } 
        try { 
         BufferedInputStream bis = new BufferedInputStream(fis); 
         bis.read(mybytearray, 0, mybytearray.length); 
         bos.write(mybytearray, 0, mybytearray.length); 
         bis.close(); 

         return; 
        }catch (IOException ex) { 
         ex.printStackTrace(); 
        } 

       File file = new File("C:\\copy.jpg"); 
       fos = new FileOutputStream(file); 
       bos = new BufferedOutputStream(fos); 
       bytesR = is.read(aByte, 0, aByte.length); 
       do { 
         baos.write(aByte); 
         bytesR = is.read(aByte); 
       } while (bytesR != -1); 
       System.out.println("File transfer successful"); 

       bos.write(baos.toByteArray()); 
       bos.flush(); 
       bos.close(); 
       clientSocket.close(); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
    } 
} 

服務器端

public class Server { 

    public static void main(String args[]) { 

     while (true) { 
      ServerSocket welcomeSocket = null; 
      BufferedOutputStream ToClient = null; 

      try { 
       welcomeSocket = new ServerSocket(3249); 
       System.out.println("The port " + welcomeSocket.getLocalPort() + " is opened and ready for use."); 
       Socket connectionSocket = welcomeSocket.accept(); 

       ToClient = new BufferedOutputStream(connectionSocket.getOutputStream()); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
    } 
} 

這裏是錯誤我得到

The file chosen is being sent... 
java.net.SocketException: Connection reset by peer: socket write error 
    at java.net.SocketOutputStream.socketWrite0(Native Method) 
    at java.net.SocketOutputStream.socketWrite(Unknown Source) 
    at java.net.SocketOutputStream.write(Unknown Source) 
    at java.io.BufferedOutputStream.write(Unknown Source) 
    at Client.main(Client.java:44) 
java.net.SocketException: Connection reset 
    at java.net.SocketInputStream.read(Unknown Source) 
    at java.net.SocketInputStream.read(Unknown Source) 
    at Client.main(Client.java:55) 

我幾乎肯定這個錯誤是不是被傳輸的所有數據之前關閉服務器插槽,以及有關的ByteArray但我所有的修復嘗試的讀寫過程都白費了,也許我錯誤的流,使他們不按預期工作,(copy.jpg文件創建,但沒有得到任何流)任何幫助,將不勝感激

編輯:我忘了提及,目前我使用無線互聯網連接和我讀了一些關於套接字porgramming提到無線網絡是不可靠的測試

回答

0

喲您的服務器端不會等待接收數據。它接受連接,然後立即繼續下一個循環。這會導致您的初始服務器套接字和套接字到客戶端被垃圾收集並關閉。

您可以通過使用telnet來驗證此行爲,這在檢查服務器時非常方便。打開服務器機(CMD或控制檯)上的命令提示符,然後運行這個命令來連接到服務器:

telnet localhost 3249 

你會看到,它相連接,然後得到幾乎立即斷開,就像你自己的客戶端應用程序。

解決方法是,除了用於接受服務器端連接的代碼外,還需要在那裏編寫代碼以接收數據。

此外,您應該將服務器套接字的創建置於循環之前,而不是在循環內部。您只需要創建一次服務器套接字,然後您可以通過它接受任意多個連接。多次打開服務器套接字將失敗,因爲端口仍然被佔用(即使先前的服務器套接字已關閉,在某些操作系統中釋放該端口通常需要幾秒鐘的時間)。