Example usage for java.net DatagramSocket getSoTimeout

List of usage examples for java.net DatagramSocket getSoTimeout

Introduction

In this page you can find the example usage for java.net DatagramSocket getSoTimeout.

Prototype

public synchronized int getSoTimeout() throws SocketException 

Source Link

Document

Retrieve setting for SO_TIMEOUT.

Usage

From source file:Main.java

public static void main(String args[]) {
    try {//from  w w w . j a  v  a2  s. c o  m

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket();

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);

        // Send the datagram packet
        ds.send(dp);

        System.out.println(ds.getSoTimeout());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.mule.transport.udp.UdpSocketFactory.java

public Object makeObject(Object key) throws Exception {
    ImmutableEndpoint ep = (ImmutableEndpoint) key;
    DatagramSocket socket;

    if (ep instanceof InboundEndpoint) {
        int port = ep.getEndpointURI().getPort();
        String host = ep.getEndpointURI().getHost();
        if (port > 0) {
            if ("null".equalsIgnoreCase(host)) {
                socket = createSocket(port);
            } else {
                socket = createSocket(port, InetAddress.getByName(host));
            }/*  w  w  w  .j ava2  s .  com*/
        } else {
            socket = createSocket();
        }
    } else {
        //If this is a client socket create a default instance
        socket = createSocket();
    }

    UdpConnector connector = (UdpConnector) ep.getConnector();
    //There is some overhead in stting socket timeout and buffer size, so we're
    //careful here only to set if needed
    if (connector.getReceiveBufferSize() != Connector.INT_VALUE_NOT_SET
            && socket.getReceiveBufferSize() != connector.getReceiveBufferSize()) {
        socket.setReceiveBufferSize(connector.getReceiveBufferSize());
    }
    if (connector.getSendBufferSize() != Connector.INT_VALUE_NOT_SET
            && socket.getSendBufferSize() != connector.getSendBufferSize()) {
        socket.setSendBufferSize(connector.getSendBufferSize());
    }
    if (connector.getTimeout() != Connector.INT_VALUE_NOT_SET
            && socket.getSoTimeout() != connector.getTimeout()) {
        socket.setSoTimeout(connector.getTimeout());
    }
    socket.setBroadcast(connector.isBroadcast());
    return socket;
}