2012-11-29 177 views
0

我試圖建立一個文件發送客戶端/服務器應用程序,但我有較大的文件的問題。我正在使用BufferedInputStream從文件和OutputStream中讀取信息以寫入套接字。我有一個從文件讀取1 KB的循環,然後發送它,這對於前25個循環工作正常,然後崩潰與套接字寫入錯誤。有任何想法嗎?這是代碼。服務器/客戶端java.net.SocketException異常

客戶

import java.io.*; 
import java.net.Socket; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class TCPClient 
{ 
public static void main(String[] args) 
{ 
    /*Variables*/ 
    int serverPort = 8899; 
    String ip = "localhost"; 
    File myFile = new File("GeneratedFile.txt"); //fileToBeSent.txt 

    System.out.println(myFile.length()); 

    try 
    { 
     /*Connect to Server*/ 
     Socket sock = new Socket(ip, serverPort); 
     System.out.println("Connection Made"); 

     /*Create Streams*/ 
     FileInputStream fis = new FileInputStream(myFile); 
     BufferedInputStream bis = new BufferedInputStream(fis); 
     OutputStream clientOutput = sock.getOutputStream(); 

     /*This is the old code for transfer*/ 
     /*Create Byte Array 
     byte[] myByteArray = new byte[(int) myFile.length()]; //was 1024 

     /*Send File 
     bis.read(myByteArray, 0, 1024); 
     clientOutput.write(myByteArray, 0, 1024); 
     clientOutput.flush(); 
     */ 

     for(long i = 0; i <= myFile.length(); i += 1024) 
     { 
      byte[] myByteArray = new byte[1024]; 
      bis.read(myByteArray, 0, 1024); 
      clientOutput.write(myByteArray, 0, 1024); 
      clientOutput.flush(); 
      System.out.println("i is: " + i); 
     } 
     System.out.println("File Written"); 
     sock.close(); 
    } catch (IOException ex) 
    { 
     Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex); 
     System.out.println("You can't do that!"); 
    } 
    System.out.println("Finished"); 
} 
} 

服務器

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

public class RequestHandler 
{ 
public void handleRequest() 
{ 
    try 
    { 
     ServerSocket welcomeSocket = new ServerSocket(8899); 

     while(true) 
     { 
      Socket socket = welcomeSocket.accept(); 
      System.out.println("Socket Open"); 

      /* Create byte array */ 
      byte[] mybytearray = new byte[1024 * 512]; 

      /* Create streams */ 
      InputStream is = socket.getInputStream(); 
      FileOutputStream fos = new FileOutputStream("newFile.txt",true); 
      BufferedOutputStream bos = new BufferedOutputStream(fos); 

      /*Write to file*/ 
      int bytesRead = is.read(mybytearray, 0, mybytearray.length); 
      bos.write(mybytearray, 0, bytesRead); 

      /*Close Stream and Socket*/ 
      bos.close(); 
      socket.close(); 
     } 
    } catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 
public static void main(String args[]) 
{ 
    RequestHandler rq = new RequestHandler(); 
    rq.handleRequest(); 
    System.out.println("Here"); 
} 
} 
+2

什麼是確切的錯誤?什麼是堆棧跟蹤? – reprogrammer

+0

堆棧跟蹤會很好,但也可以嘗試將您的'clientOutput'OutputStream包裝在BufferedOutputStream中。 – GreyBeardedGeek

+0

客戶端或服務器端發生崩潰嗎?您傳輸的文件有多大? (我的假設是,你的緩衝區對於文件的最後部分來說太大了) – Andy

回答

1

你的複製技術是不正確。這是如何在Java中複製流:

byte[] buffer = new byte[8192]; // or whatever you like, but declare it outside the loop 
int count; 
while ((count = in.read(buffer)) > 0) 
{ 
    out.write(buffer, 0, count); 
} 
out.flush(); 
// then in a finally block ... 
out.close(); 
in.close(); 

你需要在兩端。你不能假定任何給定的讀取將填充緩衝區,所以你必須循環到EOS。請注意,不會在循環內沖洗

+0

謝謝,解決了這個問題 – Dommol

相關問題