Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Daytime Client
    primarykey
    data
    text
    <p>I need to get current time from a date time server by using following code. </p> <pre><code>import java.io.*; import java.net.*; class DaytimeClient { public static void main (String[] args) throws IOException { if ((args.length != 1)) ("Syntax: DaytimeClient &lt;host&gt;[:&lt;port&gt;]"); int idx = args[0].indexOf (":"); int port = (idx &gt; -1) ? Integer.parseInt (args[0].substring (idx + 1)) : 13; String hostName = (idx &gt; -1) ? args[0].substring (0, idx) : args[0]; InetAddress host = InetAddress.getByName (hostName); DatagramSocket socket = new DatagramSocket (); socket.setSoTimeout (5000); DatagramPacket packet = new DatagramPacket (new byte[256], 1, host, port); socket.send (packet); packet.setLength (packet.getData ().length); socket.receive (packet); socket.close (); byte[] data = packet.getData (); int length = packet.getLength (); System.out.println (new String (data, 0, length, "latin1")); } } </code></pre> <p>after compile i run this code with "java DaytimeClient 165.193.126.229".But it doesn't worked. Got the error </p> <blockquote> <p>C:\daytimeclient>java DaytimeClient 96.47.67.105 Exception in thread "main" java.net.SocketTimeoutException: Receive timed out at java.net.DualStackPlainDatagramSocketImpl.socketReceiveOrPeekData(Native Method) at java.net.DualStackPlainDatagramSocketImpl.receive0(Unknown Source) at java.net.AbstractPlainDatagramSocketImpl.receive(Unknown Source) at java.net.DatagramSocket.receive(Unknown Source) at DaytimeClient.main(DaytimeClient.java:32)</p> </blockquote> <p>Then i wrote a server as follow, </p> <pre><code>import java.io.*; import java.net.*; public class DaytimeServer { public static final int DEFAULT_PORT = 13; public static void main (String[] args) throws IOException { if (args.length &gt; 1) throw new IllegalArgumentException ("Syntax: DaytimeServer [&lt;port&gt;]"); DatagramSocket socket = new DatagramSocket (args.length == 0 ? DEFAULT_PORT : Integer.parseInt (args[0])); DatagramPacket packet = new DatagramPacket (new byte[1], 1); while (true) { socket.receive (packet); System.out.println ("Received from: " + packet.getAddress () + ":" + packet.getPort ()); byte[] outBuffer = new java.util.Date ().toString () .getBytes ("latin1"); packet.setData (outBuffer); packet.setLength (outBuffer.length); socket.send (packet); } } } </code></pre> <p>And run the first code with "java DaytimeClient localhost". Then it's work.</p> <p>I need to do my work with a remort day time server so why "java DaytimeClient 165.193.126.229" doesn't worked. </p> <p>(Reference codes got from <a href="http://www.stepwise.hk/npwiki/Tutorial/JavaUDPDaytime" rel="nofollow">http://www.stepwise.hk/npwiki/Tutorial/JavaUDPDaytime</a>)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload