Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set timeout for socket receive

I want to send data to server, then wait for an answer for one minute and then close the socket.

How to do it?

 DatagramPacket sendpack = new ......;
 socket.send(pack);
 DatagramPacket recievepack = new .....;
 //wait 1 minute{
 socket.recieve(buf);
 //wait 1 minute}
 socket.close();
like image 1000
Артём Царионов Avatar asked Apr 07 '12 15:04

Артём Царионов


People also ask

How do I set a socket timeout?

You can make an instance of a socket object and call a gettimeout() method to get the default timeout value and the settimeout() method to set a specific timeout value.

How do you set a timeout on a socket receiving data in Python?

The typical approach is to use select() to wait until data is available or until the timeout occurs. Only call recv() when data is actually available. To be safe, we also set the socket to non-blocking mode to guarantee that recv() will never block indefinitely.

What is the socket timeout?

socket timeout — a maximum time of inactivity between two data packets when exchanging data with a server.

What is the default socket timeout Python?

A new Python socket by default doesn't have a timeout. Its timeout defaults to None. Not setting the connection timeout parameter can result in blocking socket mode. In blocking mode, operations block until complete or the system returns an error.


2 Answers

You can try this. Change the timeout of the socket as required in your scenario! This code will send a message and then wait to receive messages until the timeout is reached!

DatagramSocket s;

    try {
        s = new DatagramSocket();
        byte[] buf = new byte[1000];
        DatagramPacket dp = new DatagramPacket(buf, buf.length);
        InetAddress hostAddress = InetAddress.getByName("localhost");

        String outString = "Say hi";        // message to send
        buf = outString.getBytes();

        DatagramPacket out = new DatagramPacket(buf, buf.length, hostAddress, 9999);
        s.send(out);        // send to the server

        s.setSoTimeout(1000);   // set the timeout in millisecounds.

        while(true){        // recieve data until timeout
            try {
                s.receive(dp);
                String rcvd = "rcvd from " + dp.getAddress() + ", " + dp.getPort() + ": "+ new String(dp.getData(), 0, dp.getLength());
                System.out.println(rcvd);
            }
            catch (SocketTimeoutException e) {
                // timeout exception.
                System.out.println("Timeout reached!!! " + e);
                s.close();
            }
        }

    } catch (SocketException e1) {
        // TODO Auto-generated catch block
        //e1.printStackTrace();
        System.out.println("Socket closed " + e1);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
like image 178
Future2020 Avatar answered Oct 15 '22 14:10

Future2020


If you are using DatagramSocket, or Socket you can use,

socket.setSoTimeout(1000); 
//the value is in milliseconds

For any detail, you should've taken a look in DatagramSocket javadoc or Socket javadoc.

like image 26
shibli049 Avatar answered Oct 15 '22 13:10

shibli049