You are on page 1of 5

Demo1

using System;
using System.Collections.Generic;
using System.Text;

namespace NET343.Demo.One
{
class Clock
{
//Private Members
//This is where the internal state information is kept
private int m_seconds;
private int m_minutes;
private int m_hours;

//Private method. Used to clean up cases where


//hours > 23, minutes > 59, etc

private void Rationalize()


{
m_minutes += m_seconds / 60; //Steal any seconds that add up to mins
m_seconds = m_seconds % 60; //Rationalize seconds (set to a value 0-59)
m_hours += m_minutes / 60; //Same old
m_minutes = m_minutes % 60; //Same old
m_hours = m_hours % 24;
}

//Override the default To_String

public override string ToString()


{
return String.Format("{0:00}:{1:00}:{2:00}", m_hours, m_minutes, m_seconds);
}

//Constructors

public Clock()
{
//Default ctor. Start at midnight.
m_seconds = 0;
m_minutes = 0;
m_hours = 0;
}

public Clock(int hours, int minutes, int seconds)


{
//Fully specified ctor
m_seconds = seconds;
m_minutes = minutes;
m_hours = hours;
Rationalize(); //In case I got handed bad numbers.
}
//ctor that just take hour and min only
public Clock(int hours, int minutes)
{
//We often don't bother with seconds
m_seconds = 0; //Should always set all private values in a ctor
m_minutes = minutes;
m_hours = hours;
Rationalize();
}

//Copy Ctor
public Clock(Clock c)
{
m_seconds = c.m_seconds; //I can see private because in the same Class
m_minutes = c.m_minutes;
m_hours = c.m_hours;
}

//Mutators
//The mutator method, sometimes called a "setter", is most often used in object-oriented programming, in keeping with the principle
of encapsulation. According to this principle, member variables of a class are made private to hide and protect them from other code, and
can only be modified by a public member function
//(the mutator method), which takes the desired new value as a parameter, optionally validates it, and modifies the private member
variable.
public Clock Tick() //Note, this returns a Clock object.
{
m_seconds++;
Rationalize();
return this; //Special value. Basically, return myself.
}

//Overloaded
//cont that just take seconds only
public Clock Tick(int seconds)
{
m_seconds += seconds;
Rationalize();
return this;
}

public Clock Add(Clock c)


{
m_seconds += c.m_seconds;
m_minutes += c.m_minutes;
m_hours += c.m_hours;
Rationalize();
return this;
}

}
}

Demo1 Program

using System;
using System.Collections.Generic;
using System.Text;

namespace NET343.Demo.One
{
class Program
{
static void Main(string[] args)
{
Clock c1 = new Clock(); //Default to midnight
for (int i = 0; i < 300; ++i)
{
c1.Tick();
Console.WriteLine(c1); //Should call .ToString() for me.
}

c1.Tick(1000);
Console.WriteLine("1000 sec latter: " + c1);

c1.Tick(8000); //Add 8000 Seconds


Console.WriteLine("8000 Seconds Later: " + c1);

////Here's a fun one. Let's add 6 hours, 27 minutes:

c1.Add(new Clock(6, 27));


Console.WriteLine("6 Hours, 27 Minutes Later: " + c1);

Console.WriteLine("Press Enter to Continue");


Console.ReadLine();
}
}
}

namespace EndpointSocketDemon
{
class Program
{
static void Main(string[] args)
{
IPAddress test1 = IPAddress.Parse("192.168.1.1");
IPEndPoint ie = new IPEndPoint(test1, 8000);

Console.WriteLine("IP Endpoint: {0}",ie);


Console.WriteLine("Address Family: {0}",ie.AddressFamily);
Console.WriteLine("Address: {0}", ie.Address);
Console.WriteLine("Min Port: {0}", IPEndPoint.MinPort);
Console.WriteLine("Max Port: {0}", IPEndPoint.MaxPort);
ie.Port = 80;

Console.WriteLine("Modified IP Endpoint: {0}",ie.ToString());

SocketAddress sa = ie.Serialize();
Console.WriteLine("Socket Address: {0}", sa.ToString());

IPEndPoint ieLocal = new IPEndPoint(IPAddress.Loopback, 8080);

Socket skt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

Console.WriteLine("Bytes Available: {0}", skt.Available);


Console.WriteLine("Blocking? {0}", skt.Blocking);
Console.WriteLine("Local Endpoint: {0}", skt.LocalEndPoint);

skt.Bind(ieLocal);
Console.WriteLine("Local Endpoint after bind: {0}", skt.LocalEndPoint);

Console.WriteLine("Press any key to continue...");


Console.ReadKey();
}
}
}

namespace SimpleTCPClient
{
class Program
{
//How big a buffer?
const int iBufferSize = 1024;
static void Main(string[] args)
{
//Set up a buffer, assume no more than 1kB per message
byte[] byData = new byte[iBufferSize];
int iRecd = 0; //Keep track of how many bytes recieved

//Where's my server? In this case ... local


IPEndPoint ipepServer = new IPEndPoint(IPAddress.Loopback,6666);
Socket sktServer = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
//Connect to my server
sktServer.Connect(ipepServer);
//Get some messages and print them
while((iRecd = sktServer.Receive(byData)) > 0)
{
Console.WriteLine("Message from server ({0} bytes): {1}",
iRecd, Encoding.ASCII.GetString(byData,0,iRecd));
}
//All done
sktServer.Shutdown(SocketShutdown.Both);
sktServer.Close();

//Let the user read the result


Console.WriteLine("Press any key to exit ...");
Console.ReadKey();
}
}
}

namespace SimpleTCPServer
{
class Program
{
//Maximum connectiong backlog (doesn't really matter for this demo)
const int iBacklog = 5;
static void Main(string[] args)
{
//I'm going to listen on all interfaces, port 8080
IPEndPoint ipepLocal = new IPEndPoint(IPAddress.Any, 6666);
Socket sktListen = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
//Bind to my interface
sktListen.Bind(ipepLocal);
sktListen.Listen(iBacklog); //Start listening, 5 pending connections allowed

//The following is a blocking call, will sit until incoming


Socket sktAccept = sktListen.Accept();
//Get my data (a string) transmission ready
byte[] byData = Encoding.ASCII.GetBytes("Hello from SimpleTCPServer");
//Transmit
sktAccept.Send(byData);
//Send another message
byData = Encoding.ASCII.GetBytes("Goodbye");
sktAccept.Send(byData);

//All done
sktAccept.Shutdown(SocketShutdown.Both);
sktAccept.Close();
//No need to shut down a listening socket (no connection)
sktListen.Close();
}
}
}

namespace GetHostEntryExample
{
class Program
{
static void Main(string[] args)
{
IPHostEntry ipheHost; //host entry info
//check command line args for host string
if (args.Length == 1)
{
try
{
//get the host entry, may throw an exception
ipheHost = Dns.GetHostEntry(args[0]);
Console.WriteLine("Host name: {0}", ipheHost.HostName);

foreach (string sAlias in ipheHost.Aliases)


Console.WriteLine("Alias: {0}", sAlias);

foreach (IPAddress ip in ipheHost.AddressList)


Console.WriteLine("IP: {0}", ip);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
else
Console.WriteLine("Error! Usage: GetHostEntryExample hostname");
}
}
}

namespace GetIPByHostName
{
class Program
{
static void Main(string[] args)
{

//get and display the host name of the computer


string hostName = Dns.GetHostName();
Console.WriteLine(hostName);

//use the host name to get a list of ip's


IPHostEntry ipheSelf = Dns.GetHostEntry(hostName);

//display each ip found on the host


foreach (IPAddress ip in ipheSelf.AddressList)
{
//check for the loopback ip
if (IPAddress.IsLoopback(ip))
Console.WriteLine("Loopback IP: {0}", ip.ToString());

//display any IPv4 ips on host


if(ip.AddressFamily == AddressFamily.InterNetwork)
Console.WriteLine("IPv4 Address: {0}", ip.ToString());

//display any IPv6 ips on host


if(ip.AddressFamily == AddressFamily.InterNetworkV6)
Console.WriteLine("IPv6 Address: {0}", ip.ToString());
}
}
}
}

namespace NetworkInfo
{
class Program
{
static void Main(string[] args)
{
//get an array of network interface for the local machine
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();

//display the MAC address of each network adaptor


foreach (NetworkInterface nic in adapters)
{
IPInterfaceProperties ipProp = nic.GetIPProperties();

PhysicalAddress paNic = nic.GetPhysicalAddress();


Console.Write("Adapter: {0, -30} MAC: {1, -12:X} ", nic.Name, paNic);
foreach (IPAddressInformation ipAddr in ipProp.UnicastAddresses)
Console.Write("IPv4: {0}", ipAddr.Address);
Console.WriteLine();
}
}
}
}

You might also like