for this you have to write two applications, one will reside in the server machine and will wait till a client machine request its time. upon a succesful request the server will write its DateTime.Now value to the network stream, where the client can read it out and use it in his application
please consider that these two applications are windows console applications
Code for the Server Program
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Server
{
public class Program
{
public static void Main(string[] args)
{
try
{
//add a listner to liten on a port on a specified host
TcpListener listener=new TcpListener(Dns.Resolve("localhost").AddressList[0],14);
listener.Start();
Console.WriteLine("Waiting for client to connect");
Console.WriteLine("Ctrl+C to quit");
while (true)
{
Socket s = listener.AcceptSocket(); //accept a connection request made by a client on the listning socket
DateTime now = DateTime.Now; //get the servers date time value
string dateTime = now.ToShortDateString() + " " + now.ToLongTimeString();
byte[] asciiDateTime = Encoding.ASCII.GetBytes(dateTime.ToCharArray());
//encode the strring to aASCII byte stream to written in to the socket so it can be decoded from the client easily
s.Send(asciiDateTime, asciiDateTime.Length, 0);
//write to the socket data(asciidatetime) starting from 0th index to a charcter count of(asciidatetime.length)
s.Close(); //close the socket connection to the client
Console.WriteLine("Sent{0}",dateTime);
}
}
catch (SocketException error)
{
if (error.ErrorCode==10048)
{
Console.WriteLine("connection failed to this port, there is another server listning on this port");
}
}
}
}
}
Code for the Client Program
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
TcpClient client = new TcpClient(); //create a client
Console.WriteLine("specify a server name : ");
string serverName = Console.ReadLine();
try
{
//gets the DNS information of the specified server
IPHostEntry ipInfo = Dns.GetHostByName(serverName);
}
catch (SocketException error)
{
Console.WriteLine("cannot find server : "+serverName+"\nException :\n"+error.ToString());
return;
}
try
{
client.Connect(serverName,14); //connect to the specified server via port 14
}
catch (Exception error2)
{
Console.WriteLine("could not connect to server {0}\n"+error2.Message,serverName);
}
Stream s;
try
{
//get a network stream to communicate with the server machine
this will add the client to the servers pending request query and will
s = client.GetStream(); initiate the AcceptSocket() method
}
catch (InvalidOperationException)
{
Console.WriteLine("cannot connect to server {0}",serverName);
return;
}
Byte[] read=new byte[32];
//read the the byte stream returned from the server
int bytes = s.Read(read, 0, read.Length);
string time = Encoding.ASCII.GetString(read); //decode it to a string
Console.WriteLine("received"+bytes+"bytes");
Console.WriteLine("current server date time is : "+time);
client.Close();
Console.WriteLine("press enter to exit");
Console.Read();
}
}
}
this code base can be used to construct any simple-client server communication machanism based on your requirements. all you have to do is replace
DateTime now = DateTime.Now; //get the servers date time value
string dateTime = now.ToShortDateString() + " " + now.ToLongTimeString();
part in the server program with ou server task
have fun coding!!!!!!