Question : Not showing output in eclipse 3.4.1

There is a demo program of java networking which you can find below.When i am trying to run it either in console or in eclipse 3.4.1 there is no output.I cannot find where is the problem.


import java.io.*;
   import java.net.*;

   public class GetTime {
     final private static int DAYTIME_PORT = 13;
     public static void main(String args[]) throws
             IOException {
       if (args.length == 0) {
         System.err.println("Please specify daytime host");
         System.exit(-1);
       }
       String host = args[0];
       byte message[] = new byte[256];
       InetAddress address = InetAddress.getByName(host);
       System.out.println("Checking at: " + address);
       DatagramPacket packet =
           new DatagramPacket(message, message.length,
                   address, DAYTIME_PORT);
       DatagramSocket socket = new DatagramSocket();
       socket.send(packet);
       packet =
           new DatagramPacket(message, message.length);
       socket.receive(packet);
       String time = new String(packet.getData());
       System.out.println("The time at "   + host + " is: " + time);
       socket.close();
     }
   }

Answer : Not showing output in eclipse 3.4.1

If you read the Javadoc for the Socket-Implementation it clearly says:
"This method blocks until a datagram is received."
The packet-Objekt you give to the receive method is just a databuffer which waits to be filled with data that should come from the socket but obviously nobody sends data to that socket.

You could set a timeout using the method setSoTimeout() to get an exception when no packet is received on time.
Random Solutions  
 
programming4us programming4us