UDP Daytime implementation using Java - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

UDP Daytime implementation using Java

Share This
DayTimeServer.java
package net.udp.daytime; import java.net.*; import java.io.*; public abstract class DayTimeServer extends Thread { private int bufferSize; protected DatagramSocket ds; public DayTimeServer(int port, int bufferSize) throws SocketException { this.bufferSize = bufferSize; this.ds = new DatagramSocket(port); } public DayTimeServer(int port) throws SocketException { this(port, 8192); } public void run() { byte[] buffer = new byte[bufferSize]; while (true) { DatagramPacket incoming = new DatagramPacket(buffer, buffer.length); try { ds.receive(incoming); this.respond(incoming); } catch (IOException e) { System.err.println(e); } } } public abstract void respond(DatagramPacket request); }

DayTimeClient.java
package net.udp.daytime; import java.net.InetAddress; import java.util.Date; public class DayTimeClient { public final static int DEFAULT_PORT = 37; public final static String DEFAULT_HOST = "localhost"; public static void main(String[] args) { InetAddress host; int port = DEFAULT_PORT; try { if (args.length > 0) { host = InetAddress.getByName(args[0]); } else { host = InetAddress.getByName(DEFAULT_HOST); } } catch (Exception e) { System.out.println("Usage: java UDPTimeClient host port"); return; } if (args.length > 1) { try { port = Integer.parseInt(args[1]); if (port <= 0 || port > 65535) port = DEFAULT_PORT; ; } catch (Exception e) { } } try { UDPPoke poker = new UDPPoke(host, port); byte[] response = poker.poke(); if (response == null) { System.out.println("No response within allotted time"); return; } else if (response.length != 4) { System.out.println("Unrecognized response format"); return; } // The time protocol sets the epoch at 1900, // the java Date class at 1970. This number // converts between them. long differenceBetweenEpochs = 2208988800L; long secondsSince1900 = 0; for (int i = 0; i < 4; i++) { secondsSince1900 = (secondsSince1900 << 8) | (response[i] & 0x000000FF); } long secondsSince1970 = secondsSince1900 - differenceBetweenEpochs; long msSince1970 = secondsSince1970 * 1000; Date time = new Date(msSince1970); System.out.println(time); } catch (Exception e) { System.err.println(e); e.printStackTrace(); } } }

UDPPoke.java
import java.net.*; import java.io.*; public class UDPPoke { private int bufferSize; private DatagramSocket ds; private DatagramPacket outgoing; public UDPPoke(InetAddress host, int port, int bufferSize, int timeout) throws SocketException { outgoing = new DatagramPacket(new byte[1], 1, host, port); this.bufferSize = bufferSize; ds = new DatagramSocket(0); ds.connect(host, port); ds.setSoTimeout(timeout); } public UDPPoke(InetAddress host, int port, int bufferSize) throws SocketException { this(host, port, bufferSize, 30000); } public UDPPoke(InetAddress host, int port) throws SocketException { this(host, port, 8192, 30000); } public byte[] poke() throws IOException { byte[] response = null; try { ds.send(outgoing); DatagramPacket incoming = new DatagramPacket(new byte[bufferSize], bufferSize); ds.receive(incoming); int numBytes = incoming.getLength(); response = new byte[numBytes]; System.arraycopy(incoming.getData(), 0, response, 0, numBytes); } catch (IOException e) { } return response; } }

Happy Exploring!

No comments:

Post a Comment