Solved

Not showing output in eclipse 3.4.1

Posted on 2009-07-14
5
822 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

747 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