• Post Reply Bookmark Topic Watch Topic
  • New Topic

tranfering big files (over a network)

 
glup klosar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't want to use Buffers in order to avoid overflows.
Let me show you the receiver side. I have an InputStream coming from the socket, and a FileOutputStream that will write on the file. I want to do that writing with a channel, because it has a lot of space. However, there's no way for me to connect this channel to the InputStream connected to the socket. (though I can connect it to the FileOutputStream)
Any suggestions? I'm really grateful, this means a lot to me.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get a channel to read from a socket, your best bet is to go to the Socket object and call getChannel(). Likewise, FileOutputStream has a getChannel() too. And the latter returns a FileChannel, which has the particularly useful methods transferTo() and transferFrom(). Note that you should use this method in a loop, to ensure that you have read all the bytes you can.
 
glup klosar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please tell me how that loop might look like? why isn's a single call of socket_channel.transferTo(0, 999999999999, output_channel) for example enough?

Thanks a lot
 
glup klosar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK..I know the number should be smaller...but apart from that, I am also getting null point exception at that line Here is how the code looks like

FileInputStream in = new FileInputStream(inputFileName);
FileChannel fromFile = in.getChannel();
SocketChannel toSocket = sendSocket.getChannel();
fromFile.transferTo(0, 999, toSocket );

What do you guys think?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the NullPointerException, you need to look at exactly which line is throwing the exception. Can you show us the stack trace?

gk wrote:why isn's a single call of socket_channel.transferTo(0, 999999999999, output_channel) for example enough?

Because the API is written so as to allow the system to deliver fewer bytes than were requested. This might be because the network is busy, or because not all the bytes have been sent yet. Maybe we're waiting for a database to complete a complex query, or maybe there's a human at the other end, slowly typing into a terminal. The API is written to allow you to process all the bytes that are available right now, and then decide if you want the computer to do something else while waiting for more data to become available.

For the loop, well the main problem is knowing when to stop. For a socket, I think you may need to keep looping as long as the channel is open - you never know when they might send more data, until it's closed. You could supplement this with a timeout as well:
 
glup klosar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that's not the problem, such factors are not involved. I simply run the receive code on one machine, it starts listening, then I go to another machine and run the send code. When I used buffers (and smaller files), it worked. Here is the code (sender side), you will get the exact idea what'shappening :
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you've gone from allegedly having input from a socket and output to a file, to the reverse. So yes, the details are different now. Also I should note that I haven't tested the recommendations out carefully - I'm just looking at the docs and suggesting possibilities based on what I see there. So I may have committed errors along the way. Beware. But there may be value in these suggestions nonetheless.


The only way that line could throw NullPointerException is if toSocket is null. Look back to where toSocket came from. What method was used to assign it a value? Look at the JavaDoc for that method. It contains an important clue.
 
glup klosar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
I do believe you refer to the sentence "Returns : the socket channel associated with this socket, or null if this socket was not created for a channel" of the getChannel() method, but how do I make sure the socket is created FOR a channel?
 
glup klosar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone has a clue....
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I was referring to the documentation for the getChannel() method on Socket. It sounds like you have only read the short summary of the method, found at the top of the page. Notice how all the methods in this area are blue - they're links. If you click on one of them, you are taken to a longer description of the method, found lower on the page.
 
glup klosar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the problem is that I should have said sc = SocketChannel.open(), where sc is the name of the socket channel, and then connect it to a socket. socket.getChannel() just returns a channel if it has already been connected to the socket, not as I believed, that each socket already has its own channel. Now I have connection problems, and I think it's due to the firewall...maybe it's better if we discuss that in another subforum.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!