2017-11-27 21 views
0

thisガイドに従ってJava NIOソケットチャネルを使用してファイルを送信しようとしました。それはうまく動作します。私はファイルのリストを送信する方法を変更しました。基本的には、上記のガイドで既に実装されているファイルリストと呼び出しメソッドをループします。 「Address already in use」というエラーがあり、FileSenderクラスのソケットクローズラインにコメントしました。その後、コードにエラーはありませんでした。プログラムが途中で止まっているようだ。この問題をどうやって解決するのですか?問題を解決するためのよりよい方法はありますか?Javaを使用して複数のファイルを送信するNIO SocketChannel

Main.Java

public static void main(String[] args) throws IOException, InterruptedException{ 
    RunningConfiguration.run(); 

    List<File> files = new <File>ArrayList(); 

    File a = new File("pics/city.jpg"); 
    File b = new File("pics/desert.jpg"); 
    File c = new File("pics/flower.jpg"); 
    File d = new File("pics/night.jpg"); 

    List<Node> nodes = RunningConfiguration.getNodeList(); 

    ListIterator li = nodes.listIterator(); 

    while(li.hasNext()){ 
     Node node = (Node)li.next(); 
     FileSender.send(node, files, "pics/received/"); 
    } 

} 

FileSender.Java

public class FileSender { 
private final InetSocketAddress fileSocketAddress; 
private final File file; 

public FileSender(InetAddress inetAddress, File file) throws IOException{ 
    this.fileSocketAddress = new InetSocketAddress(inetAddress,RunningConfiguration.FILE_PORT); 
    this.file = file; 
} 

public static void send(InetSocketAddress inetSocketAddress, File file) throws IOException{ 
    FileSender nioClient = new FileSender(inetSocketAddress.getAddress(),file); 
    SocketChannel socketChannel = nioClient.createChannel(); 
    nioClient.sendFile(socketChannel); 
} 

public static void send(Node to, File file) throws IOException{ 
    FileSender nioClient = new FileSender(to.getSocketAddress().getAddress(),file); 
    SocketChannel socketChannel = nioClient.createChannel(); 
    nioClient.sendFile(socketChannel); 
} 

public static void send(Node to, File file,String filepath) throws IOException{ 
    FileSender nioClient = new FileSender(to.getSocketAddress().getAddress(),file); 
    SocketChannel socketChannel = nioClient.createChannel(); 
    nioClient.sendFile(socketChannel); 
} 

public static void send(Node to,List<File> files,String filepath) throws IOException{ 
    ListIterator ltr = files.listIterator(); 
    while(ltr.hasNext()){ 
     File file = (File) ltr.next(); 
     FileSender nioClient = new FileSender(to.getSocketAddress().getAddress(),file); 
     SocketChannel socketChannel = nioClient.createChannel(); 
     nioClient.sendFile(socketChannel); 
    } 
} 
public SocketChannel createChannel() { 
    SocketChannel socketChannel = null; 

    try { 
     socketChannel = SocketChannel.open(); 
     SocketAddress socketAddress = this.fileSocketAddress; 
     socketChannel.connect(socketAddress); 
     System.out.println("Connected..Now sending the file"); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return socketChannel; 
} 


public void sendFile(SocketChannel socketChannel) { 
    RandomAccessFile aFile = null; 
    try { 
     //File file = new File("data\\web.exe"); 
     aFile = new RandomAccessFile(this.file, "r"); 
     FileChannel inChannel = aFile.getChannel(); 
     ByteBuffer buffer = ByteBuffer.allocate(1024); 

     while (inChannel.read(buffer) > 0) { 
      buffer.flip(); 
      socketChannel.write(buffer); 
      buffer.clear(); 
     } 

     Thread.sleep(400); 
     System.out.println("End of file reached.."); 
     socketChannel.close(); 
     aFile.close(); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
} 
} 

FileReceiver.java

private String fileName; 

public FileReceiver(String fileName) { 
    this.fileName = fileName; 
} 

public static void receive(String fileName) { 
    FileReceiver nioServer = new FileReceiver(fileName); 
    SocketChannel socketChannel = nioServer.createServerSocketChannel(); 
    nioServer.readFileFromSocket(socketChannel); 
} 

public FileReceiver() { 
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
} 

public SocketChannel createServerSocketChannel() { 

    ServerSocketChannel serverSocketChannel = null; 
    SocketChannel socketChannel = null; 

    try { 
     System.out.println("File receiver listening at port: " + RunningConfiguration.FILE_PORT); 
     serverSocketChannel = ServerSocketChannel.open(); 
     serverSocketChannel.socket().bind(new InetSocketAddress(RunningConfiguration.FILE_PORT)); 
     socketChannel = serverSocketChannel.accept(); 
     System.out.println("Connection established...." + socketChannel.getRemoteAddress()); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return socketChannel; 
} 

/** 
* Reads the bytes from socket and writes to file 
* 
* @param socketChannel 
*/ 
public void readFileFromSocket(SocketChannel socketChannel) { 

    RandomAccessFile aFile = null; 
    try { 
     aFile = new RandomAccessFile(this.fileName, "rw"); 
     ByteBuffer buffer = ByteBuffer.allocate(1024); 
     FileChannel fileChannel = aFile.getChannel(); 
     while (socketChannel.read(buffer) > 0) { 
      buffer.flip(); 
      fileChannel.write(buffer); 
      buffer.clear(); 
     } 
     // Thread.sleep(1000); 
     fileChannel.close(); 
     System.out.println("End of file reached..Closing channel"); 
     socketChannel.close(); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    }/*} catch (InterruptedException e) { 
     e.printStackTrace(); 
    }*/ 
} 
+0

リモートポイント(受信者)で発生したことを確認するためにコードと受信者コードを共有できますか。 –

+0

@JorgeOmarMedraコードを追加しました。 – Arunwij

+0

"FileReceiver.java'で" Address already in use "例外が発生していますか? Exceptionの型は 'BindException'ですか? –

答えて

0

あなたのn受信者がいつ停止するかを知るために、長さ、おそらく名前をファイルの前に送ります。受信側は、正確にそのバイト数だけを読み取るようにしなければならず、最後の読み取り時にlimitを減らす必要があります。さらに、コピーループが間違っています:

while (inChannel.read(buffer) > 0) { 
    buffer.flip(); 
    socketChannel.write(buffer); 
    buffer.clear(); 
} 

これはストリームの最後に正しく機能するとは限りません。上記のように更なる改変が必要であることを再度指摘しておく。

関連する問題