java I/O blocks thread on server side
<p>i m working on client socket connection. client is a GPRS hardware device. i m receiving request from this client on my serversocket and then opening multiple threads. my problem is that when device/client close the socket then my IO detects that throws an exception but when i put off the battery from the device while sending the request to the serversocket it is blocked without throwing any exception. somebody suggested me to use setSOTimeout() to comeout from the blocking thread.</p>

<p>i have tried Socket.setSOTimeout(int) . but this is not working out in my case. i m sending my code properly. </p>

<pre><code>//  inside main class--- 

    public class ServerSocketClass{
    public ServerSocketClass() {
    try{
        serverSocket = new ServerSocket(port);//creating a serversokcet on a port
    System.out.println("Server waiting for client on port " + 
    serverSocket.getLocalPort());
    } catch (Exception e) {
        e.printStackTrace();
    handleExceptions("errorServerSocketOpen.txt", e);
    System.exit(0);
}
try {
    while (true) {
        Socket clientSocket = serverSocket.accept();//accepting the client connection and //creating client socket

        new AcceptConnection(clientSocket);//calling the constructor of other //class to open a new thread
        System.out.println("constructor called.....");
    }
} catch (Exception e) {
    handleExceptions("errorClientSocketOpen.txt", e);
   }
  }
 }
//inside other class---


public class AcceptConnection implements Runnable {
    public AcceptConnection(Socket socket) {
    this.clientSocket = socket;
    if (clientSocket != null) {
        new Thread(this).start();
    }
    public void run() {
        // clientSocket.setSoTimeout(60000);// this is what i added.timeout on each client socket opened in threads
        InputStream inputStream = clientSocket.getInputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(clientSocket.getOutputStream());
        byte[] mainBuffer = new byte[2048];
        int len = -1, totalLength = 0;
        debugInfo = " GOING TO READ FROM SOCKET.... " + "\n";
        while ((len = inputStream.read(mainBuffer)) &gt; -1) {
            totalLength = len;
        }//end of while
    } }//end of other class
</code></pre>

<p>now my problem is that when multiple threads are opened and i send the data from client than after 60 seconds it closes the main socket and stops receiving the data.and a readtimeout error occurs.</p>

<p>please help me out and tell me how my objective could be fulfilled.</p>

<p>thanks in advance</p>

<p>**</p>

<blockquote>
  <p>@stephen</p>
</blockquote>

<p>** 
ok stephen got it what u r trying to say... u r right on your statement "<code>Well yes ... that's what you told it to do.</code>"May be i m not able to make u understand my problem or i m not getting the setSoTimeout() logic as i m newbie in java.
i would like to ask one more time...
this is how i m creating a new client socket for each client connection and opening a new thread for each client connection.</p>

<pre><code>    while (true) {
            Socket clientSocket = serverSocket.accept();//accepting the client connection and //creating client socket
                new AcceptConnection(clientSocket);//calling the constructor of other class to open a new thread
            System.out.println("constructor called.....");
        }
public void run() {
            // clientSocket.setSoTimeout(60000);// this is what i added.timeout on each uclient socket opened in threads
........................................
.......................................
}
</code></pre>

<p>now i want to say if i m opening a new thread for a new client connection and i m separately putting setSoTimeout() on each client connection object then if a particular thread A is blocked on I/O while reading then after a timeout set in setSoTimeout(50000) ,say 50 sec ,only thread A should come out of read and give the exception, not other threads running simultaneously say B,C,D.but in my case after a timeout all threads returns after giving exception and in fact any new client connection gives the same error and server application stops receiving any data on read.
i want only thread A should give exception and come out from read without affecting other client socket objects(or threads).</p>

<p>now i hope i have told u everything about my confusion and problem.</p>

<p>please help me out and thanks a lot.</p>
