Socket setKeepAlive Example

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

Previous topic - Next topic

VelMurugan

Socket setKeepAlive Example

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

Socket class setKeepAlive example. blic void setKeepAlive(boolean on) throws SocketException Enable/disable SO_KEEPALIVE.

Here is the code

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

import java.net.*;

public class SetKeepAlive {

    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));

        if (!socket.getKeepAlive()) {
            socket.setKeepAlive(true);
        }
        System.out.println("SO_KEEPALIVE is enabled. " + socket.getKeepAlive());

        socket.close();
    }
}


Output

SO_KEEPALIVE is enabled. true

Source : codingdiary