2

I just learnt about the java nio package and channels and now, I tried to write a really simple file transfer program using channels. My aim was to get rid of all this bytewise reading stuff. As a first try, I wrote the following server code:

public class Server {
    public static void main(String args[]) throws FileNotFoundException, IOException {
        String destination = "D:\tmp\received"; 
        int port = 9999; 
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress(9999)); 
        SocketChannel socketChannel = serverSocketChannel.accept(); 
        FileChannel fileChannel = new FileOutputStream(destination).getChannel();
        fileChannel.transferFrom(socketChannel, 0, 32); 
        socketChannel.close();
        serverSocketChannel.close();
    }
}

and the following client code:

public class Client {
    public static void main(String args[]) throws FileNotFoundException, IOException {
        String fileName = "D:\\dump\\file";
        InetSocketAddress serverAddress = new InetSocketAddress("localhost", 9999); 
        FileChannel fileChannel = new FileInputStream(fileName).getChannel();
        SocketChannel socketChannel = SocketChannel.open(serverAddress); 
        fileChannel.transferTo(0, fileChannel.size(), socketChannel); 
        socketChannel.close();
        fileChannel.close();
    }
}

to transfer a predefined 32-byte file over a predefined port without any kind of error handling and stuff.

This program compiles and runs without any errors, but at the end the destination file ("received") is not written.

Is it possible to transfer a file with this technique or did I misunderstood anything? Can you tell me what I did wrong in the above code? After some research, I have not found any solution yet, but just find code snippets that use this bytewise stuff (like: while(there is data) { read 32 byte and write them into file }).

Green绿色
  • 1,620
  • 1
  • 16
  • 43
  • Define "is not written". Is the file short? Empty? Created? – user207421 Oct 31 '15 at 17:16
  • A 32-bit file is 4 bytes, but you are trying to read a fixed 32 bytes regardless of the size of the file. It is usually better to send the length with the content so the receiver knows how much to read. – Peter Lawrey Oct 31 '15 at 18:41
  • @EJP "is not written" means there is no file created. The file should be created in the folder D:\tmp, but the folder remains empty after the execution. – Green绿色 Nov 01 '15 at 06:07
  • @PeterLawrey Sorry, I meant 32 byte. I know that it's better to send the length, but this test code should be as minimal as possible. So I hard-coded the file size. In the file to be sent, I wrote 32 ASCII characters, so its size is 32 byte. – Green绿色 Nov 01 '15 at 06:09

1 Answers1

1
"D:\tmp\received"

Change these backslashes to forward slashes. This is either an illegal file name, which would have thrown an exception you should have noticed, or at least it is not a filename equal to the one you think you're writing.

You also need to call these transfer methods in a loop until they have transferred everything you're expecting. That's why they have a return value. Check the Javadoc.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thank you. I've overlooked that completely. I replaced them with forward slashes and now it works as expected. One short question: How can I mark questions as 'solved' at StackOverflow? – Green绿色 Nov 01 '15 at 08:32
  • Just accept and/or upvote the answer you found most useful, if any. – user207421 Nov 01 '15 at 09:02