2012-02-28 54 views
0

我想要一種在套接字中發送停止消息的高效且快速的方式。在套接字中發送停止消息

我有一個從一臺電腦發送文件到另一臺的方法。發件人的所有文件都出現在接收者的PC上。但是,所有數據都被寫入第一個文件(僅)。其他文件存在,但是是空的。發生這種情況是因爲接收方法不知道何時開始寫入下一個文件。

發件人

public static void sendFile (final Socket sock, File source) 
{ 
    FileInputStream fileIn = null; 

    try 
{ 
     //Read bytes from the source file 
     fileIn = new FileInputStream(source); 

     //Write bytes to the receive 
     //No need to use a buffered class, we make our own buffer. 
     OutputStream netOut = sock.getOutputStream(); 

     byte[] buffer = new byte[BUFFER_SIZE]; 
     int read; 

     while ((read = fileIn.read(buffer)) != -1) 
     { 
      netOut.write(buffer, 0, read); 
      netOut.flush(); 
     } 
     //Send some stop message here 
} 
    catch (Exception e) 
{ 
     e.printStackTrace(); 
} 
    finally 
    { 
     if (fileIn != null) 
     { 
      try 
      { 
       fileIn.close(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

//Send files via socket 
public static void sendFile (final Socket sock, File[] source) 
{ 
    for (int i = 0; i < source.length; i++) 
     sendFile (sock, source[i]); 
} 

接收機:

public static void receiveFile (final Socket sock, File destination) 
{ 
    BufferedOutputStream out = null; 

    try 
    { 
     //Receive data from socket 
     InputStream clientInputStream = sock.getInputStream(); 

     //Write bytes to a file 
     out = new BufferedOutputStream (new FileOutputStream (destination)); 

     byte[] buffer = new byte[BUFFER_SIZE]; 
     int read; 
     while (true) 
     { 
      read = clientInputStream.read(buffer); 


      out.write(buffer, 0, read); 
      out.flush(); 
     } 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    finally 
    { 
     if (out != null) 
     { 
      try 
      { 
       out.close(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

//Receive files via socket 
public static void receiveFile (final Socket sock, File[] destination) 
{ 
    for (int i = 0; i < destination.length; i++) 
     receiveFile (sock, destination[i]); 
} 

回答

2

你需要修改你的發送/接收協議發送文件之前,至少包括最小的頭。您的標題應至少包含要遵循的數據大小以及您可能需要的任何內容(如文件名)。

+0

通過 「數據的大小」,我想你指的是文件的大小? – Sunshine 2012-02-28 00:53:45

+0

@陽光 - 正好。 – mah 2012-02-28 00:54:30

0

我試着用你建議的標題,但它不起作用。接收者仍然不知道何時停止(所以我得到EOFException)。所有收到的數據都寫入第一個文件。

public static void sendFile (Socket sock, File source) 
{ 
    FileInputStream fileIn = null; 

    try 
    { 
     //Read bytes from the source file 
     fileIn = new FileInputStream(source); 

     //Write bytes to the receive 
     //No need to use a buffered class, we make our own buffer. 
     OutputStream netOut = sock.getOutputStream(); 

     byte[] buffer = new byte[BUFFER_SIZE]; 
     int readBytes = 0; 
     long fileSize = source.length(); 
     long counter = 0; 

     //Send the file size 
     DataOutputStream objOut = new DataOutputStream (netOut); 
     System.out.println ("Writing: " + source.length()); 
     objOut.writeLong (fileSize); 
     objOut.flush(); 

     while ((counter += readBytes) < fileSize) 
     { 
      readBytes = fileIn.read(buffer); 
      netOut.write(buffer, 0, readBytes); 
      netOut.flush(); 
     } 
     fileIn.close(); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    finally 
    { 
     if (fileIn != null) 
     { 
      try 
      { 
       fileIn.close(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

[]

public static void receiveFile (Socket sock, File destination) 
{ 
    BufferedOutputStream fileOut = null; 

    try 
    { 
     //Receive data from socket 
     InputStream netIn = sock.getInputStream(); 

     //Write bytes to a file 
     fileOut = new BufferedOutputStream (new FileOutputStream (destination)); 

     byte[] buffer = new byte[BUFFER_SIZE]; 
     int readBytes = 0; 
     long fileSize; 
     long counter = 0; 

     //Receive the file size 
     DataInputStream objIn = new DataInputStream (netIn); 
     fileSize = objIn.readLong(); 
     System.out.println ("Receiving: " + fileSize); 

     while (true) 
     { 
      readBytes = netIn.read (buffer); 
      fileOut.write (buffer, 0, readBytes); 
      fileOut.flush(); 

      counter += readBytes; 
      if (counter > fileSize) 
       break; 
     } 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    finally 
    { 
     if (fileOut != null) 
     { 
      try 
      { 
       fileOut.close(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
     } 
    } 
    System.out.println ("Ending method"); 
} 
+0

不應該是如果(計數器== fileSize)休息;? – 2012-03-02 18:51:34

相關問題