Compilation of basic questions and answers on C# network application programming

Table of contents

1. Briefly describe the characteristics of decentralized, centralized and distributed communication models.

2. Briefly describe the advantages and disadvantages of C/S and B/S.

3. What is a socket? What types of sockets are there?

4. What are the uses for digital ink? Do general WPF elements have digital ink related events?

5.Briefly describe which functions of smartphones and car navigation systems you have seen can be simulated on a PC or demonstrated as prototypes using digital ink?

6. What is the difference between a process and a thread? Why use multithreading? In what situations is multithreading suitable?

7. What are the differences and connections between foreground threads and background threads? How to determine whether a thread belongs to the foreground thread or the background thread? How to set a thread as background thread?

8. What is synchronization? Why synchronization is needed? What statements does C# provide to easily implement code synchronization?

9. What is a thread pool? What are the benefits of using a thread pool?

10. What are the differences and connections between application domains and processes?

11. What is encoding? What is decoding? Why are characters encoded and decoded? What classes does the .NET framework provide for character encoding and decoding?

12. What are the data streams provided by .NET that inherit from the Stream class?

13. Briefly describe the characteristics and implementation principles of symmetric encryption and asymmetric encryption.

Symmetric encryption:

Asymmetric encryption:

14.What is a digital signature? What are digital signatures used for?

15.How is an asynchronous method that only contains the async and await keywords different from an asynchronous method called with Task.Run?

16.What is the difference between calling methods when executing normal methods and asynchronous methods as tasks?

17.What is the difference between Action and Func delegates?

18.How many ways are there to create tasks?

19. How many timers are available in WPF applications?

20. Briefly describe the mechanism to implement the task cancellation function.

twenty one. What parallel methods does TPL support?

twenty two. What are the forms of partitioning in parallel programming?

twenty three. Briefly describe what are the Parallel helper classes? What are the functions?

twenty four. How many commonly used concurrent collection classes are there?

25.When using the Parallel.Invoke method, what should we pay attention to in order to allow Action to interact with the WPF interface?

26. Briefly introduce the differences and connections between Web services and WCF.

27. Briefly introduce the hosting method and characteristics of WCF services.

28. What is a service contract, what is a data contract, and what characteristics are used to declare service contracts and data contracts respectively?

29. What are the implementation technologies when writing HTTP applications based on C/S? What are the characteristics of each?

(1). Implemented using pluggable protocols

(2). Implemented using HttpWebRequest and HttpWebResponse

(3).Implemented with WCF

30. What are the message exchange modes between WCF client and server?

41. What are the main features of TCP?

42. What are the common methods to solve TCP's messageless boundary problem?

43. Briefly describe the main differences between task-based asynchronous TCP programming and direct implementation with Thread.

44.Briefly answer the general steps for writing TCP server and client programs with WCF.

45. What are the main differences between UDP and TCP?

46. What is broadcasting? What is multicast? What's the difference between the two?

47. Briefly answer the steps to join and leave a multicast group using the UdpClient object.

48. Brief answer: What are the main differences between writing multicast programs using WCF and UDP and using the UdpClient class? What are the advantages and disadvantages of each of these two implementation methods?

49. Briefly describe the main differences between transaction queues and non-transaction queues.

50. How to use MSMQ to achieve duplex communication?

51. Briefly describe the uses of dead letter queues and virus message queues.


1. Briefly describe the characteristics of decentralized, centralized and distributed communication models.

Decentralized: There is no exchange or sharing of resources or information between independent systems. There was a lot of data redundancy and high maintenance costs, and it was eliminated.

Centralized: Data and management functions are concentrated on a single computer, and users can only operate through the terminal. The advantages are low hardware cost, no redundancy and inconsistency problems, and the disadvantages are low reliability and flexibility, and slow response.

Distributed: It is a mixture of decentralized systems and centralized systems, and is currently a relatively complete solution.

2. Briefly describe the advantages and disadvantages of C/S and B/S.

C/S is a thick client application programming architecture, and its main work is run on the client. The advantage is that you can make full use of the performance advantages of the local computer, but the disadvantage is that you need to install client software on the local computer.

B/S uses HTTP for communication. The advantage is that a single computer can access any web server, but the disadvantage is that the B/S mode has sandbox restrictions.

3. What is a socket? What types of sockets are there?

Sockets are the basic operating unit that supports TCP/IP network communication and are the endpoints for processes between different hosts to communicate. In a socket instance, not only the IP address and port of the local machine are saved, but also the IP address and port of the other party are saved. Information such as the network protocol used for communication between the two parties is also saved.

There are 3 types of sockets: streaming sockets, datagram sockets, and raw sockets.

4. What are the uses for digital ink? Do general WPF elements have digital ink related events?

Digital ink can be used to draw ink shapes on various devices such as computers, mobile phones, tablets, and car navigation systems. It can also be used to implement functions related to touch screens and handwriting applications.

All WPF elements support digital inking functionality.

5.Briefly describe which functions of smartphones and car navigation systems you have seen can be simulated on a PC or demonstrated as prototypes using digital ink?

Digital ink technology is used in smartphones for functions such as sliding buttons to unlock the phone, sliding apps to close programs, hand-drawing tablets, and handwriting input.

Digital ink technology is also used in car navigation systems to realize drag-and-drop movement of maps, setting departure points, destinations, and handwriting input.

All the above functions can be simulated on a PC.

6. What is the difference between a process and a thread? Why use multithreading? In what situations is multithreading suitable?

A process is an executing program, and a thread is one or more execution streams within a process. Multithreading allows multiple tasks to be executed simultaneously. Multithreading technology can be used when performing consecutive operations that take a long time to complete, or when waiting for a response from the network or other I/O devices.

7. What are the differences and connections between foreground threads and background threads? How to determine whether a thread belongs to the foreground thread or the background thread? How to set a thread as background thread?

A thread is either a foreground thread or a background thread. The difference between the two is that background threads will not affect the termination of the process, while foreground threads will affect the termination of the process.

8. What is synchronization? Why synchronization is needed? What statements does C# provide to easily implement code synchronization?

When a statement is executed, the code following it will not be executed until the statement is completed. This execution method is called synchronous execution. When multiple threads executing in parallel access certain resources at the same time, you must consider how to keep the multiple threads synchronized. The purpose of synchronization is to prevent deadlock and contention situations when multiple threads access certain resources at the same time. The lock statement provided by C# can easily implement code synchronization.

9. What is a thread pool? What are the benefits of using a thread pool?

The thread pool is a collection of threads that perform tasks in the background. The benefits are: for example, when a thread cannot enter the thread pool for execution, it is first put into the waiting queue, automatically determines which processor to use to execute a thread in the thread pool, and automatically adjusts Load balancing issues when these threads execute, etc. In addition, the thread pool always processes requested tasks asynchronously in the background without occupying the main thread or delaying the processing of subsequent requests in the main thread.

10. What are the differences and connections between application domains and processes?

A process can contain only one application domain or multiple isolated application domains at the same time. Multi-process is a function used at the operating system level, which consumes large resources and complicated detailed control; application domain is a function used at the application level, which is faster, consumes less resources and is more secure than directly using multi-process to achieve process management. , is lightweight process management.

11. What is encoding? What is decoding? Why are characters encoded and decoded? What classes does the .NET framework provide for character encoding and decoding?

The process of converting a sequence of characters into a sequence of bytes is called encoding; when these bytes are transmitted to the receiver of the network, the receiver converts the sequence of bytes into a sequence of characters, a process called decoding.

In network communications, in many cases, both communicating parties convey character information. However, character information cannot be passed directly from one end of the network to the other. The character information first needs to be converted into a byte sequence before it can be transmitted over the network. Therefore, the sender needs to perform encoding, and the corresponding receiver needs to perform decoding.

The Encoding class is generally used in the .NET framework to implement encoding and decoding. The Encoding class is located under the System.Text namespace. This class is mainly used to encode and decode character sets and convert one encoding format to another encoding format. If you have more detailed requirements, you can also use the Encoder and Decoder classes located in the same namespace as the Encoding class for encoding and decoding.

12. What are the data streams provided by .NET that inherit from the Stream class?

File stream (FileStream), memory stream (MemoryStream), network stream (NetworkStream), encrypted stream (CryptoStream).

13. Briefly describe the characteristics and implementation principles of symmetric encryption and asymmetric encryption.

Symmetric encryption:

(1) Use the same key to encrypt and decrypt data;

(2) The private key algorithm encrypts data in blocks, one data block at a time, and therefore supports data streaming.

The advantages of the symmetric encryption algorithm: high confidentiality, fast encryption and decryption speed, and suitable for encrypting large amounts of data.

Disadvantages of symmetric encryption: Both communicating parties use the same key and IV for encryption and decryption, and the sender needs to pass the key and IV to the receiver first. If the attacker intercepts the key and IV, he or she knows how to decrypt the data.

Asymmetric encryption:

(1) Use a private key that needs to be kept secret and a public key that can be disclosed to anyone. Data encrypted with the public key can only be decrypted with the private key, conversely, data encrypted with the private key can only be decrypted with the public key;

(2) Private key encryption algorithms use variable-length buffers, so streams cannot be used.

Advantages of asymmetric encryption: less vulnerable to attacks.

Disadvantages of asymmetric encryption: Since streams cannot be used, it is not suitable for encrypting large amounts of data.

14.What is a digital signature? What are digital signatures used for?

In applications, digital signatures can be used to implement data authentication and data integrity verification. Data authentication is to verify whether the data is sent by the person holding the private key; data integrity verification is used to verify whether the data has been modified during transmission.

The purpose of digital signature: After downloading a file through the Internet, verify whether the downloaded file is exactly the same as the original file. In other words, if two communicating parties want to ensure that the information comes from the other party and not from a third party, they need to use digital signatures for authentication. Additionally, digital signatures prevent a specific party from denying that a message was sent.

15.How is an asynchronous method that only contains the async and await keywords different from an asynchronous method called with Task.Run?

The async and await keywords are features provided by C# 5.0. An asynchronous method containing only the async and await keywords will not create a new thread, it just means to execute the specified task asynchronously in the current thread. The Task.Run method is a function provided by the .NET Framework 4.5, which uses a separate thread to perform a task in the thread pool.

16.What is the difference between calling methods when executing normal methods and asynchronous methods as tasks?

Ordinary methods must be called using the Task.Run method, or using the constructor of the Task or Task<TResult> classes to explicitly create a Task instance and then start it. Asynchronous methods do not need to be called with the Task.Run method.

17.What is the difference between Action and Func delegates?

The Action delegate encapsulates methods without return values ​​(with 0 to 16 input parameters and the return type is void), and the Func delegate encapsulates methods with return values ​​(with 0 to 16 input parameters and the return type is TResult).

18.How many ways are there to create tasks?

There are 4 ways.

(1) Use the Task.Run method to implicitly create and execute tasks;

(2) Use the async and await keywords to implicitly create asynchronous tasks;

(3) Use the scheduler of the WPF control to implicitly create and execute tasks;

(4) Create a task by explicitly calling the constructor of Task or Task<TResult>.

19. How many timers are available in WPF applications?

There are 3 types.

(1) System.Timers.Timer class;

(2)System.Windows.Threading.DispatcherTimer类;

(3) System.Threading.Timer class.

20. Briefly describe the mechanism to implement the task cancellation function.

System.Threading.CancellationTokenSource is used to create cancellation notifications, called cancellation sources.

The System.Threading.CancellationToken structure is used to propagate notification that an operation should be canceled, called a cancellation token.

The code that calls the task can use the CancellationTokenSource class to create a cancellation source before assigning the task. In the code that calls the task, cancellation notification can be issued through the cancellation source's Cancel method, which sets the IsCancellationRequested property to true on each copy of the cancellation token.

After the method executing the task receives the cancellation notification, it can terminate the execution.

twenty one. What parallel methods does TPL support?

TPL supports data parallelism, task parallelism and parallel query (PLINQ).

twenty two. What are the forms of partitioning in parallel programming?

Partition by range; partition by block; dynamic partition; custom partition.

twenty three. Briefly describe what are the Parallel helper classes? What are the functions?

Helper classes include ParallelOptions, ParallelLoopState, ParallelLoopResult, CancellationToken, and CancellationTokenSource.

The ParallelOptions class is used to provide operation options for methods of the Parallel class. Common attributes include: CancellationToken, MaxDegreeOfParallelism, and TaskScheduler.

The ParallelLoopState class is used to interact iterations of a Parallel loop with other iterations. Commonly used properties and methods are: IsExceptional property, IsStopped property, Break method, Stop method.

The ParallelLoopResult class is used to provide the completion status of the Parallel loop. Common properties include: IsCompleted, LowestBreakIteration.

CancellationTokenSource is used to create cancellation notifications, called cancellation sources.

The CancellationToken structure is used to propagate notification that an operation should be canceled, called a cancellation token.

twenty four. How many commonly used concurrent collection classes are there?

ConcurrentBag<T>;ConcurrentQueue<T>;ConcurrentStack<T>;ConcurrentDictionary<TKey, TValue>。

25.When using the Parallel.Invoke method, what should we pay attention to in order to allow Action to interact with the WPF interface?

You cannot use the default task scheduler, but associate the task scheduler with the current synchronization context of WPF by setting the parallel option. The relevant code is as follows.

ParallelOptions options = new ParallelOptions();

options.TaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();

26. Briefly introduce the differences and connections between Web services and WCF.

Web service is an application service that uses XML as the data exchange format and is deployed on the Web server. In the Web service, methods that can be called by various client programs can be exposed.

WCF is a distributed application technology framework and programming model launched by Microsoft that conforms to SOA ideas. It is a runtime service system based on the concept of message communication.

Web services are only one part of WCF. In addition to Web services, WCF also includes various other services.

27. Briefly introduce the hosting method and characteristics of WCF services.

WCF service hosting methods include: using IIS or WAS to carry, using Windows services to carry, and self-hosting. Its characteristics are as follows.

(1) Using IIS or WAS to carry: This is the most commonly used hosting method, which is characterized by simple and convenient deployment.

(2) Hosted using Windows services: WCF services created using the [WCF Service Library] template are hosted using Windows processes.

(3) Self-hosting: refers to developers writing their own code to implement the work of hosting WCF. The advantage is that it is flexible to implement and requires minimal environmental support. The disadvantage is that advanced host management functions require writing your own code to implement them.

28. What is a service contract, what is a data contract, and what characteristics are used to declare service contracts and data contracts respectively?

The service contract refers to which services WCF exposes to the client. The WCF server exposes the following content to the client through the service agreement: operation method, message exchange mode, communication protocol adopted, and serialization format. Service contracts are jointly declared using the ServiceContract attribute and the OperationContract attribute.

The data contract describes what data is included in the message, that is, what data can be serialized into XML and transmitted to the client. Data contracts are jointly declared using the DataContract attribute and the DataMember attribute.

29. What are the implementation technologies when writing HTTP applications based on C/S? What are the characteristics of each?

(1). Implemented using pluggable protocols

Pluggable protocols are implemented using the WebRequest and WebResponse classes. These two classes are the base classes of various classes related to specific communication protocols, and provide basic methods such as uploading and downloading. In other words, no matter which communication protocol is used, these two pairs can be used to implement it.

(2). Implemented using HttpWebRequest and HttpWebResponse

The HttpWebRequest class and the HttpWebResponse class are provided for HTTP and are inherited from the WebRequest class and the WebResponse class respectively.

(3).Implemented with WCF

Programming HTTP applications using WCF and the task-based programming model is a recommended approach in service-oriented programming.

The first two methods are traditional programming models. In actual projects, if traditional programming models are used to implement them, in addition to business processing, many details also need to be completed by programmers themselves, such as load balancing, network monitoring, and security. Management, prevention of attacks, etc. When using WCF to implement it, programmers only need to deal with business logic, and other work can be done internally by WCF.

30. What are the message exchange modes between WCF client and server?

Request/reply mode, one-way mode, duplex mode.

41. What are the main features of TCP?

(1) One-to-one communication. Once the communicating parties establish a TCP connection, either party in the connection can send data to and receive data from the other party. Each TCP connection can only have two endpoints and can only communicate one-to-one.

(2) Safe sequential transmission. The data transmitted through the TCP connection can ensure that the data reaches the receiver accurately without errors, loss, or duplication, and the order in which each data arrives is the same as the order in which the data is sent.

(3) Send data through byte stream. When using TCP to transmit data, the data is transmitted in the form of a byte stream.

(4) The transmitted data has no message boundaries.

42. What are the common methods to solve TCP's messageless boundary problem?

(1) Send a fixed-length message.

(2) Send the message length together with the message.

(3) Use special markers to separate messages.

43. Briefly describe the main differences between task-based asynchronous TCP programming and direct implementation with Thread.

When using Thread directly to implement TCP programming, it is implemented with multi-threading as the goal, and developers need to manage multi-threads themselves. Therefore, developers must be proficient in all the details of multi-thread implementation before they can write actual TCP applications. Task-based asynchronous TCP programming implements TCP programming through tasks. When implemented with multi-tasking, even if developers are not familiar with all technical implementation details such as multi-threading, thread pools, resource conflicts, and load balancing, they can still quickly write Actual TCP applications, and the robustness of the program is much higher than directly using multi-threading.

44.Briefly answer the general steps for writing TCP server and client programs with WCF.

The general steps for writing a TCP server-side application using WCF are as follows.

1) Create a WPF application and add WCF service to the project.

2) Define the protocol for communication between the server and the client in the WCF service interface file, and implement the protocol methods that need to be implemented on the server.

3) Modify the server configuration file, start the WCF service, wait for the client to call and communicate with the client.

4) Determine whether to close the WCF service based on the actual situation.

The general steps for writing a TCP client application using WCF are as follows.

1) Create a WPF application and add a service reference.

2) Implement the WCF service definition and the protocol methods that need to be implemented on the client side.

3) Call the WCF service to communicate with the server.

45. What are the main differences between UDP and TCP?

UDP is a simple, datagram-oriented connectionless protocol that provides fast but not necessarily reliable transmission services. Like TCP, UDP is also a transport layer protocol built on the underlying IP protocol. Compared with TCP, UDP has the following characteristics.

(1) UDP not only supports one-to-one communication, but also supports one-to-many communication.

(2) UDP transmission speed is faster than TCP.

(3) UDP has message boundaries, and there is no need to consider message boundary issues when using UDP.

(4) UDP does not ensure that the order of sending and receiving data is consistent.

(5) UDP is not as reliable as TCP.

46. What is broadcasting? What is multicast? What's the difference between the two?

Broadcasting means sending a message to multiple devices at the same time, and all devices in the subnet can receive the message from the sender. Each broadcast message contains a special IP address. There are two types of broadcast message addresses: local broadcast and global broadcast. Local broadcast can send broadcast messages to all devices in the subnet, and other networks will not be affected by local broadcast.

Multicast is to send messages from one device to a selected subset of devices within the local network or the entire network, that is, to those devices that join a designated multicast group.

The difference between the two: Broadcast can only send messages to the subnet where it is located, and it sends messages to all devices in the subnet. It has no purpose. It not only causes network load, but also consumes high resources. Multicast is to send messages to machines that join a specific multicast group. The messages are purposeful and resource consumption is not high.

47. Briefly answer the steps to join and leave a multicast group using the UdpClient object.

In the UDP protocol, broadcast and multicast are implemented with the help of special IP addresses. Especially multicast is special. For IPv4, multicast refers to broadcasting within the Class D IP address range from 224.0.0.0 to 239.255.255.255.

When joining a multicast group, you first create a UdpClient object and then use the JoinMutiCastGroup method to join the multicast group. When exiting a multicast group, use the DropMulticastGroup method of the UdpClient object to exit the multicast group. The parameter indicates the IPAddress object to exit the multicast group.

48. Brief answer: What are the main differences between writing multicast programs using WCF and UDP and using the UdpClient class? What are the advantages and disadvantages of each of these two implementation methods?

When writing a multicast program using UDP, you need to send messages to a special IP address. When writing a multicast program using the UdpClient class, you need to join a specific multicast group before you can receive data sent from the multicast sender. The specific implementation mainly uses the JoinMutiCastGroup method of the UdpClient class to join the multicast group. The sender uses the Send method of the UdpClient object to send data to the multicast address, and the receiving end uses the Receive method of the UdpClient object to receive data. To exit the multicast group, just call the DropMulticastGroup method.

When writing a multicast program using WCF and UDP, you need to use the WCF service definition and implement the communication protocol, and then send the message to the multicast address, so that any client that implements the WCF service definition and implements the communication protocol can receive the message.

When implemented using the UdpClient class, it is mainly implemented with multi-threading and multi-tasking, which is easy to understand, but requires more implementation details to be considered. The WCF mode further encapsulates UDP multicast programming, reduces the complexity of programming, and is more suitable for UDP communication programs in distributed environments.

49. Briefly describe the main differences between transaction queues and non-transaction queues.

A transaction is a series of indivisible operations performed for a single logical job with the purpose of ensuring data integrity and business logic consistency without destroying the correctness and integrity of the data. Transactions allow a group of operations to be grouped together. If one operation fails, all operations will fail.

Non-transaction queues are temporary, messages are stored in memory, and transactions are not used to protect operations on messages. Once there is a problem with the server, or the caller may have an exception, the message will be lost.

The transaction queue is persistent and messages are saved on disk. When the server crashes, restarts, or crashes, the messages can be read after the system recovers. At the same time, message publishing, retrieval and deletion are all within the scope of environmental transactions, thus ensuring the reliability of messages.

50. How to use MSMQ to achieve duplex communication?

Since the default queuing binding in WCF does not support duplex communication using queues, when WCF and MSMQ are bound, all service operations are required to be one-way, so MSMQ does not support duplex communication. If you want to use MSMQ to achieve duplex communication, you can use two one-way protocols to solve it. The basic method is as follows:

The service agreement is also defined and implemented on the client, and the server can call the services provided by the client through the proxy. At this time, the server becomes the client, and the client becomes the server, thereby achieving a function similar to duplex communication.

51. Briefly describe the uses of dead letter queues and virus message queues.

The dead letter queue is a special queue used to record queued messages that failed to be delivered. The message delivery failure may be due to network failure, the queue has been deleted, the queue is full, authentication failure, or failure to deliver on time within the message time to live (TTL). caused by. In order to record the failure of the message to be transmitted to the target queue, the failed message will be transmitted to the dead letter queue. Dead letter queue messaging handles delivery failures, ensuring you are notified if a message fails to be delivered to the destination queue.

A virus message queue is a message queue that records that the maximum number of delivery attempts to an application has been exceeded. Virus message queues can be used to solve the problem of the service being unable to process a message when it is read, and to terminate the delivery of messages that are repeatedly read from the queue when the transaction from which the message is read is terminated.

Guess you like

Origin blog.csdn.net/wyd_333/article/details/131724737