• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Close a Socket that is hanging on an I/O operation

 
Neha Sekhri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a multi-threaded Java Client/Server application. For each incoming client request, the server forks a thread, within which an I/O operation is performed.

My problem is that the I/O operation never seems to finish (as if server is waiting on receiving something from client side, which it never receives). As a result of which over time there are thousands of hung tcp connections on the server end, which eventually bring down the server.

Is it possible in Java to kill this hung socket forcefully somehow?

Thanks
-Neha
 
T.A. Nguyen
Ranch Hand
Posts: 36
Eclipse IDE Java ME Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use the connect(ip, timeout) method. this will allow you to set a timeout. Also use the Thread.interrupt() to interrupt any thread.

http://java.sun.com/j2se/1.4.2/docs/api/java/net/Socket.html#connect(java.net.SocketAddress,%20int)

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html#interrupt()

enjoy!
 
Neha Sekhri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
Unfortunately the client is written in C (and server in Java) and thus I cannot use this connect function.Also, since this is java forum, perhaps it not appropriate to bring the socket creation in C in to discussion here.

I did try to specify timeout on the server end (by setting the SO_TIMEOUT and SO_LINGER properties of the clientSocket, which is an object of class java.net.Socket), but it doesnot seem to work. One would think, it would be simple to do this..but doesnt look like at this stage..

I think as long as I do not have timeout thing working on server end, it would not make any sense to interrupt the thread...

-Neha
Originally posted by T.A. Nguyen:
use the connect(ip, timeout) method. this will allow you to set a timeout. Also use the Thread.interrupt() to interrupt any thread.

http://java.sun.com/j2se/1.4.2/docs/api/java/net/Socket.html#connect(java.net.SocketAddress,%20int)

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html#interrupt()

enjoy!
 
T.A. Nguyen
Ranch Hand
Posts: 36
Eclipse IDE Java ME Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I meant for you to fix your server side codes, I assumed that you are writing in Java, and the socket connection should use the connect(ip, timeout) method. this will allow you to set a timeout. Also use the Thread.interrupt() to interrupt any thread that exceed the time limit.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic