Professional Documents
Culture Documents
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;
//Constructors
public Clock()
{
//Default ctor. Start at midnight.
m_seconds = 0;
m_minutes = 0;
m_hours = 0;
}
//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;
}
}
}
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);
namespace EndpointSocketDemon
{
class Program
{
static void Main(string[] args)
{
IPAddress test1 = IPAddress.Parse("192.168.1.1");
IPEndPoint ie = new IPEndPoint(test1, 8000);
SocketAddress sa = ie.Serialize();
Console.WriteLine("Socket Address: {0}", sa.ToString());
skt.Bind(ieLocal);
Console.WriteLine("Local Endpoint after bind: {0}", skt.LocalEndPoint);
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
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
//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);
namespace GetIPByHostName
{
class Program
{
static void Main(string[] args)
{
namespace NetworkInfo
{
class Program
{
static void Main(string[] args)
{
//get an array of network interface for the local machine
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();