DatagramSocket setSoTimeout - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for DatagramSocket setSoTimeout method.

This code example shows how to use the following methods:connect, setSoTimeout
public class RtpSocket { 
  private final byte [] buf = new byte[4096];
  private DatagramSocket socket;
 
  public RtpSocket(int localPort, InetSocketAddress remoteAddress) throws SocketException {
    socket = new DatagramSocket(localPort);
    socket.setSoTimeout(1);
    socket.connect(new InetSocketAddress(remoteAddress.getAddress().getHostAddress(), remoteAddress.getPort()));
    Log.d( "RtpSocket", "Connected to: " + remoteAddress.getAddress().getHostAddress() );
  } 
 
  public void setTimeout(int timeoutMillis) {
    try { 
      socket.setSoTimeout(timeoutMillis);
    } catch (SocketException e) {
      Log.w("RtpSocket", e);
    } 
  } 
 
  private long totalSendTime = 0;
8
CodeRank
This code example shows how to use the following methods:close, connect, receive, send, setSoTimeout
        } catch (IllegalArgumentException e) {
            // jndi.40=Unable to connect: bad IP address 
            throw new DomainProtocolException(Messages.getString("jndi.40")); //$NON-NLS-1$ 
        } 
        try { 
            dSocket = new DatagramSocket();
            srvAddr = InetAddress.getByAddress(srvAddrArr);
            dSocket.connect(srvAddr, serverPort);
            outPacket = new DatagramPacket(outBuf, outBufLen, srvAddr,
                    serverPort);
            dSocket.setSoTimeout(timeout);
            dSocket.send(outPacket);
            inPacket = new DatagramPacket(inBuf, inBufLen, srvAddr, serverPort);
            dSocket.receive(inPacket);
        } catch (IllegalStateException e) {
            // jndi.41=Error while querying DNS server 
            throw new DomainProtocolException(Messages.getString("jndi.41"), e); //$NON-NLS-1$
        } catch (SocketTimeoutException e) {
            throw (e);
        } catch (IOException e) {

6
CodeRank
This code example shows how to use the following methods:close, receive, setSoTimeout
import java.net.SocketTimeoutException; 
 
public class DatagramTimeout { 
    public static void main(String[] args) throws Exception {
        boolean success = false;
        DatagramSocket sock = new DatagramSocket();
 
        try { 
            DatagramPacket p;
            byte[] buffer = new byte[50];
            p = new DatagramPacket(buffer, buffer.length);
            sock.setSoTimeout(2);
            sock.receive(p);
        } catch (SocketTimeoutException e) {
            success = true;
        } finally { 
            sock.close();
        } 
        if (!success)
            throw new RuntimeException("Socket timeout failure.");

7
CodeRank
This code example shows how to use the following methods:receive, setSoTimeout
    } 
 
    public int receive(byte[] buf, int off, int len, int waitMillis)
        throws IOException 
    { 
        socket.setSoTimeout(waitMillis);
        DatagramPacket packet = new DatagramPacket(buf, off, len);
        socket.receive(packet);
        return packet.getLength();
    } 
 
    public void send(byte[] buf, int off, int len)
        throws IOException 
    { 
        if (len > getSendLimit())
        { 
            /* 
             * RFC 4347 4.1.1. "If the application attempts to send a record larger than the MTU, 
             * the DTLS implementation SHOULD generate an error, thus avoiding sending a packet 
             * which will be fragmented." 
7
CodeRank
This code example shows how to use the following methods:close, receive, send, setSoTimeout
     * @return true if the transaction was successful. 
     */ 
    public boolean requestTime(String host, int timeout) {
        DatagramSocket socket = null;
        try { 
            socket = new DatagramSocket();
            socket.setSoTimeout(timeout);
            InetAddress address = InetAddress.getByName(host);
            byte[] buffer = new byte[NTP_PACKET_SIZE];
            DatagramPacket request = new DatagramPacket(buffer, buffer.length, address, NTP_PORT);
 
            // set mode = 3 (client) and version = 3 
            // mode is in low 3 bits of first byte 
            // version is in bits 3-5 of first byte 
            buffer[0] = NTP_MODE_CLIENT | (NTP_VERSION << 3);
 
            // get current time and write it to the request packet 
            long requestTime = System.currentTimeMillis();
            long requestTicks = SystemClock.elapsedRealtime();
            writeTimeStamp(buffer, TRANSMIT_TIME_OFFSET, requestTime);

6
CodeRank
This code example shows how to use the following methods:close, receive, setSoTimeout
 
	protected InetAddress listenForDevice() {
		try { 
			nAddr = InetAddress.getLocalHost();
			byte[] buf = new byte[1024];
			DatagramSocket socket = new DatagramSocket(DISCOVERY_PORT);
			socket.setSoTimeout(5000);
			DatagramPacket packet = new DatagramPacket(buf, buf.length);
			socket.receive(packet);
			socket.close();
			return packet.getAddress();
		} catch (Exception e) {
			return nAddr;
		} 
	} 
 
	protected InetAddress probeForDevice() {
		try { 
			DatagramSocket socket = new DatagramSocket(DISCOVERY_PORT);
			socket.setBroadcast(true);
6
CodeRank
This code example shows how to use the following methods:sleep, close, send, setReceiveBufferSize, setSendBufferSize
        zenPingA.start();
 
        MulticastSocket multicastSocket = null;
        try { 
            Loggers.getLogger(MulticastZenPing.class).setLevel("TRACE"); 
            multicastSocket = new MulticastSocket(54328);
            multicastSocket.setReceiveBufferSize(2048);
            multicastSocket.setSendBufferSize(2048);
            multicastSocket.setSoTimeout(60000);
 
            DatagramPacket datagramPacket = new DatagramPacket(new byte[2048], 2048, InetAddress.getByName("224.2.2.4"), 54328);
            XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("request").field("cluster_name", "test").endObject().endObject();
            datagramPacket.setData(builder.bytes().toBytes());
            multicastSocket.send(datagramPacket);
            Thread.sleep(100);
        } finally { 
            Loggers.getLogger(MulticastZenPing.class).setLevel("INFO"); 
            if (multicastSocket != null) multicastSocket.close();
            zenPingA.close();
            threadPool.shutdown();

6
CodeRank
This code example shows how to use the following methods:close, receive, send, setSoTimeout
     * @return true if the transaction was successful. 
     */ 
    public boolean requestTime(String host, int timeout) {
        DatagramSocket socket = null;
        try { 
            socket = new DatagramSocket();
            socket.setSoTimeout(timeout);
            InetAddress address = InetAddress.getByName(host);
 
			byte[] buffer = new byte[NTP_PACKET_SIZE];
            DatagramPacket request = new DatagramPacket(buffer, buffer.length, address, NTP_PORT);
 
            // set mode = 3 (client) and version = 3 
            // mode is in low 3 bits of first byte 
            // version is in bits 3-5 of first byte 
            buffer[0] = NTP_MODE_CLIENT | (NTP_VERSION << 3);
 
            // get current time and write it to the request packet 
            long requestTime = System.currentTimeMillis();
            long requestTicks = SystemClock.elapsedRealtime();

6
CodeRank
This code example shows how to use the following methods:close, receive, setSoTimeout
    public void testDatagramSocketSetSOTimeout() throws Exception { 
        DatagramSocket sock = null;
        int timeout = 5000;
        long start = System.currentTimeMillis();
        try { 
            sock = new DatagramSocket();
            DatagramPacket pack = new DatagramPacket(new byte[100], 100);
            sock.setSoTimeout(timeout);
            sock.receive(pack);
        } catch (SocketTimeoutException e) {
            // expected 
            long delay = System.currentTimeMillis() - start;
            if (Math.abs(delay - timeout) > 1000) {
                fail("timeout was not accurate. expected: " + timeout
                        + " actual: " + delay + " miliseconds.");
            } 
        } finally { 
            if (sock != null) {
                sock.close();
            } 

7
CodeRank
This code example shows how to use the following methods:close, receive, send, setSoTimeout
     * @param timeout network timeout in milliseconds. 
     * @return true if the transaction was successful. 
     */ 
    public boolean requestTime(String host, int timeout) {
        try { 
            DatagramSocket socket = new DatagramSocket();
            socket.setSoTimeout(timeout);
            InetAddress address = InetAddress.getByName(host);
            byte[] buffer = new byte[NTP_PACKET_SIZE];
            DatagramPacket request = new DatagramPacket(buffer, buffer.length, address, NTP_PORT);
 
            // set mode = 3 (client) and version = 3 
            // mode is in low 3 bits of first byte 
            // version is in bits 3-5 of first byte 
            buffer[0] = NTP_MODE_CLIENT | (NTP_VERSION << 3);
 
            // get current time and write it to the request packet 
            long requestTime = System.currentTimeMillis();
            long requestTicks = SystemClock.elapsedRealtime();
            writeTimeStamp(buffer, TRANSMIT_TIME_OFFSET, requestTime);

6
CodeRank

Related DatagramSocket setSoTimeout Questions & Answers:

setSoTimeout Method Overview
Sets the read timeout in milliseconds for this socket.
See Code Examples for other Java 8 DatagramSocket Methods: