java I/O blocks thread on server side
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.
i have tried Socket.setSOTimeout(int) . but this is not working out in my case. i m sending my code properly.
// 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)) > -1) {
totalLength = len;
}//end of while
} }//end of other class
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.
please help me out and tell me how my objective could be fulfilled.
thanks in advance
**
@stephen
**
ok stephen got it what u r trying to say... u r right on your statement "Well yes ... that's what you told it to do.
"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.
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
........................................
.......................................
}
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).
now i hope i have told u everything about my confusion and problem.
please help me out and thanks a lot.
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.
Well yes ... that's what you told it to do.
As I think I said in my answer to your previous question, you cannot distinguish between these cases:
- The client has no data to send for a period.
- The network is partitioned for a period.
- The client has disappeared from the network.
Another option that you have is to call Socket.setKeepAlive()
to enable TCP keepalives. This causes the a special "keepalive" handshake to be performed periodically to ensure that the TCP/IP connection is still alive. Unfortunately, it doesn't look like you can set the TCP/IP keepalive interval in Java.
EDIT
NIO won't help. When I said "cannot" above ... I meant that it is PHYSICALLY IMPOSSIBLE to do it. Here's an analogy to help you understand.
The only communication between Outer WoopWoop and the rest of the world is by letter. Once a week my friend in Outer WoopWoop (who lives alone) posts a letter to me to fill me in on the gossip. Last week I didn't receive a letter from my friend. How can I tell if:
- my friend has died,
- my friend had no gossip last week,
- the Outer WoopWoop postal workers have been on strike, or
- all of the above?
The correct answer is that I cannot tell.
精彩评论