Methods provided in the socket class that support asynchronous operations

Source: Internet
Author: User
Tags socket

The use of socket in network programming, often need to listen to a certain port long, to obtain socket connection, and then perform related operations.

But at this point, the main thread will be blocked and unable to do anything else. To address this type of problem, the socket class provides methods that support asynchronous operations, as shown in table 17-10.

Table 17-10 methods that support asynchronous operations in the socket class

Method Description
BeginAccept () Begins an asynchronous request to create a new socket object to accept incoming connection requests
Endaccept () Ends an asynchronous request to create a new socket object to accept incoming connection requests
BeginConnect () Start an asynchronous request to the remote host
EndConnect () End an asynchronous request to a remote host
BeginDisconnect () Start asynchronous request disconnect from remote host
EndDisconnect () End an asynchronous request to disconnect from a remote host
BeginReceive () Start receiving data asynchronously from a connected socket
EndReceive () End asynchronously receive data from a connected socket
Beginreceivefrom () Start receiving data asynchronously from a specified network device
Endreceivefrom () Ends asynchronous receiving of data from a specified network device
BeginSend () Start sending data asynchronously to a connected socket
Endsend () End asynchronous sending of data
Beginsendfile () Start sending files asynchronously to a connected socket
Endsendfile () Ending asynchronous sending of files
Beginsendto () Asynchronously sends data to a specific remote host
Endsendto () End asynchronous sending of remote host data

As you can see from the table above, these methods appear to be in pairs. These methods can avoid the blocking phenomenon in network communication. The use mechanism of these methods is to register a callback function in the method that begins at begin, when the corresponding event occurs, the callback function is invoked, and the corresponding end-opening method is called in the callback function.

The following is an example of beginaccept () and endaccept () to illustrate the use of asynchronous methods. The declaration of BeginAccept () is shown below.

public IAsyncResult BeginAccept(AsynCallback callback,object state);

Where the first parameter is an asynchronous delegate asyncallb an object that Ack,state contains state information for 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 and any user-defined data for this asynchronous operation, and buffer represents the byte data that needs to be transmitted;

Bytestransferred represents the number of bytes that have been transferred. The out parameter here is similar to the use of ref, which indicates a pass-through reference. The difference is that ref is the address that passes the argument, and out is the return value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.