title: How to set a timeout in a UDP client for when a packet is not sent from server?
body: <p>I am writing a simple program about UDP socket programming. I am using datagram sockets. I have to send packet from the client to the server. Then the server decides randomly if to send a packet back. The client has to accept the packet if sent or wait 2 seconds and assume the packet is lost. I cannot handle the case of the packet lost. </p>
  
  <pre><code>    System.out.println("Receiving message...");
      dsock.receive(dpack); // receive the packet
      System.out.println("Message received");
  </code></pre>
  
  <p>It works all fine if the packet is sent but how can I handle a situation when a packet is not sent and I still have this line of code existing?</p>