Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of C # Asynchronous Communication

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you the "sample Analysis of C# Asynchronous Communication", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample Analysis of C# Asynchronous Communication".

First of all, let's take a look at the understanding of the concept and application of C # asynchronous communication: using Socket in network programming, it is often necessary to listen to a port for a long time to obtain Socket connection, and then perform related operations.

But at this point, the main thread is blocked and cannot do anything else. To solve this problem, the Socket class provides some methods that support asynchronous operations, as shown in Table 17-10.

The following table shows the methods that support asynchronous operations in the Socket class

Square method

State clearly

BeginAccept ()

Start an asynchronous request to create a new Socket object to accept incoming connection requests

EndAccept ()

End an asynchronous request to create a new Socket object to accept incoming connection requests

BeginConnect ()

Start an asynchronous request to a remote host

EndConnect ()

End an asynchronous request to a remote host

BeginDisconnect ()

Start an asynchronous request to disconnect from the remote host

EndDisconnect ()

End an asynchronous request to disconnect from a remote host

BeginReceive ()

Start receiving data asynchronously from the connected Socket

EndReceive ()

End receiving data asynchronously from the connected Socket

BeginReceiveFrom ()

Start receiving data asynchronously from a specified network device

EndReceiveFrom ()

End receiving data asynchronously from a specified network device

BeginSend ()

Start sending data asynchronously to the connected Socket

EndSend ()

End the asynchronous transmission of data

BeginSendFile ()

Start sending files asynchronously to the connected Socket

EndSendFile ()

End the asynchronous sending of the file

BeginSendTo ()

Send data asynchronously to a specific remote host

EndSendTo ()

End asynchronous transmission of data to a remote host

As can be seen from the above table, these methods appear in pairs. These methods can avoid congestion in network communication. The mechanism for using these methods is to register a callback function in the method at the beginning of Begin, call the callback function when the corresponding event occurs, and call the method at the beginning of the corresponding End in the callback function.

Let's take BeginAccept () and EndAccept () as examples to illustrate the use of asynchronous methods. The declaration of BeginAccept () is shown below.

Public IAsyncResult BeginAccept (AsynCallback callback, object state)

The * parameters are objects that are asynchronously delegated to AsynCallb and ack,state contains the status information of this request.

The EndAccept () method has three overloaded forms, as shown below.

Public Socket EndAccept (IAsyncResult asynresult); public Socket EndAccept (out byte [] buffer, IAsyncResult asynresult); public Socket EndAccept (out byte [] buffer, out int bytesTransferred, IAsyncResult asynresult,)

Asynresult is used to store state information for this asynchronous operation and any user-defined data; buffer represents the bytes of data that need to be transferred

BytesTransferred represents the number of bytes that have been transferred. The out parameter here is similar to the use of ref in that it indicates passing references. The difference is that ref is the address where the parameter is passed and out is the return value.

The specific usage is as follows.

Private AsyncCallback callbackOnAccpt; / / Custom callback method private Socket s;... / / C # asynchronous communication s=new Socket (AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); callbackAccpt = new AsyncCallback (this.OnAccept); / / instantiated callback method s.BeginAccept (callbackOnAccpt, this.s); / / start asynchronous request. Private void OnAccept (IAsyncResult asynresult) {/ / C# asynchronous communication. S.EndAccept (asynresult); / / end asynchronous request. }

BeginConnect () and EndConnect () are used for asynchronous connection requests to remote hosts. The declaration of BeginConnect () is shown below.

Public IAsyncResult BeginConnect (EndPoint remoteEP, AsyncCallback callback,object state); public IAsyncResult BeginConnect (IPAddress address, int port,AsyncCallback requestedcallback,object state); public IAsyncResult BeginConnect (IPAddress [] address, int port,AsyncCallback requestedcallback,object state); / / C # Asynchronous Communication public IAsyncResult BeginConnect (string host,int port,AsyncCallback requestedcallback,object state)

Each of its overloaded forms contains an AsyncCallback parameter, which is characteristic of the method at the beginning of this Begin; remoteEP represents the IP and port of the remote host.

The declaration of EndConnect () is shown below.

Public Socket EndConnect (IAsyncResult asynresult)

The meaning of its parameters is exactly the same as EndAccept ().

BeginSend () indicates that data is started to be sent asynchronously to the connected Socket, and its most common declaration is as follows.

Public IAsyncResult BeginSend (byte [] buffer, int offset,int size,SocketFlags socketFlags, AsyncCallback callback,object state)

Buffer represents the data to be sent, offset represents the location of the data to be sent in the buffer, size is the number of bytes sent, and socketFlags refers to the bitwise combination of SocketFlags values.

The declaration of EndSend () is shown below.

Public Socket EndSend (IAsyncResult asynresult)

BeginReceive () means to start receiving data asynchronously from the connected Socket, and its common declaration is as follows.

Public IAsyncResult BeginReceive (byte [] buffer, int offset,int size,SocketFlags socketFlags, AsyncCallback callback,object state)

Its parameters are exactly the same as BeginSend (), so I won't repeat them here.

The declaration of EndReceive () is shown below.

Public Socket EndReceive (IAsyncResult asynresult)

The implementation of asynchronous communication in Socket of C # asynchronous communication is much more difficult than synchronization. I will not give an example here. To put it simply, in synchronization, if the main thread calls a method, the main thread must wait until the method thread has finished executing before it can proceed. Therefore, the execution of the main thread and the method thread is serial. In asynchronism, both can be executed at the same time, and their execution is parallel.

The above is all the contents of the article "sample Analysis of C# Asynchronous Communication". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report