// UDTimeClient.cs
// Synchronous UDP TIME client code example. Prepared for
// Virginia Tech ECE 4564 by Scott Midkiff (9/15/05). Contact
// midkiff@vt.edu.
// Arguments: [server [port]]
// The optional server parameter is the server hostname or IP
// address. If the server is specified, the optional port
// parameter can be used to specify the server port. The
// defaults are "localhost" and port 37.
// The client gets a 32-bit value from the server, which is
// the number of seconds since the Internet time epoch of
// midnight on January 1, 1900.
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace UDP_Time_Client{
public class UDPTimeClient{
// Define a method to convert a 32-bit unsigned integer,
// stored in a byte array, into an Int64, a 64-bit integer).
// (There may be a better way to do this with a built-in
// function, but I've not discovered it.)
static Int64 BytesToInt64(byte[] bytes4)
{
Int64 value64 = (uint)bytes4[3];
for (int i = 2; i >= 0; i--)
{
value64 = value64 << 8;
value64 = value64 | bytes4[i];
}
return (value64);
}
public static int Main(String[] args){
// Buffer for "dummy" message to server.
byte[] msg = new byte[1];
// Buffer for 32-bit response from the server
byte[] bytes = new byte[4];
try{
// By default, use localhost as the server host and port 37
// as the server port. Allow these to be altered by command
// line arguments, as noted above.
IPAddress ipAddress = IPAddress.Loopback;
Int32 sServPort = 37;
if (args.Length > 0)
{
// Optional server host is specified. First, see if we can
// parse the server name as an IP address. If not, then we
// use DNS to do a host name lookup.
if (!IPAddress.TryParse(args[0], out ipAddress))
{
IPHostEntry ipHostInfo = Dns.GetHostEntry(args[0]);
ipAddress = ipHostInfo.AddressList[0];
}
}
if (args.Length > 1)
{
// Convert IP address to dot decimal format and port to unsigned short
sServPort = Convert.ToInt32(args[1]);
}
//Create endpoint for server.
IPEndPoint remoteEP = new IPEndPoint(ipAddress, sServPort);
// Create a UDP/IP socket.
Socket clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp );
// Display the server endpoint information.
Console.WriteLine("UDP Time Client");
Console.WriteLine("Using server at: {0}", remoteEP.ToString());
// Generate a "dummy" message.
msg[0] = 0;
// Send zero-byte message to server.
clientSocket.SendTo(msg, 0, SocketFlags.None, remoteEP);
// Receive the response from the server.
EndPoint senderEP = (EndPoint) remoteEP;
int bytesRec = clientSocket.ReceiveFrom(bytes, bytes.Length, SocketFlags.None, ref senderEP);
// Convert to a 64-bit integer.
Int64 serverTime = BytesToInt64(bytes);
// Convert to time string. Start with Internet time epoch of
// January 1, 1900 at 12:00 a.m. (or time 00:00). Then add the
// difference provided by the server.
DateTime theTime = new DateTime(1900, 1, 1, 0, 0, 0);
theTime = theTime.AddSeconds(serverTime);
// Display time.
Console.Write(theTime.ToString());
// Release the socket.
clientSocket.Close();
}catch (ArgumentNullException ane){
Console.WriteLine("ArgumentNullException: {0}",ane.ToString());
}catch (SocketException se){
Console.WriteLine("SocketException: {0}",se.ToString());
}catch (Exception e){
Console.WriteLine("Unexpected exception: {0}", e.ToString());
}
// Pause.
Console.WriteLine("\npress ENTER to continue...");
Console.ReadLine();
return 0;
}
}
}