Learn C #

xiaoxiao2021-03-06  21

TCP / IP network application ****************************************************** ******************** Get this name and IP address ******************************** ************************************************************** USING SYSTEM; Using System .NET;

public class IPTest {public static int Main () {string strHostName; strHostName = Dns.GetHostName (); Console.WriteLine ( "native name:" strHostName); IPHostEntry ipEntry = Dns.GetHostByName (strHostName); IPAddress [] addr = IpenTry.addressList; String [] str = ipenTry.aliaseS; for (int i = 0; i

*********************************************************** ****************** TCP network time service application ********************************** ***************************************** / / server: provide time Service Using System.net.Sockets; use system.net; using system.threading; using system.text;

Public class dateserver {private tcplistener mylistener; private int port = 4554;

Public dateServer () {try {mylistener = new tcplistener (port); mylistener.start (); console.writeline ("Server-side Ready - accepting links ..."); Thread THREADSTART (StartListen) ); Th.Start ();} catch (exception e) {console.writeline ("Anomalus occurred while starting listening:" E.toTString ());}}

Public static void main (string [] argv) {dateserver dts = new dateserver ();

Public void startlisten () {while (true) {socket mysocket = myListener.acceptsocket (); if (mysocket.connected) {Console.writeline ("Client Connected !!"); btye [] received = new byte [64]; INT i = mysocket.Receive (Receive, Receive.Length, 0); string recne = system.text.Encoding.ASCII.GetString (Reveive); console.writeline (RECE); if (String.Compare (Rece, "Reqtime" == 0) {datetime new = datetime.now; string strdateline = "server time" now.toT7tring (); byte [] BytedateLine = Encoding.uncode.getBytes (strdateline.tocharRay ()); mysocket.send (Bytedateline, Bytedateline.length, 0);}}}}}

@ Client: service request time using System; using System.Net; using System.Net.Sockets; using System.Threading; using System.Text; public class DateClient {private TcpClient tcpc; private string name; private int port = 4554 ; private bool readData = false; public DateClient (string name) {tryagain: this.name = name; try {tcpc = new TcpClient ( "localhost", port); NetworkStream nts = tcpc.GetStream (); if (nts.CanWrite ) {String sender = "reqtime"; byte [] sends = encoding.unicode.getBytes (Sender.tochararray ()); NTS.Write (Sends, 0, Sends.Length); nts.flush ();} while ReadData && nts.canread) {if (nts.dataavailable) {byte [] rcd = new byte [128]; int i = nts.read (RCD, 0, 128); string ree = encoding.unicode.getstring (RCD) Console.writeline (ree); readdata = true; }}} Catch ("Exception E) {Console.WriteLine (" Unable to link to server " E.TOString ()); console.write (" Try once? [Y / N]: "); char check = (char CHAR ); If (check == 'y' || check == 'y') goto tryagain;

Public static void main (string [] argv) {if (argv.Length <= 0) {console.writeline ("Usage: Dataclient ); console.write (" Do you want to enter a server name [Y / N ]? "); Char check = (char) console.read (); if (check == 'y' || Check == 'y') {Console.write (" Please enter the server name: "); string newname = Console.readline (); DateClient DC = New DateClient (newname); Console.WriteLien ("Disconnect!"); Console.readline ();}} else {dateclient DC = New Dateclient (Argv [0]); Console.writeline ("Disconnect!"); Console.readline ();}}} ************************************* *********************************************************** ******** UDP multicast network time service application ******************************************* *********************************************************** **** / / Time Server Using System; use system.net.sockets; use system.net; using system.threading; using system.text;

class UdpServer {private static int port = 2001; public static void Main () {UdpClient uc = new UdpClient (); IPAddress MultiCastip = IPAddress.Parse ( "225.3.12.45"); uc.Connect (MultiCastip, port); while ( True) {thread.sleep (1000); DateTime Now = DateTime.now; string strdatetime = now.tostring (); console.writeline ("Server Time: strdatetime); Byte [] BytedateTime = Encoding.uncode.GetBytes STRDATETIME.TOCHARRAY ()); uc.send (BytedateTime, BytedateTime.Length);}}

// Time Service client using System; using System.Net.Sockets; using System.Net; using System.Text; class UdpClients {private static int port = 2001; public static void Main () {UdpClient uc = new UdpClient (port ); IPAddress MultiCastip = IPAddress.Parse ( "225.3.12.45"); uc.JoinMulticastGroup (MultipCastip); IPEndPoint ipEnd = new IPEndPoint (MultiCastip, port); Console.WriteLine ( "ready to accept the time ..."); while ( True) {byte [] rxdatetimebuf = uc.receive (ref iPnd); string strdatetime = encoding.unicode.getstring; console.writeline ("Client Receive Time" strDateTime);}}}

Multi-threaded programming *************************************************************** ********************************** Work thread ************ *********************************************************** ********************* // windows way void read timemE (Void);

Handle hthread; dword threadid; hthread = CreateThread (Null, 0, (LPTHREAD_START_RUNTINE) REATIME, NULL, 0, & ThreadID;

Void Readtime (void) {char str [50]; systemtime st; while (1) {getSystemTime (& st); sprintf (STR, "% u:% u:% u", st.WHOUR, St.Wminute, ST. WSECOND); setdlgitemtext (hwnddlg, IDE_TIME, STR); SLEEP (1000);}}

// c # mode public class foo {public void readtime (void);

Void foo :: readtime (void) {char str [50]; systemTime st; while (1) {datetime now = datetime.now; string strdateline = now.toshortdateString () " now.tolongTimeString (); thread. Sleep (1000);}}

public class Simple {public static int Main (string [] args) {Console.WriteLine ( "Thread Simple Sample"); Foo oFoo = new Foo (); Thread oThread = new Thread (new ThreadStart (oFoo.ReadTime)); oThread .Start (); return 0;}} // Win32 Runtime // Visual C # CreateThread [CreateThread (& Method)] Combination of Thread and ThreadStart [new Thread (newThreadStart (& oFoo :: Method))] TerminateThread Thread.AbortSuspendThread Thread. SuspendResumeThread Thread.ResumeSleep Thread.SleepExitThread ExitThreadGetCurrentThread Thread.CurrentThreadSetThreadPriority Thread.PriorityWaitForSingleObject on the thread handle Thread.JoinNo equivalent Thread.NameNo equivalent Thread.ApartmentStateNo equivalent Thread.IsBackground

// Application configuration and components ***************************************************** **************************************************************************************** *********************************************************** ************************************ Using System; Public Class SquareSample {public void Calcsquare (int nsideLength, out int nsquared) {nsquared = nsideLength * nsideLength;} public int calcsquare (int nsideength) {return nsideLength * nsideLength;}}

class SquareApp {public static void Main () {SquareSample sq = new SquareSample (); int nSquared = 0; #if CALC_W_OUT_PARAM sq.CalcSquare (20, out, nSquared); #else nSquared = sq.CalcSquared (15); #endif Console.writeline (nsquared.tostring ());}}

#define release # define demoversion

#if debug # undef demoversion # ENDIF

Using system; class demo {public static void main () {#if debug console.writeline ("debug version"); #endif release &&! demoversion console.writeline ("Full Release version"); #ELSE console.writeline Demo violet "); #ndif}} **************************************************** ************************************************************************************** *********************************************************** ****************************** # Define debug # define release # defien demovers

#if demoversion &&! Debug # WARNING you are building a demo version # Endif

Using system; class demo {public static void main () {console.writeline ("demo Application");}}

*********************************************************** ******************************* Condition properties *************** *********************************************************** ****************** # Define debug

using System; class Info {[conditional ( "DEBUG")] public static void Trace (string strMessage) {Console.WriteLine (strMessage);} [Conditional ( "DEBUG")] public static void TraceX (string strFormat, params object [ ] list) {console.writeline (strformat, list);}}

Class testconditional {public static void main () {info.trace ("Cool"); info.tracex ("{0} {1} {2}", "c", "u", 2001);}}

*********************************************************** ******************************* Design component *************** *********************************************************** ***************** CSC file.cscsc / target: library file.cs

Using cscomponent; using vbcomponent; ../ csc /reference:cscomponent.dll /reference:vbcomponet.dll / target: Winexe Form1.cs

转载请注明原文地址:https://www.9cbs.com/read-40343.html

New Post(0)