Socket setSoTimeout Example

Started by VelMurugan, Jan 02, 2009, 07:07 AM

Previous topic - Next topic

VelMurugan

Socket setSoTimeout Example

Socket class setSoTimeout example. This example shows you how to use setSoTimeout method.

Socket class setSoTimeout example. public void setSoTimeout(int timeout) throws SocketException Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time.

Here is the code

/*
* @ # SetSoLinger.java
* A class repersenting use to SetSoLinger method
* of NumberFormat class in java.net package
* version 17 June 2008
* author Rose India
*/

import java.net.*;

public class SetSoTimeout {

    public static void main(String args[]) throws Exception {
        Socket socket = new Socket();

        //Connects this socket to the server.
        socket.connect(new InetSocketAddress("192.168.10.222", 8080));

        System.out.println("SO_TIMEOUT " + socket.getSoTimeout());
        socket.setSoTimeout(1);
        System.out.println("SO_TIMEOUT " + socket.getSoTimeout());
        socket.close();
    }
}


Output

SO_TIMEOUT 0
SO_TIMEOUT 1

Source : codingdiary