Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

SocketChannel and FileChannel

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Arthur, where are your pants? Check under this tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic