• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 840
  • Last Modified:

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");
       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();
       packet =
           new DatagramPacket(message, message.length);
       String time = new String(packet.getData());
       System.out.println("The time at "   + host + " is: " + time);
  • 2
  • 2
1 Solution
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.
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.
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.
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.
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now