Add to Google Reader or Homepage

java.io.interruptedioexception

InterruptedIOException:

  • The Interrupted IOException is thrown when the input or output transfer has been terminated because the thread performing it was interrupted.Simply it shows us that the I/O transfer has been interrupted.
  • This type of exceptions usually occur in the network programming wherein the client tries to communicate with the server.
  • If the host is unreachable the client/Server waits for a long time and you are at the mercy of the underlying operating system to time out eventually.
  • Rather than waiting for a long time to timeout,you can also be able to interrupt the thread by using the InterruptedIOException,thereby terminating the socket connection.
  • For this purpose a method called setSoTimeout() is provided in which you can specify the value of the timeout.
  • If the waiting time exceeds the timeout value then the InterruptedIOException is thrown to indicate the sockect connection termination.

Program:

import java.io.*;
import java.net.*;
public class Server{
ServerSocket SSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Server(){}
void run()
{
try{

SSocket = new ServerSocket(5000, 10);
SSocket.setSoTimeout(10000);
System.out.println("Waiting for connection");
connection = SSocket.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
         out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
out.writeObject("Connection successful");

do{
try{
message = (String)in.readObject();
System.out.println("client>" + message);
if (message.equals("done"))
out.writeObject("done");
}
catch(ClassNotFoundException c)
{
c.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
}while(!message.equals("done"));
}
catch(InterruptedIOException e)
{
System.out.println("InterruptedIOException");
e.printStackTrace();
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
try{
in.close();
out.close();
SSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}

public static void main(String args[])
{
Server server = new Server();
while(true){
server.run();
}
}
}

The above program shown is an example of socket connection.This program acts as a server and it has a timeout value set for 10 seconds.If any client fails to communicate to the server within 10 seconds then InterruptedIOException will be thrown.This Exception will be more useful in client side.

Error:

Waiting for connection
InterruptedIOException
java.net.SocketTimeoutException: Accept timed out
        at java.net.PlainSocketImpl.socketAccept(Native Method)
        at java.net.PlainSocketImpl.accept(Unknown Source)
        at java.net.ServerSocket.implAccept(Unknown Source)
        at java.net.ServerSocket.accept(Unknown Source)
        at Server.run(Server.java:17)
        at Server.main(Server.java:65)
Exception in thread "main" java.lang.NullPointerException
        at Server.run(Server.java:51)
        at Server.main(Server.java:65)
The temp batch file is supposed to be deleted hence...
The batch file cannot be found.

Process returned 0 (0x0)   execution time : 10.177 s
Press any key to continue.


 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top