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();
     }
   }
softprbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

a_bCommented:
Once you specify the host through the command line args the program is stuck at receive where it is waiting to receive a packet. Once it receives it will continue execution.
0
softprbAuthor Commented:
Hi a_b,
Thanks for your reply.But i am only getting the output as "Checking at: /HOSTNAME" and the last output is not displaying after waiting for long time.May be The host is not able to receiving the packet, also in that case no exception is also coming.
0
a_bCommented:
I am getting the same thing.
The code is waiting at - socket.receive(packet); to recieve the packet.
Try setting a timeout to the socket, that way you will get an exception if the connection times out, but the problem is clear.
0
softprbAuthor Commented:
Is there any one who can say where is the problem in that code i.e why code is waiting at - socket.receive(packet); to recieve the packet.
0
SteinerCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.