0

I am trying to copy large files over a network using sockets and channels. Here is a code snippet

//sender side
FileInputStream in = new FileInputStream(inputFileName);
FileChannel fromFile = in.getChannel();
SocketChannel toSocket = sendSocket.getChannel();
fromFile.transferTo(0, 999999, toSocket );

However, in the line where I call transferTo I get NullPointerException. Any idea why? (I also enclosed it in a while loop as I've been advised, but that's just for robustness and doesn't treat that problem)

Edited by mike_2000_17: Fixed formatting

2
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by __avd
0

I am trying to copy large files over a network using sockets and channels. Here is a code snippet

//sender side
FileInputStream in = new FileInputStream(inputFileName);
FileChannel fromFile = in.getChannel();
SocketChannel toSocket = sendSocket.getChannel();
fromFile.transferTo(0, 999999, toSocket );

However, in the line where I call transferTo I get NullPointerException. Any idea why? (I also enclosed it in a while loop as I've been advised, but that's just for robustness and doesn't treat that problem)

You code is working. I think sendSocket.getChannel() method return null. Please check the status of socket channel - whether it is available and writable or not.

Edited by mike_2000_17: Fixed formatting

0

yeah, I know it should be good....but how do I do that, and how to I fix it???

0

yeah, I know it should be good....but how do I do that, and how to I fix it???

Please check your firewall settings. I think it blocks the server activities.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.