Iocp implementation steps

Source: Internet
Author: User

<1> iocp implementation steps

If you understand how iocp works, it is easy to implement.

The implementation steps are as follows:

1. Create an iocp

2. Create a socket (the socket can be obtained by accept)

3. Associate the socket with iocp

4. Socket submits various requests to iocp

5. After the iocp operation is completed, return the result to the socket

6. Repeat Steps 3 and 4 until the socket is closed.

It takes only a few steps, but it requires a lot of code to implement it. The following describes how to create a client socket. The principle is explained here. The parameters and return values of the function are ignored first.

1. // create iocp

// Use the createiocompletionport function to create iocp

// Note parameter settings

M_hiocp = createiocompletionport (invalid_handle_value, null, (ulong_ptr) 0, 0 );

// M_hiocp indicates a complete port.

2. // create a connection socket

// Use the wsasocket function to create a socket. wsa_flag_overlapped must be specified.

M_sockclient =: wsasocket (af_inet,

Sock_stream, ipproto_tcp, null, null, wsa_flag_overlapped );

// M_sockclient = socket (af_inet, sock_stream, ipproto_tcp); // You can also

// The default value is wsa_flag_overlapped.

 

// The following binding is very important and easy to miss. (If the binding is missing, the error code 10022-an invalid parameter is provided during connextex.

Sockaddr_in local_addr;

Zeromemory (& local_addr, sizeof (sockaddr_in ));

Local_addr. sin_family = af_inet;

Int IRT =: BIND (m_sockclient, (sockaddr *) (& local_addr), sizeof (sockaddr_in ));

// M_sockclient is the socket created

3. // associate the socket with iocp

// Use createiocompletionport to associate the socket with iocp.

Createiocompletionport (handle) m_sockclient, m_hiocp, (ulong_ptr) m_sockclient, 0 );

// Sockclient associated with m_hiocp

// (Ulong_ptr) m_sockclient identifies the socket operation later

4. // The socket submits various required requests to iocp. The connection request is submitted here.

// The code is long and the entire function is copied.

Bool ciocp_clientdlg: connecttoserver ()

{

//----------------------

Lpfn_connectex m_lpfnconnectex = NULL;

DWORD dwbytes = 0;

Guid guidconnectex = wsaid_connectex;

// Focus on obtaining the pointer of the connectex Function

If (socket_error = wsaioctl (m_sockclient, sio_get_extension_function_pointer,

& Guidconnectex, sizeof (guidconnectex ),

& M_lpfnconnectex, sizeof (m_lpfnconnectex), & dwbytes, 0, 0 ))

{

Trace ("wsaioctl is failed. Error code = % d", wsagetlasterror ());

Return false;

}

Myoverlapped * pmyoverlapped = new myoverlapped; // carrier of socket and I/O communication

Pmyoverlapped-> operatetype = op_connect; // you can specify the request type based on the request type to obtain the I/O result. //

Pmyoverlapped-> hevent = NULL; // non-event model

// Set the connection Destination Address

Sockaddr_in addrpeer;

Zeromemory (& addrpeer, sizeof (sockaddr_in ));

Addrpeer. sin_family = af_inet;

Addrpeer. sin_addr. s_addr = inet_addr ("192.168.0.15 ");

Addrpeer. sin_port = htons (5400 );

Int nlen = sizeof (addrpeer );

Pvoid lpsendbuffer = NULL;

DWORD dwsenddatalength = 0;

DWORD dwbytessent = 0;

// Key

Bool bresult = m_lpfnconnectex (m_sockclient,

(Sockaddr *) & addrpeer, // [in] peer address

Nlen, // [in] the length of the Peer address

Lpsendbuffer, // content to be sent after [in] connection, not required here

Dwsenddatalength, // The number of bytes of the content sent by [in], which is not required here

& Dwbytessent, // The number of bytes sent by [out], which is not required here

(Overlapped *) pmyoverlapped); // [in] This is complicated and will be explained in the next article.

If (! Bresult) // return value processing

{

If (wsagetlasterror ()! = Error_io_pending) // call failed

{

Trace (text ("connextex error: % d/N"), wsagetlasterror ());

Return false;

}

Else; // The operation is pending (in progress... )

{

Trace0 ("wsagetlasterror () = error_io_pending/N"); // The operation is in progress

}

}

Return true;

}

// The focus of this function is the wsaioctl function (to obtain the connectex function address) and m_lpfnconnectex function pointer (to call connectex, that is, to submit a request to the I/O to connect to the other party ).

// In this way, the CONNECT request is submitted to I/O.

5. // return the result to the socket after the iocp operation is completed

// After the request is submitted, how does sokcet get the result? It depends on taking the initiative to go to the I/O exit and wait until the data is obtained. Generally, one or more threads are waiting for results. Focus on the // getqueuedcompletionstatus function.

Void ciocp_clientdlg: iocpworkerthread ()

{

Myoverlapped * lpoverlapped = NULL;

DWORD dwbytetransfered = 0;

Ulong_ptr * perhandlekey = NULL;

While (1)

{

Lpoverlapped = NULL;

If (m_hiocp = NULL)

{

Break;

}

// The following function call is to wait at the I/O exit and obtain the I/O operation result.

Bool bresult = getqueuedcompletionstatus (

M_hiocp, // specifies the iocp from which or the local data is stored.

& Dwbytetransfered, // The number of bytes of data sent or received

(Pulong_ptr) & perhandlekey, // specifies an association value when the socket is associated with iocp

(Lpwsaoverlapped *) & lpoverlapped, // or the structure passed in by connectex

Infinite); // wait until there is a result

... // Process the data obtained from I/O. There are a lot of code, skipped!

}

}

// If connecex is successful, the call to getqueuedcompletionstatus ends, and m_sockclient is the socket that can be used to send and receive data.

6. // repeat Steps 3 and 4 until the socket is closed.

// Now you can use m_sockclient to send and receive data.

// Submit the following request for receiving

Wsarecv (socket, & lpoverlapped-> wsabuf, 1,

& Lpoverlapped-> dwbyterecvsend, & flags,

(Lpwsaoverlapped) lpoverlapped, null );

// Submit the following sending request

Int nresult = wsasend (

Socket,

& Pmyoverlapped-> wsabuf, // wsabuf pointer, send data here

1, // the size of the array pointed to by the wsabuf pointer

& Pmyoverlapped-> dwbyterecvsend, // actual number of bytes sent

0,

(Lpwsaoverlapped) pmyoverlapped, // overlapped Structure

0 );

The implementation steps of the iocp client are also the six points above. A socket can connect to the other party, mainly in connectex, getqueuedcompletionstatus to obtain whether the result of connectex is successful.

The above example shows how to implement a client, and the server implementation is similar, that is, to create a socket-> bind socket-> listen-> Accept the connection.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zougangx/archive/2010/05/06/5563872.aspx

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.