Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

SocketChannel and FileChannel  RSS feed

Deepali Mendhekar
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi experts,

I am using nio package to transfer files from client to server. I use jdk1.4.2 on the client side (because have not upgraded our client code to new jre releases) and jdk1.5 on server side (because we use tomcat as webserver and latest tomcat is much stable and scalable but needs jre1.5). I use SocketChannel along with FileChannel to transfer the content of file to the server.

Client code snippet:
long bytesRead = fileChannel.transferTo(bytesTransferred, bufferSize, socketChannel);

Server code snippet:
long readBytes = fileChannel.transferFrom(socketChannel, 0, m_fileSize);

My problem is that all goes well unless the client machine crashes or the internet goes down, resulting in socketChannel on the client side not getting closed gracefully and hence no exception is thrown on the server sode! So, the thread on the server side that is trying to read from the socketChannel never dies (unless ofcourse webserver is restarted). I am sure I am missing something here, not sure what?

Please look at the following code and let me know what is wrong here! I appreciate you help and thank you for your time.


PS: I have tried to set timeout on socket and that didn't solve my problem and this is my first post so please be gentle, I know it is a big post and my problem could be silly!

Client code:

Server code:

Martin Mathis
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was under the impression that if your client were to die and you try to do anything with it, an exception is thrown on the SocketChannel on the other end.

" An existing connection was forcibly closed by the remote host"

is the Exception that I would normally get in such a scenario. So I don't think the fact that the client dies should be a problem.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!