2012-02-28 14 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

+0

@Sunshine - 正確に。 – mah

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

if(counter == fileSize)break; –

関連する問題