Solved

Not showing output in eclipse 3.4.1

Posted on 2009-07-14
5
823 Views
Last Modified: 2013-12-15
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();
     }
   }
0
Comment
Question by:softprb
  • 2
  • 2
5 Comments
 
LVL 20

Expert Comment

by:a_b
ID: 24856590
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
 

Author Comment

by:softprb
ID: 24856617
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
 
LVL 20

Expert Comment

by:a_b
ID: 24856625
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
 

Author Comment

by:softprb
ID: 24856713
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
 
LVL 11

Accepted Solution

by:
Steiner earned 125 total points
ID: 24858467
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Eclipse variable value view scroll 3 154
Netbeans Open project initial dir 5 42
Java Help 2 78
Support for Notepad++ (including downloading & installing a plugin) 5 59
In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now