?
Solved

Not showing output in eclipse 3.4.1

Posted on 2009-07-14
5
Medium Priority
?
830 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 375 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

771 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