For the application that completes the port, can the port be used only for servers?

Source: Internet
Author: User

DWORD winapi serverworkerthread (lpvoid lpparam)
{
Handle completionport = (handle) lpparam;
DWORD bytestransferred;
Lpoverlapped;
Lpper_handle_data perhandledata = NULL;
Lpper_io_data periodata = NULL;
DWORD sendbytes;
DWORD recvbytes;
DWORD flags;
Bool Bret = false;

While (true)
{
Bret = getqueuedcompletionstatus (completionport,
& Bytestransferred,
(Pulong_ptr)
& Perhandledata,
(Lpoverlapped *)
& Lpoverlapped,
Infinite );

// If the check is successful, use the macro containing_record.
Periodata = (lpper_io_data) containing_record (lpoverlapped,
Per_io_data,
Overlapped );

// Check to see if an error has occurred on the socket.
If (0 = bytestransferred)
{
Closesocket (perhandledata-> socket );
Globalfree (perhandledata );
Globalfree (periodata );

Continue;
}

// Data Processing
// Success !!! Here we receive data from the client.
// Cout <periodata-> databuf. Buf <Endl;
// MessageBox ("sffdasf ");
Afxmessagebox (periodata-> databuf. BUF );

Flags = 0;

// Create a single I/O operation data for the next overlapping call
Zeromemory (& (periodata-> overlapped), sizeof (overlapped ));

Periodata-> databuf. Len = 1024;
Periodata-> databuf. Buf = periodata-> buffer;
Periodata-> operationtype = 0; // read
Wsarecv (perhandledata-> socket,
& (Periodata-> databuf ),
1,
& Recvbytes,
& Flags,
& (Periodata-> overlapped ),
Null );
}

Return 0;
}
Int startwinsock (void)
{
Wsadata WSA;
Return wsastartup (makeword (), & WSA ); //
}
Void ccptest_netdlg: onbnclickedbutton1 ()
{
// Todo: add the control notification handler code here
System_info systeminfo;
Cstring ctemp;

Struct sockaddr_in internetaddr;/* target address */
Socket listen;
Int I;
Wsadata WSD;

Handle completionport;
// Load Winsock

Startwinsock (); // makeword (2, 2), & WSD );

// Step 1:
// Create an I/O completion port

Completionport = createiocompletionport (
Invalid_handle_value, null, 0, 0 );

// Step 2:
// Determine how many processors are on the System

Getsysteminfo (& systeminfo );

// Step 3:
// Create worker threads based on the number
// Processors available on the system. For this
// Simple case, we create one worker thread for each
// Processor.

For (I = 0; I <systeminfo. dwnumberofprocessors; I ++)
{
Handle threadhandle;

// Create a server worker thread, and pass
// Completion port to the thread. Note:
// Serverworkerthread procedure is not defined
// In this listing.

Threadhandle = createthread (null, 0,
Serverworkerthread, completionport,
0, null );

// Close the thread handle
Closehandle (threadhandle );
}

// Step 4:
// Create a listening socket

Listen = wsasocket (af_inet, sock_stream, 0, null, 0,
Wsa_flag_overlapped );

Internetaddr. sin_family = af_inet;
Internetaddr. sin_addr.s_addr = htonl (inaddr_any );
Internetaddr. sin_port = htons (0, 5150 );
BIND (Listen, (psockaddr) & internetaddr,
Sizeof (internetaddr ));

// Prepare socket for listening

Listen (Listen, 5 );

While (true)
{
Per_handle_data * perhandledata = NULL;
Sockaddr_in saremote;
Socket accept;
Int remotelen;
// Step 5:
// Accept connections and assign to the completion
// Port

Remotelen = sizeof (saremote );
Accept = wsaaccept (Listen, (sockaddr *) & saremote, & remotelen, null, 0 );

// Step 6:
// Create per-handle data information structure
// Associate with the socket
Perhandledata = (lpper_handle_data)
Globalalloc (gptr, sizeof (per_handle_data ));

Printf ("socket Number % d connected/N", accept );
Perhandledata-> socket = accept;
Memcpy (& perhandledata-> clientaddr, & saremote, remotelen );

// Step 7:
// Associate the accepted socket with
// Completion port

Createiocompletionport (handle) Accept,
Completionport, (DWORD) perhandledata, 0 );

// Step 8:
// Start processing I/O on the accepted socket.
// Post one or more wsasend () or wsarecv () cballs
// On the socket using overlapped I/O.
Wsasend (listen,

}


}
Top

2 floor clicksoft (good)The score is 0 at 18:19:00.

Yes, but is it necessary? Top

Tccsdn (Zile) on the third floor)The score is 0 at 18:20:55.

Uptop

4 floor laiyiling (Graphics ◎ multimedia)The score is 0 at 19:29:29.

There are many examples of finished disconnections on the Internet
Http://www.vckbase.com/document/viewdoc? Id = 980top

Anycom () on the fifth floor ()The score is 0 at 23:00:41.

Why is it unnecessary ?????? Top

6 floor jasic2002 ()The score is 0 at 08:08:44.

Microsoft recommends that the server end use the complete port and the client end use the message-based socket communication! Top

Krh2001 (bianchenglang) on the 7th floor)The score is 0 at 08:11:06.

For servers that need to connect to a large number of clients, port completion is a good choice.

It is unnecessary for the client. Top

Stonex_2000 (PRISM) on the eighth floor)The score is 0 at 11:07:29.

Yes, but the completion port cannot be used on Win98. Are all clients Win2k or above? Top

Wy99sinacom (road) on the 9th floor)The score is 0 at 14:49:43.

No need to top

Llm06 (blacksheep) on the 10th floor)The score is 0 at 15:11:51.

Yes.
You only need to associate your socket with the completion port, then deliver asynchronous requests, and process the completed I/O requests in the thread pool.
Www.codeproject.com has an example of a completed port, which can still be written, but there are many bugs. Download and learn. Top

11 floor dingpiao (.......)The score is 0 at 22:49:07.

Does the client need to process many connections?
Why don't you become a server? Top

12 floor shicheng521 ()The score is 0 at 15:36:53.

A single client has so many connections. Isn't the server unable to handle it?
Do you want to destroy it? Top

Practise_think on the 13th floor ")The score is 0 at 22:13:17.

You can try it !!! Top

Anycom () on the 14th floor ()The score is 0 at 16:07:41.

Even if you want to purchase a Web page with tens of thousands of servers at the same time ..........

But it seems that there are very few routines for completing active port connection !!! Top

Anycom () on the 15th floor ()The score is 0 at 16:18:42.

Even if you want to connect to a server with tens of thousands of children, you can collect web pages ..........

But it seems that there are very few routines for completing active port connection !!!
Top

16/F anycom ()The score is 0 at 16:19:14.

Hope that the high people will give top routines

Flashboy on the 17th floor)The score is 0 at 23:25:03.

A typical example of active port connection is "Proxy Server ". you can find out the code of the proxy server that completes the port model, which includes both response connection and active connection. however, if you only want to create a pure client, it is recommended that you do not need to use iocp as complicated. You can use the overlapping Io model or select model. the advantage of iocp is reflected in the response and management of a large number of concurrent connections. Therefore, the advantage of iocp is even more obvious for servers. top

Smallbigcat on the 18 th Floor)The score is 0 at 17:05:58.

The firewall of the server is still usable.

1: Create a response first
2: connect to the peer Server
3: Set the socket handle to the iocp handle.
4: vote for a wsarecv operation top

Tgame on the 19th floor (drunk from now on)The score is 0 at 18:06:24.

Smallbigcat (...) That's true.

Anycom () on the 20th floor ()The score is 0 at 20:23:51.

Even if you want to connect to eight thousand servers with tens of thousands of children at the same time. Top

21 floor lianglp (looking for Golden splitting points)The score is 0 at 22:12:26.

It doesn't matter. Either the server or the client can use TCP or UDP,
In fact, iocp is just a model for implementing Io input and output. For it, I don't know at all
It is a socket handle or other handle. In fact, this model is a passive model,
Whether the IO handle can be associated with iocp depends on whether the IO handle supports it.
Top

Hellboy (INT argc, char * argv []) on the 22nd floorThe score is 0 at 17:15:27.

After successful connect,

Createiocompletionport (handle) client,
Completionport, (DWORD) perhandledata, 0 );
Add the successfully connected socket to the completion port. wsarecv will be notified of the wsasend called by the socket later.
Write the specific code yourself.

Our company simulates a large number of client-side test code and uses the port used for connection.
Good results.
 

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.