C# network application programming - review summary

Chapter 1 Introduction to Network Application Programming

Three Network Architectures

The TCP and UDP protocols are at the transport layer

socket socket

The socket is located between the application layer and the transport layer . A socket instance stores information such as the IP address and port of the machine, the IP address and port of the other party, and the network protocol adopted by the two parties for communication.

Three types:

  1. Stream socket: realize connection-oriented TCP communication

  2. Datagram socket: realize connectionless UDP communication

  3. Raw sockets: implement IP packet communication

TCP application programming

There are two types of communication in the field of IP connections: Connection-Oriented and Connectionless

The server uses the TcpListener class, and the client uses the TcpClient class; or all use Socket to implement

UDP application programming

The UdpClient class is a further encapsulation of socket programming at the UDP level.

IPAddress class

Provides IP address conversion and processing functions

  • The Parse method converts an IP address string into an instance of IPAddress:IPAddress ip = IPAddress.Parse("143.24.20.36");

  • The AddressFamily attribute can determine whether the IP address is IPv6 or IPv4:if (ip.AddressFamily == AddressFamily.InterNetworkV6)

IPEndPoint class

Describes the host and port information an application needs to connect to a service on a host

Common constructor: public IPEndPoint(IPAddress address, int port);the first parameter specifies the IP address, and the second parameter specifies the port number

IPHostEntry class

The IPHostEntry class associates a Domain Name System (DNS) hostname with a set of aliases and a set of matching IP addresses. This class is generally used together with the Dns class

  • AddressList property: Get or set the list of IP addresses associated with the host (including IPv4 and IPv6)

  • HostName attribute: contains the host name of the specified host

DNS class

  • GetHostEntry static method: used to query the list of IP addresses associated with a certain host name or IP address in the DNS server:IPAddress[] ips = Dns.GetHostEntry(“news.sohu.com”).AddressList;

  • GetHostAddresses method: Get the IP address of the specified host, this method returns an array of IPAddress type: IPAddress[] ips = Dns.GetHostAddresses("www.cctv.com");(if the ip address is in the brackets, this address will be returned; if it is empty, all ipv4 and ipv6 addresses of the machine will be returned)

  • GetHostName method: used to get the local host name:string hostname = Dns.GetHostName( );

Network card information detection related classes

  • NetworkInterface class: Provides configuration and statistics for network adapters. You can use this class to detect how many network adapters the machine has, the model of the network adapter, and the speed of the network adapter, etc.:NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();

     

  • IPInterfaceProperties class: Detect various addresses supported by all network adapters of the machine. The IPInterfaceProperties class is an abstract class and cannot be instantiated. Its instance can be obtained through GetIPProperties() of the NetworkInterface object

     

Network traffic detection related classes

  • IPGlobalProperties class: Provides information about local computer network connections and communication statistics:IPGlobalProperties properties =IPGlobalPropeties.GetIPGlobalProperties( );

     

  • TcpConnectionInformation class: Provides information about native Transmission Control Protocol (TCP) connections:TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();

Chapter 3 Process Threads and Application Domains

process

  • A process is a basic concept at the operating system level, which can be simply understood as a "running program".

  • Process is the basic unit of resource scheduling and allocation

  • Processes are independent of each other.

  • In the management of the operating system level, the Process class can be used to start and stop the local or remote process.

thread

  • Divide a process into several independent execution flows, each execution flow is called a thread

  • Thread is the basic unit of CPU scheduling and allocation

  • A process can contain only one thread or multiple threads at the same time.

  • Threads share process resources

Concurrent execution of threads: threads are parallel on a macro level and serial on a micro level

Thread asynchronous execution: Each thread runs independently when executing, no context, no waiting for each other, and the execution sequence is unknown

number of logical cores

Use the static ProcessorCount property provided by the System.Environment class to obtain the number of logical cores available on the machine

Process management (Process class)

start process

  1. Create an instance of the Process component, for example: Process p = new Process();

  2. Specify the application name to run and the parameters to pass:

    p.StartInfo.FileName = "文件名";

    p.StartInfo.Arguments = "参数";

    p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

  3. Call the Start method of the instance to start the process: myProcess.Start();

or:Process.Start(“Notpad.exe”);

stop process

  1. Kill method and CloseMainWindow method: the former is used to forcibly terminate the process, and the latter is just a "request" to terminate the process.

  2. HasExited attribute: The HasExited attribute is used to determine whether the started process has stopped running.

  3. WaitForInputIdle method: Only applicable to processes with a user interface, it can make Process wait for the associated process to enter the idle state.

  4. WaitForExit method: Set the time to wait for the associated process to exit

  5. ExitCode attribute and ExitTime attribute: The ExitCode attribute is used to obtain the value specified when the associated process is terminated (0 success, non-zero error). The ExitTime attribute is used to obtain the time when the associated process exits. These two properties can only be detected when the HasExited property is true.

  6. EnableRaisingEvents Property The EnableRaisingEvents property is used to get or set whether the Exited event should be raised when the process terminates. true if the Exited event is raised when the associated process terminates, otherwise false

Get all process information

The GetProcesses static method of the Process class is used to create a new Process array and associate the array with all process resources on the local computer.

  • Get all processes of the local computer:Process[] myProcesses = Process.GetProcesses();

  • Get all processes of a remote computer:

    Process[] myProcesses =Process.GetProcesses (remoteMachineName);

Get the specified process information

  • The GetProcessById static method of Process will automatically create a Process object, associate it with the process on the local computer, and pass the process Id to the Process object.Process p = Process.GetProcessesById(5152);

  • The GetProcessesByName static method returns an array containing all associated processes.

    Get the process with the specified name on the local computer:

    Process[] myProcesses = Process.GetProcessesByName("进程名称");

Get the process with the specified name on the remote computer: ​Process[] myProcesses =Process.GetProcessesByName( "远程进程名称",remoteMachineName);

thread management

main thread and secondary thread

  • No matter what type of application program, when these programs are run as a process, the system will create a default thread for the process, which is called the main thread.

  • The main thread is used to execute the code in the Main method, and when the Main method returns, the main thread is automatically terminated.

  • In a process, threads other than the main thread are called auxiliary threads.

Foreground threads and background threads

  • A thread is either a foreground thread or a background thread.

  • Background threads do not affect the termination of the process, while foreground threads do.

  • Using the IsBackground property of the Thread object, you can set or judge whether a thread is a background thread or a foreground thread

  • Set the IsBackground property of a thread to true to make it a background thread.

  • By default, the threads belonging to the managed thread pool are all background threads (that is, its IsThreadPoolThread property is true), and the threads generated by creating and starting a new Thread object are all foreground threads.

Create and start threads

  • Create a separate thread through Thread, the common form is:Thread t=new Thread(<方法名>);

  • Threads are implemented through delegation, which depends on whether the defined method has parameters. The method without parameters uses the ThreadStart delegate: Thread t3 = new Thread(new ThreadStart(MethodC));the method with parameters uses the ParameterizedThreadStart delegate:Thread t3 = new Thread(new ParameterizedThreadStart(MethodC));

  • The thread created by Thread is the foreground thread by default.

  • The thread starts calling the Start method of the instance t.Start();// calling the method without parameters t.Start(“abc”);// calling the method with parameters

Thread Termination and Cancellation

  • The first method is to first set a Boolean field with a modifier of volatile to indicate whether the thread needs to be terminated normally, which is called terminating the thread. (The field will no longer be optimized away by the compiler. This ensures that the field takes on the latest value at all times.)

  • The second method is to call the Abort method of the Thread instance in other threads to terminate the current thread. The final effect of this method is to forcibly terminate the execution of the thread, which belongs to the case of abnormal termination, which is called canceling the execution of the thread.

sleep process

Call the static Sleep method provided by the Thread class to suspend the current thread for a period of time: Thread.Sleep(1000);(The Sleep method suspends the thread where the statement is located, not other threads; and it is not possible to suspend other threads from one thread.

Get or set the priority of a thread

When creating a thread, the default priority is Normal

Use the following methods to assign a higher priority to a thread:Thread t1 = new Thread(MethodName); t1.priority = ThreadPriority.AboveNormal

Thread Pool

  1. The threads in the managed thread pool are all background threads.

  2. Tasks added to the thread pool do not necessarily execute immediately.

  3. The thread pool can automatically reuse created threads. Once a thread in the pool completes its task, it returns to the queue of waiting threads, waiting to be used again, rather than destroying it directly.

  4. Developers can set the maximum number of threads in the thread pool.

  5. Starting with .NET Framework 4.0, the default size of the thread pool is determined by several factors; the threads in the thread pool are implemented using multi-core processing technology.

  6. In the traditional programming model, developers generally add work items to the thread pool directly using the ThreadPool.QueueUserWorkItem method.ThreadPool.QueueUserWorkItem(new WaitCallback(Method1)); ThreadPool.QueueUserWorkItem(new WaitCallback(Method2));

  7. ThreadPool only provides some static methods, and the thread pool cannot be used by creating an instance of this class.

Resource synchronization in thread pool multithreaded programming

  • Synchronous execution: When a statement is executed, the code behind it will not be executed until the statement is completed. This execution method is called synchronous execution.

  • Asynchronous execution: When a statement is executed, regardless of whether the statement is completed or not, the following statement will continue to be executed. This execution method is called asynchronous execution.

  • After one thread starts another or more threads, these threads will execute at the same time, which is called parallelism (concurrency to be precise).

  • When multiple threads executing in parallel access certain resources at the same time, you must consider how to keep multiple threads synchronized.

  • A classic example of a deadlock is when two threads both stop responding and are both waiting for the other to complete, preventing either thread from continuing to execute.

  • A race is an error in which the outcome of a program depends on which of two or more threads reaches a particular block of code first.

How to achieve resource synchronization

  • Lock public or private fields with the volatile modifier. Use this modifier to directly access a field in memory, rather than caching the field in a processor register. The advantage of this is that all processors have access to the latest value of the field.

  • Lock local variables with the static method provided by the Interlocked class. The System.Threading.Interlocked class provides atomic-level static operation methods through locking and unlocking. When operating on a local variable in the parallel execution process, this method can be used to achieve synchronization.

  • Use the lock statement to lock the code block. Directly use the lock statement provided by C# to lock the code block containing local variables. After exiting the locked code block, it will be automatically unlocked.

Multithreaded programming model in WPF

By default, the .NET framework does not allow one thread to directly access controls in another thread

In order to solve deadlocks and synchronization problems during asynchronous execution, every element in WPF (including the root element) has a Dispatcher property

To interact with the user interface in a background thread, this can be done by registering a work item with the WPF control's Dispatcher. There are two common methods: Invoke method and InvokeAsync method.

  1. The Invoke method is a synchronous call, that is, it does not return until the delegate is actually executed in the thread pool.

  2. InvokeAsync is an asynchronous call.

application domain

Definition: A main process can contain one or more "subprocesses", and the memory range (or boundary) occupied by each "subprocess" is called an application domain.

Application domain and thread relationship

  1. Application domains form isolation boundaries for security, version control, reliability, and offloading of managed code. When an application is executed, all managed code is loaded into an application domain and run by one or more managed threads.

  2. There is no one-to-one correlation between application domains and threads.

  3. Application domains are isolated from each other, and one application domain cannot directly access the resources of another application domain.

Relationship between application domains and processes

  1. Each application domain within an application process can be thought of as a "child process".

  2. A process can contain only one application domain, or it can contain multiple isolated application domains at the same time.

  3. Multi-process is a function used at the operating system level, which consumes a lot of resources and is complicated to control details; application domain is a function used at the application level, which is faster, less resource-consuming, and safer than directly using multi-process to realize process management. is a lightweight process manager for application developers.

assembly

A build block for .NET Framework applications that provides the CLR with the information it needs to recognize and implement types. assembly contains module, module contains type, type contains member

reflection

Provides objects that encapsulate assemblies, modules, and types.

  1. Using reflection, you can dynamically create instances of classes, bind classes to objects or obtain classes from existing objects, and call methods of classes or access their fields and properties.

  2. Metadata information can be found from an assembly using reflection.

Chapter 4 Encryption and Decryption of Data Flow and Data

Data Encoding and Decoding

Encoding: The process of converting a sequence of characters into a sequence of bytes. Decoding: The process of converting a sequence of bytes into a sequence of characters.

Common character set encoding methods:

  • ASCII

  • Unicode

  • UTF-8

  • GB2312 and GB18030

Encoding class

 

Get all encoding names and their description information: Use the static GetEncodings method of the Encoding class to get an array of EncodingInfo types containing all encodings

foreach ( EncodingInfo ei in Encoding.GetEncodings( )) { Encoding en = ei.GetEncoding( );

}

Use the Convert method of the Encoding class to convert the byte array from one encoding to another encoding, and the conversion result is an array of byte type

byte[] b = Encoding.Convert(unicode, utf8, unicode.GetBytes(s));

data flow

  1. FileStream class, MemoryStream class, NetworkStream class, CryptoStream class

  2. StreamReader and StreamWriter classes for reading and writing text

  3. BinaryReader and BinaryWriter classes for binary reading and writing, etc.

1. Use the constructor to create a FileStream object:

FileStream (string path, FileMode mode, FileAccess access)

  • Optional values ​​​​of the FileMode enumeration: CreateNew, Create, Open, OpenOrCreate, Truncate, Append

  • The optional values ​​​​of the FileAccess enumeration are: Read, Write, ReadWrite

2. Use the File class to create a FileStream object:

  • Create a read-only file stream using the OpenRead method;FileStream fs= File.OpenRead(@"D:\ls\File1.txt");

  • Create a write-only file stream using the OpenWrite method.FileStream fs= File.OpenWrite(@"D:\ls\File1.txt");

memory stream

Using the MemoryStream class under the System.IO namespace, you can operate on byte arrays stored in memory as memory streams:

  • Use the Write method to write the byte array to the memory stream

  • Use the Read method to read the data in the memory stream into a byte array

The usage of MemoryStream is similar to the usage of file stream, which supports search and random access to data stream:

  • The CanSeek attribute value defaults to true

  • Get the current position of the memory stream through the Position property.

Use occasions of MemoryStream:

  • In data encryption and caching of data with variable length, etc. The capacity of the memory stream can be automatically increased

  • When data of photo type is read from the database and displayed in the graphic control. Byte Array -> Memory Stream -> ImageSource

network flow

NetworkStream can be regarded as setting up a data channel between the data source and the receiving end, and reading and writing data can be carried out for this channel.

Note: The NetworkStream class only supports connection-oriented sockets.

 

The process of sending and receiving TCP data using network streams:

  1. The Write method is responsible for sending the byte array from the process buffer to the native TCP send buffer

  2. Then the TCP/IP protocol stack actually sends the data to the network through the network adapter

  3. Eventually it reaches the receiver's TCP receive buffer.

Get the NetworkStream object

  1. Use the GetStream method of the TcpClient object to get the network stream object. TcpClient tcpClient=new TcpClient( );tcpClient.Connect("www.abcd.com", 51888);          NetworkStream networkStream = tcpClient.GetStream( );

  2. Use Socket to get the network stream object.NetworkStream myNetworkStream = new NetworkStream(mySocket);

send data

The Write method is a synchronous method. Before writing data to the network stream, the Write method will be blocked until the sending is successful or an exception is returned.

byte[] writeBuffer = Encoding.UTF8.GetBytes("Hello"); myNetworkStream.Write(writeBuffer, 0, writeBuffer.Length);

Receive data

Call the Read method to read data from the receive buffer to the process buffer to complete the read operation

numberOfBytesRead = myNetworkStream.Read(readBuffer, 0,readBuffer.Length);

StreamReader and StreamWriter classes

1. Create instances of StreamReader and StreamWriter

  • If the data source is a file stream, memory stream or network stream, you can use the constructors of the StreamReader and StreamWriter objects to get the read and write streams.NetworkStream networkStream = client.GetStream( );

    StremReader sr = new StremReader (networkStream); StreamWriter sw = new StreamWriter (networkStream);

  • If you need to process file streams, you can also directly use the file path to create a StreamWriter object.StreamWriter sw= new StreamWriter ("C:\\file1.txt");

  • Equivalent to this method is the CreateText method provided by the File and FileInfo classes.StreamWriter sw = File.CreateText ("C:\\file1.txt");

2. Read and write text data

  • Write text data using the Write and WriteLine methods of the StreamWriter class

  • Use the ReadLine method of the StreamReader class to read text data.

Note: Don't forget to close the stream with the Close method, or let the system close it automatically with the using statement.

BinaryReader and BinaryWriter classes

The System.IO namespace also provides the BinaryReader and BinaryWriter classes to read and write streams in binary mode, making it easier to operate on binary data such as image files and compressed files.

For every read method in BinaryReader, there is a corresponding write method in BinaryWriter.

Chapter 5 Asynchronous Programming

Lambda expression

A lambda expression is an anonymous function that can be used to create delegate or expression tree types.

Basic usage definition

(input parameter list) => {expression or statement block}

x => x * x

Using Lambda Expressions in LINQ to Objects

var q2 = from i in numberListwhere i < 4select i;

Action and Func delegation

  • Action<[T1,T2,...,T16]>: Encapsulate a method without a return value, that is, the return value is void

 

 

  • Func<[T1,T2,...,T16,] TResult>: Encapsulates a method with a return value, and the return value type is Tresult

Tuple (Tuple class)

A tuple is a data structure in which elements have a fixed number and sequence.

Provides easy access and manipulation of datasets

In the .NET framework, tuples with 1 to 7 elements can be directly created through the Tuple.Create method, and objects with more elements can also be created through nested tuples. The elements in the tuple can be obtained through the ItemN (N=1,2,3,...,7) attribute of the Tuple object.

Advantage: Multiple values ​​can be passed to a method with one parameter

Implementation of asynchronous programming

Traditional Asynchronous Programming Model (APM)

The asynchronous operation using the Item design pattern is implemented through two methods named Begin--- and End---, which refer to the start and end of the asynchronous operation, respectively. For example: the Begin Read and End Read methods of the network stream object

Asynchronous control is complicated, and this technology has been eliminated at present.

Event-based asynchronous programming design pattern (EAP)

This pattern implements asynchronous methods with an event-driven model.

Usually there are one or several --- Async methods and a corresponding ---Completed event.

For example: Background Worker component, Picture Box control, etc.

This technology is currently obsolete.

Task-based Asynchronous Pattern (TAP)

.NET 4.0 framework, asynchronous programming suggested asynchronous programming techniques

Improved task-based asynchronous patterns (async, await, Task.Run, and TAP)

The combination of TAP and C# keywords is currently the recommended asynchronous programming technique.

Task

Task (Task) represents an asynchronous operation. Threads are needed when a task is running, and the Common Language Runtime (CLR) will create the necessary threads to support the execution of the task.

Task class

Represents an asynchronous operation that returns no value:Task t = Task. Run( () => { Console.WriteLine(“this is a task“); } );

Task<Result>类

Represents an asynchronous operation that returns a value:var t = Task<int>.Run( () => { int num=5; num=DateTime.Now.Year+5; return num; });

Task.delay method

  • Delay(Int32) //Delay the specified number of milliseconds

  • Delay(Timespan) //Delay the specified time (year, month, day, hour, minute, second, millisecond, etc.)

  • Delay(Int32, Cancellation Token) //Cancel the task operation after delaying the specified number of milliseconds

  • Delay(Timespan, Cancellation Token) //Delay the task operation after the specified time

asynchronous operation keyword

A method decorated with async is called an asynchronous method. Event handlers decorated with async are called asynchronous event handlers. Both can also be collectively referred to as asynchronous methods.

Naming convention for asynchronous methods: methods are suffixed with "Async"

async modifier

For normal methods:

  • If the method does not return a value, it is co-signed with async and Task.

  • If the method has a return value, it is co-signed with async and Task<T>.

For event handlers:

  • Co-sign with async and void.

await operator

  • The await operator means waiting for the result of an asynchronous execution. Essentially operate on the return value of the method.

  • When using await to asynchronously wait for the completion of the task, the code behind it will not be executed, but it will not affect the user's operation on the UI.

  • The await operator must be placed inside an async method.

create task

Define the method of task execution

  • Define tasks in a normal way

     

    Ordinary methods should be called with the Task.Run method, or use the constructors of the Task and Task<Result> classes to explicitly create a Task instance, and then start it.

  • Define tasks with asynchronous methods

     

  • Define tasks with anonymous methods

     

Create and execute tasks implicitly using the Task.Run method

The Task.Run method is a function provided by the .NET framework 4.5, which executes a task in a separate thread in the thread pool.

Several forms of the Run method:

  • Run(Funk<Task>) executes a task with no return value in the thread pool using the default scheduler

  • Run<Result>(Funk<Task<Result>>) executes a task with a return value in the thread pool using the default scheduler

  • Run(Funk<Task>, Cancellation Token) Can listen for cancellation notifications during task execution

  • Run<Result>(Funk<Task<Result>>, Cancellation Token) Can listen for cancellation notifications during task execution

Implicitly create asynchronous tasks using the async and await keywords

The async and await keywords are functions provided by C# 5.0. An asynchronous method that only contains the async and await keywords does not create a new thread, it just means that the specified task is executed asynchronously in the current thread.

Create and execute tasks implicitly using the scheduler of WPF controls

It can only be used in WPF applications, and the synchronization between asynchronous processes is more complicated.

Create a task by explicitly calling the constructor of Task or Task<Result>

Usage: Create a task first, and then call the Start method to start the task. Similar to the usage of Thread.

  • Constructor for the Task class.

     

  • Constructor of Task<Result> class

     

Cancel or terminate the execution of a task

The Cancellation TokenSource class and the Cancellation Token structure introduced by the .NET framework are used to coordinate the cancellation of multiple threads, thread pool work items or Task objects.

Cancellation TokenSource class and Cancellation Token structure

  • Cancellation TokenSource is used to create cancellation notifications, known as cancellation sources .

    CancellationTokenSource cts = new CancellationTokenSource();

  • The Cancellation Token structure is used to propagate notification that an operation should be canceled, known as a cancellation token .

    Cancellation Token ct=cts.Token;

The Cancel method of the CancellationTokenSource object issues a cancellation notification, and then sets the IsCancellationRequested property of the Cancellation Token object to true.

After receiving the cancellation notification, the method executing the task can terminate the operation in one of the following ways

  • In the task code, simply return from the delegate, the task status value is RanToCompletion (normal completion).

    The task status can be obtained through the task's Status property. Similar to the boolean variable method set when the thread terminates

  • In the task code, an OperationCanceledException exception is thrown and passed to the token on which cancellation was requested.

    Call the ThrowIfCancellationRequested method of the Cancellation Token object

Cold tasks and hot states

A task explicitly created with the constructor of the Task class or the Task<Result> class is called a cold task.

Cold tasks must be started through the Start method.

The execution of a task during its lifetime is called the hot state.

Status property and TaskStatus enumeration

Use the Status property of the task instance to get the status of task execution. The status of task execution is represented by the TaskStatus enumeration.

The enumeration values ​​of TaskStatus are:

  • Created: The task has been initialized, but has not yet entered the schedule.

  • WaitingForActivation: The task has entered the scheduling plan and is waiting to be activated by the scheduler.

  • WaitingToRun: The task has been activated by the scheduler, but execution has not yet started.

  • Running: The task is running, but has not yet completed.

  • RanToCompletion: The task completed successfully.

  • Canceled: The task completed because it was canceled (either the task itself raised an OperationCanceledException, or the caller signaled the task's Cancellation Token before the task was executed).

  • Faulted: The task completed due to an unhandled exception.

  • WaitingForChildrenToComplete: The task itself has completed and is waiting for additional child tasks to complete.

Attributes related to task completion

  • IsCompleted attribute: Indicates whether the task is completed. When the task status is RanToCompletion, the task completed successfully.

  • IsCanceled attribute: Indicates whether the task is completed due to cancellation.

  • IsFaulted property: Indicates whether the task was completed due to an unhandled exception.

  • The relationship between cancellation and completion: Cancellation is a signal to the task that it is hoped that the task will end as soon as possible.

    Completion is the end of task execution.

Report the progress of task execution

The progress of a task execution can be reported using the Progress<T> class. The Progress<T> class is implemented through the IProgress<T> interface. The declaration of this class is as follows:

 

Chapter 6 Parallel Programming

Parallel strategies from the perspective of business implementation: Parallel programming models are divided into data parallelism and task parallelism .

Looking at parallel strategies from the perspective of hardware implementation: parallelism can be divided into single-machine multi-core parallelism and multi-machine multi-core parallelism .

Task Parallel Library (TPL) and its taxonomy

TPL classification:

  • Data Parallelism: Performs the same operation on the elements of the source collection or array at the same time. With the help of the For or Foreach method of the Parallel class to achieve.

  • Task parallelism: realize task parallelism with the help of the static method Invoke provided by the Parallel class.

  • Parallel query: Parallel implementation of LINQ to Objects query, namely PLINQ.

Advantages of TPL over traditional multithreaded programming models

  • The TPL programming model uses the CLR thread pool to perform multiple tasks and automatically handles work partitioning, thread scheduling and cancellation, state management, and other low-level details.

  • The TPL also dynamically scales the degree of concurrency to make the most efficient use of all available processors.

  • TPL is more intelligent than Thread. When it uses heuristics to predict that the task set will not gain performance benefits from parallel operation, it will automatically choose to run sequentially.

Parallel class

  • The Parallel.For method is used to execute the for loop in parallel.

  • The Parallel.Foreach method is used to execute the foreach loop in parallel.

  • The Parallel.Invoke method is used for task parallelism.

Parallel helper class

  • ParallelOptions class: Provides operation options for parallel methods. CancellationToken: get or set the cancellation token TaskScheduler: the default value is null

  • ParallelLoopState class: Interacts iterations of a Parallel loop with other iterations.

    Break method: tells the Parallel loop to stop executing iterations other than the current iteration as soon as possible. ​Stop method: Tell the Parallel loop to stop execution as soon as possible.

  • ParallelLoopResult: Provides the completion status of the Parallel loop.

    IsCompleted: Gets whether the loop has completed.

data parallelism

Performs the same operation on the elements of the source collection or array simultaneously. Implementation method: Parallel.For or Parallel.Foreach method

Parallel.For method

The Parallel.For method is used to execute the for loop in parallel

Simple Parallel.For loop:Parallel.For(<开始索引>,<结束索引>,<每次迭代执行的委托>)

Parallel.For loop with parallel option:Parallel.For(<开始索引>,<结束索引>,<并行选项>,<每次迭代执行的委托>)

Parallel.For loop with parallel loop state:

 

Parallel.For loop with thread local variables, thread local variables refer to local variables in a thread, other threads cannot access. Data stored in thread-local variables is called thread-local data:

 

Using Parallel.ForEach Method to Realize Data Parallelism

Simple Parallel.ForEach loop:ForEach<TSource>(IEnumerable<TSource>, Action<TSource>)

task parallelism

Task parallelism refers to running one or more independent tasks at the same time, and the parallel tasks are executed asynchronously.

Parallel.Invoke method

The Parallel.Invoke method is used for task parallelism. The overloaded forms are:

  • public static void Invoke(Action[] actions )

  • public static void Invoke(ParallelOptions parallelOptions, Action[] actions )

Both of these methods perform the provided operations in parallel as much as possible, and the second overload can also cancel the operation.

Chapter 7 Getting Started with WCF

XML

XML (Extensible Markup Language, Extensible Markup Language) is a meta-markup language that uses text to define semantic tags. It has the advantages of being platform-independent, flexible to define data and structural information, and convenient for network transmission.

Features:

  • XML is a text encoding and can be transmitted normally on any network.

  • Independent of the operating system, object model and programming language chosen

  • All tags in XML are self-defined, and through these custom tags, different parts of a certain data and their nested hierarchical structure can be described.

  • XML stipulates that all tags must have start and end tags.

Web Service

Web Service is also called Web Service

According to different data exchange formats, Web Service is further divided XML Web ServiceintoJSON Web Service

The architecture of Web services is based on the three roles of service provider, service requester, and service registry , and is constructed by using three operations of publishing, discovery, and binding

 

SOAP (Simple Object Access Protocol): An XML-based message exchange protocol with HTTP as the basic transport protocol, which defines the format for exchanging data between the client and the Web service.

WSDL (Web Service Description Language): Describes the methods provided by Web services and various ways to call these methods.

The process of communication between the client and the Web service

  1. Phase 1: Serialization:

    (1) The client application creates an instance of the Web service proxy class.

    (2) The client application calls the method of the proxy class.

    (3) The client infrastructure serializes the parameters required by the Web service into SOAP messages, and sends them to the Web server through the network.

  2. Phase 2: Deserialization:

    (4) The Web server receives the SOAP message and deserializes the XML, creates an instance of the Web service at the same time, calls the method provided by the Web service, and passes the deserialized XML as a parameter to the method.

    (5) The Web server executes the method provided by the Web service, and obtains the return value and various output parameters

  3. Phase 3: Serialization:

    (6) The Web server serializes the return value and output parameters into a SOAP message, and returns it to the client infrastructure through the network.

  4. Phase 4: Deserialization:

    (7) The client infrastructure receives the returned SOAP message, deserializes the XML into the return value and output parameters, and passes it to the instance of the proxy class.

    (8) The client application receives the return value and output parameters.

     

Message Queue (MSMQ)

MQ (Message Queue) is an asynchronous transmission mode based on queue and transaction processing to realize mutual communication between multiple different applications. The realization principle is: the message sender puts the information to be sent into a container (called Message), and then saves it in a system public message queue (Message Queue); the local or remote message receiving program then Messages addressed to it are taken from the queue for processing.

Service Oriented Architecture (SOA)

The basic idea is to use a unified, "service"-centric model to integrate various technologies, not just limited to Web services. For .NET developers, this SOA-based concrete implementation is WCF.

WCF

Features:

  • Service-centric

  • Support multiple message exchange modes

  • Support multiple transmission protocols and encoding methods

  • Supports workflow, transactions, and persistent message processing

  • Uniformity, Security and Scalability

Endpoint (EndPoint)

The endpoint (EndPoint) is used to determine the target of network communication, which is realized by the EndPoint class and specified by the <endpoint> configuration section in the configuration file. For WCF, an endpoint consists of an address, a contract, and a binding, all of which are indispensable. Among them, the address (Address) is used to disclose the location of the service, and the agreement (Contract) is used to disclose which specific service is provided.

Address

Addresses in WCF are used to determine the location of an endpoint. The address can be a URL, FTP, network path, or local path. The address format specified by WCF is:[传输协议]://[位置][:端口][/服务路径]

For example:http://www.mytest.com:50001/MyService

WCF services can be transported between a variety of different underlying network protocols (such as TCP, UDP, HTTP, etc.).

For example:net.tcp://localhost:50001/MyService

Binding

WCF uses bindings to define how WCF clients communicate with WCF services. WCF provides a variety of binding methods:

BasicHttpBinding、WSHttpBinding、NetTcpBinding、NetNamedPipeBinding、NetMsmqBinding

Contract

A contract represents the rules for information exchange between a client and a server. Without specifying a contract, there is no way to communicate between the client and server. For example: service agreement, data agreement, message agreement, etc.

Service Agreement: Refers to which services WCF exposes to clients. Including ServiceContract characteristics (definition service agreement) and OperationContract characteristics (definition operation agreement).

Data contract: A data contract is an agreement to exchange data between the server and the client, that is, describe the data to be exchanged in an abstract way and transmit it to the client. Use the DataContract feature (defining which classes can be serialized) and the DataMember feature declaration (declaring which members of the class can be serialized)

Message contracts: In some cases, a single type is required to represent an entire message. Use message contracts to avoid unnecessary wrappers during XML serialization. MessageHeader attribute and MessageBodyMember attribute. Inside the message agreement, the message header is specified through the MessageHeader attribute (MessageHeaderAttribute class), and the message body is specified through the MessageBodyMember attribute (MessageBodyMemberAttribute class). And can apply MessageHeader attribute and MessageBodyMember attribute to all fields, properties and events

If the type contains both a message contract and a data contract, only the message contract is processed

WCF stipulates that the interface that implements the service can only contain method declarations, and it is not allowed to declare properties and fields in the interface. In other words, properties and fields are exposed through data contracts in classes that implement the interface

Ways to host WCF

  • Use IIS or WAS to host WCF services (WCF service applications use this mode)

  • Utilize Windows Service to Host WCF Service (WCF Service Library)

  • Self-hosting mode (write your own code to implement hosting WCF)

There are 4 main steps in writing a WCF server program: selecting the hosting method, designing and implementing the agreement, configuring the service, and hosting the service

Endpoint Binding Method

basicHttpBinding, wsHttpBinding, wsDualHttpBinding, , netTcpBinding, udpBinding, netNamedPipeBindingandnetMsmqBindingnetPeerTcpBinding

Chapter 8 WCF and HTTP Application Programming

Introduction to HTTP

HTTP (HyperText Transfer Protocol, Hypertext Transfer Protocol) In the TCP/IP architecture, HTTP belongs to the application layer protocol and is located at the top layer of TCP/IP.

HTTP works in TCP mode: the HTTP client first establishes a TCP connection with the server, then the client sends an HTTP request through the socket, and receives the HTTP response through the socket

HTTP is stateless: "stateless" means that after the client sends a request, the server does not store any state information about the client. Even if the client requests the same object again, the server will still resend the object, regardless of whether the object was originally sent to the client.

HTTP uses meta information as a header: HTTP provides the server with information about this HTTP request by adding a header (Header), that is, adding a part of information before the main data, called meta information (Metainformation). For example, what type of object is transferred, what encoding is used, and so on.

HTTP request

GET request: GET request is the most common request, which means that the client tells the server which resources to obtain. A GET request is followed by the location of a web page. In addition to the page location as a parameter, this request can also follow the version of the protocol such as HTTP/1.0 as a parameter to send more information to the server.

POST request: A POST request requires the server to receive a large amount of information. Compared with GET requests, POST requests do not append request parameters to the URL, but provide additional information to the server in the request body. It is generally used for the client to fill in the content contained in the Web form (Form), and then send the filled data to the server in the form of POST request.

HEAD request: The HEAD request communicates between the client program and the server without returning specific documents. Therefore, the HEAD method is usually not used alone, but plays an auxiliary role together with other request methods.

HTTP programming related classes (understand)

WebRequest and HttpWebRequest

  • WebRequest: The abstract base class for the request/response model. Used to access Internet data. It allows applications using the request/response model to request data from the Internet in a protocol-agnostic manner.

  • HttpWebRequest: is a specific implementation for HTTP. This class interacts with the server through the HTTP protocol. Use the Create method to initialize a new WebRequest instanceHttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriString);

HttpWebResponse class

Created by calling the GetResponse method of the WebRequest instance, and does not create an HttpWebResponse instance through the constructor

HttpWebResponse response=(HttpWebResponse)request.GetResponse();

Basic HTTP binding (BasicHttpBinding class)

Basic HTTP binding is implemented with the BasicHttpBinding class, which is configured with the basicHttpBinding element in the configuration file

Secure HTTP binding (WSHttpBinding class)

WSHttpBinding defines a secure, reliable and interoperable binding suitable for non-duplex services. The binding implements the WS-ReliableMessaging specification (ensures reliability) and the WS-Security specification (ensures message security and authentication).

Duplex secure HTTP binding (WSDualHttpBinding class)

The WSDualHttpBinding class also uses HTTP as the basic transport protocol, and is configured with the wsDualHttpBinding element in the server and client configuration files. This binding also uses "text/XML" as the default message encoding. However, it only supports the SOAP security model and requires reliable messaging.

Message exchange pattern between WCF client and server

Request response mode (Action/Reply)

After the client sends a request to the WCF server, the server executes the service operation and returns the operation result to the client. If the client is not invoked through an asynchronous operation, the client code will be blocked until the server returns the result of the service operation.

One-way mode (IsOneWay)

When a client invokes a WCF service operation, the server does not return the operation result to the client. Even if an execution error occurs on the server side, it will not return a result to the client.

sogo communication

Duplex means that both the client and the server can actively call each other. In this communication mode, WCF uses two-way binding to realize that the server and the client disclose endpoint information to each other.

Chapter 9 WCF and TCP Application Programming

Characteristics of TCP

TCP is the abbreviation of Transmission Control Protocol (Transmission Control Protocol). It is a connection-oriented transport layer protocol in the TCP/IP system and provides duplex and reliable services in the network.

Features:

  • One-to-one communication.

  • Secure sequential transfer.

  • Send and receive data through byte streams.

  • Data transferred without message boundaries

The solution to the TCP no-message boundary problem

  • Send fixed-length messages, this method is suitable for occasions where the message length is fixed.

  • Send the message length together with the message. Generally, 4 bytes are used in front of each sent message to indicate the length of the message, and then it is sent to the other party together with the message; Get the actual message length, and then receive the data sent by the sender in sequence according to the message length value. This approach is suitable for any occasion.

  • Use special tokens to separate messages, and special delimiters to separate messages. This method is mainly used when the message itself does not contain special tags.

Technology Selection of TCP Application Programming

  1. Using the Socket class to realize all the details in the process of TCP communication is controlled by the program written by oneself. Features: The method is flexible, but requires programmers to write a lot of code. Suggestion: Use this technique when defining some new protocols or having more flexible control over the underlying details.

  2. Using TcpClient and TcpListener and multi-threading to realize the TcpClient and TcpListener class further encapsulates the Socket class, which simplifies the difficulty of writing TCP programs with Socket to a certain extent, but the flexibility is also limited. Features: The details of monitoring and communication during TCP data transmission (such as message boundary issues) still need to be solved by programmers themselves through code.

  3. When writing TCP applications with TcpClient and TcpListener and multi-task implementation, developers do not need to consider implementation details such as multi-thread creation, management, and load balancing. They only need to realize multi-threading as multiple tasks.

  4. Problems such as monitoring with WCF and no-message boundaries are automatically completed within WCF. Programmers only need to consider the business logic in the transmission process. In addition, using WCF can also implement a custom protocol.

socket

There are three steps in connection-oriented socket programming:

  1. The server establishes a connection with the client (connection establishment phase)

  2. Send and receive messages between the server and the client (message sending and receiving phase)

  3. Disconnect, close the socket (disconnect phase)

     

1. Establish a connection (server)

IPHostEntry local=Dns.GetHostByName(Dns.GetHostName()); 
IPEndPoint iep = new IPEndPoint(local.AddressList[0], 1180); //创建套接字 
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //绑定 
serverSocket.Bind(iep); serverSocket.Listen(10); //监听 
Socket clientSocket = serverSocket .Accept();

1. Establish a connection (client)

IPAddress remoteHost = IPAddress.Parse("192.168.0.1"); 
IPEndPoint iep = new IPEndPoint(remoteHost, 1180); 
Socket localSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
localSocket.Connect(iep); //连接

2. Sending and receiving information (server)

Socket clientSocket = serverSocket .Accept(); //建立连接后,利用Send方法向客户端发送信息 clientSocket.Send(Encoding.ASCII.GetBytes("server send Hello")); //接收客户端信息 
byte[] myresult = new Byte[1024]; 
int receiveNum = clientSocket.Receive(myresult); 
Console.WriteLine("接收客户端消息:{0}", Encoding.ASCII.GetString(myresult));

2. Sending and receiving information (client)

//建立连接成功后,向服务器发送信息 
string sendMessage = "client send Message Hello"+DateTime.Now; localSocket.Send(Encoding.ASCII.GetBytes(sendMessage)); 
Console.WriteLine("向服务器发送消息:{0}", sendMessage); //接收服务器信息 
byte[] result = new Byte[1024]; localSocket.Receive(result); 
Console.WriteLine("接收服务器消息:{0}", Encoding.ASCII.GetString(result));

3. Disconnect

serverSocket.Shutdown(SocketShutdown.Both); serverSocket.Close();

Utilizing Traditional Technology to Realize TCP Application Programming

TcpClient class and TcpListener class

In order to simplify the complexity of network programming, .NET encapsulates the socket, and the encapsulated classes are the TcpListener class and the TcpClient class under the System.Net.Sockets namespace

  • The TcpListener class is used to monitor client connection requests.

  • The TcpClient class is used to provide connection information between local hosts and remote hostsTcpClient tcpClient = new TcpClient("www.abcd.com", 51888);

Programming flow of TCPListener and TCPClient

 

WCF and TCP related bindings

When using WCF to write TCP applications, you only need to set the relevant bindings in the server configuration file, and you can easily realize the corresponding functions, and it is not easy to make mistakes.

NetTcpBinding

NetTcpBinding class is used to bind WCF and TCP together, and provide communication between TCP server and client in the form of service. In the server configuration file, use the <netTcpBinding> element to configure

The default configuration of the <netTcpBinding> element is as follows:

  • Security mode: Transport (guaranteed transmission security).

  • Message encoding method: Binary (using a binary message encoder).

  • Transport protocol: TCP.

  • Sessions: Provides transport sessions (can also be configured as reliable conversations).

  • Transactions: No transaction processing capabilities (can also be configured to support transaction processing).

  • Duplex: Support.

Chapter 10 WCF and UDP Application Programming

Basic knowledge of UDP

UDP (User Datagram Protocol, User Datagram Protocol) is a simple, datagram-oriented connectionless protocol that provides fast but not necessarily reliable transmission services. The main function of UDP is to compress network data traffic into the form of datagrams. Each datagram uses 8 bytes to describe the header information, and the remaining bytes contain specific transmission data. UDP features:

  • UDP can be transmitted one-to-many, and UDP not only supports one-to-one communication, but also supports one-to-many communication. In other words, UDP can use multicast technology to send information to multiple receivers at the same time.

  • The transmission speed of UDP is faster than that of TCP. Since UDP does not need to establish a connection with the other party first, and does not require transmission confirmation, its data transmission speed is much faster than that of TCP.

  • UDP has message boundaries, and there is no need to consider message boundaries when using UDP.

  • UDP does not guarantee orderly transmission, and UDP does not ensure that the order in which data is sent is consistent with the order in which it is received. For bursty datagrams, it may be out of order. However, this kind of disorder basically rarely occurs, and usually only occurs when the network is very congested.

  • UDP is not as reliable as TCP, and UDP does not provide a guarantee mechanism for data transmission.

Unicast, broadcast and multicast

Unicast refers to sending information only to a specified remote host, which is essentially a one-to-one communication.

Broadcasting refers to sending messages to multiple computers in the subnet at the same time, which is divided into local broadcasting and global broadcasting. Local broadcasting is to send broadcast messages to all computers in the subnet, and other networks will not be affected by local broadcasting. Global broadcasting refers to the use of an IP address with all bits set to 1 (255.255.255.255 for Ipv4). However, since routers automatically filter out global broadcasting by default, it is meaningless to use this address.

Multicast is also called multicast. Since multicast is grouped, it is also called multicast. For IPv4, multicast refers to broadcasting within the class D IP address range from 224.0.0.0 to 239.255.255.255 (the first byte is between 224 and 239). In other words, the sender program sends data through an address in these ranges, and the receiver program also listens to and receives data from these address ranges.

Technology Selection of UDP Application Programming

Implemented with the Socket class

Directly use the Socket class under the System.Net.Sockets namespace to implement. When using this method, the programmer needs to write the most code, and all the details of the underlying processing need to be considered by the programmer

Implemented with UdpClient and multithreading

Use the UdpClient class and the Thread class under the System.Net.Sockets namespace to implement. The UdpClient class encapsulates the basic Socket. When sending and receiving data, there is no need to consider the details that must be dealt with when sending and receiving sockets. To a certain extent, it reduces the difficulty of writing UDP applications with Socket and improves programming efficiency.

Implementation with UdpClient and multitasking

It is implemented with UdpClient class and task-based programming model (Task class). Implementing with multitasking has more advantages than directly implementing with multithreading

Implemented with WCF

Use WCF to achieve. That is to bind WCF and UDP together through configuration, which is another form of encapsulation of Socket

Send and receive data using the UdpClient class

TCP has TcpListener class and TcpClient class, while UDP only has UdpClient class. This is because UDP is a connectionless protocol, so only one Socket is needed.

Since UDP does not require the sender and receiver to establish a connection first, the sender can send UDP datagrams directly to the specified remote host at any time. In this mode, the sender is the client, and the receiver with monitoring function is the server

The UdpClient class provides various overloaded constructors, which are used for IPv4 and IPv6 data sending and receiving respectively. Among these constructors, the most commonly used overloading form is the constructor with local endpoint parameters, the syntax is as follows.public UdpClient(IPEndPoint localEp)

UdpClient objects created with this constructor are automatically bound to the local endpoint specified in the parameters. The purpose of binding is to listen for datagrams from other remote hosts. For example:IPEndPoint localEndPoint = new IPEndPoint(localAddress, 51666); UdpClient client =new UdpClient(localEndPoint);

send data:

  • When using the Send method of the UdpClient object to send data synchronously, this method returns the number of bytes sent.

  • There are many overloaded forms of the Send method, and only the most commonly used overloaded forms are introduced here, and the syntax is as follows:

    public int Send(byte[] data, int length, IPEndPoint remoteEndPoint)

Receive data:

  • The Receive method of the UdpClient object is used to obtain the UDP datagram from the remote host, the syntax is as follows.

    public byte[] Receive(ref IPEndPoint remoteEndPoint)

Send and receive data asynchronously

For tasks that may take a long time to execute, or tasks that cannot be predicted how long they will take, it is best to use task-based asynchronous programming (call the SendAsync method and ReceiveAsync method of the UdpClient object). The advantage of using this method is that the user interface will not pause when sending and receiving data. Send data asynchronously, for example:await client.SendAsync(bytes, bytes.Length, remoteEndPoint);

Receive data asynchronously, for example:

 

Use UdpClient to Realize Group Sending Function

Join and leave multicast groups

Multicast groups can be permanent or temporary. In practical applications, most multicast groups are temporary, that is, they only exist when there are members in the multicast group. Any receiving end that joins the multicast group can receive the data sent from the multicast sending end. However, if you do not join a multicast group, you cannot receive multicast data.

When sending data to a multicast group, you need to create a UdpClient object first:UdpClient udpclient = new UdpClient("224.0.0.1", 8001);

When using multicast, you should pay attention to the setting of the object TTL value. Use the Ttl property of the UdpClient object to modify the default value of TTL, for example

udpClient.Ttl = 50;

Use the JoinMulticastGroup method of the UdpClient object to join the specified multicast group:udpClient.JoinMulticastGroup(IPAddress.Parse("224.0.0.1"));

Use the DropMulticastGroup method of UdpClient to exit the multicast group. The parameter indicates the IPAddress object to exit the multicast group:udpClient.DropMulticastGroup(IPAddress.Parse("224.100.0.1"));

Whether to allow receiving multicast

Use the MulticastLoopback attribute of the UdpClient object to control whether to allow receiving multicast information. This property defaults to true, which allows receiving multicast

Using WCF to Realize UDP Application Programming

The binding related to UDP in WCF is only <udpBinding>, and the corresponding class is UdpBinding class

When implementing multicast with WCF, only one project is required. This project is both a server and a client, and can only be implemented in a self-hosted manner

Guess you like

Origin blog.csdn.net/weixin_52357218/article/details/127703315