• 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 ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

SocketChannel and FileChannel

 
Greenhorn
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.

Regards,
Deepali

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.

"java.io.IOException: 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.
 
We begin by testing your absorbancy by exposing you to this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic