Complete port usage in Windows

Source: Internet
Author: User

Recently, I used UDP in Windows to receive data. I checked Baidu and said it was easy to use the port. I started to think it was complicated to use the port in windows, it is easier to find and use it,

After the port is used, initialize and create a thread in the main function:

G_hcompletionport = createiocompletionport (invalid_handle_value, null, 0, 0 );
Createthread (null, 0, (lpthread_start_routine) workthread,
Null, 0, null );

You need to define a structure for receiving data:

Typedef struct _ per_handle_data
{
Socket S;
Sockaddr_in ADDR;
Char Buf [buffer_size];
Int noperationtype;
} Per_handle_data;

Then assign a per_handle_data and use createiocompletionport to add the received socket handle to the completion port:

Per_handle_data * pperhandle = (per_handle_data *) malloc (sizeof (per_handle_data ));

Pperhandle-> S = socket (af_inet, sock_dgram, 0 );
Memset (& pperhandle-> ADDR, 0, sizeof (struct sockaddr_in ));
Pperhandle-> ADDR. sin_family = af_inet;
Pperhandle-> ADDR. sin_port = 8888; // UDP port number
Pperhandle-> noperationtype = 1;

BIND (pperhandle-> S, (struct sockaddr *) & pperhandle-> ADDR, sizeof (struct sockaddr ));

Createiocompletionport (handle) pperhandle-> S, g_hcompletionport, (ulong_ptr) pperhandle, 0 );

When an overlapped is allocated, wsarecv is used to receive data.

Overlapped * Pol = (overlapped *) malloc (sizeof (overlapped ));
Wsabuf Buf;
Buf. Buf = pperhandle-> Buf;
Buf. Len = buffer_size;
DWORD dwrecv;
DWORD dwflags = 0;

Ret =: wsarecv (pperhandle-> S, & Buf, 1, & dwrecv, & dwflags, Pol, null );
If (Ret <0)
Errcode = wsagetlasterror ();

While (1)

{

 

}

Use getqueuedcompletionstatus in the thread function to obtain and process the port status.

Void * workthread (void * name)
{
DWORD dwtrans;
Overlapped * poverlapped;
Per_handle_data * pperhandle;
// Overlappedplus * lpplus;
While (1)
{
Bool Bok = getqueuedcompletionstatus (g_hcompletionport, & dwtrans, (pulong_ptr) & pperhandle, & poverlapped, 0 );
If (! Bok)
{
Sleep (1 );
Continue;
}
If (pperhandle-> noperationtype = 1) // only processes the received data. pperhandle-> Buf is the content of the received data, and dwtrans is the length of the received data.
{

Processrecv (pperhandle-> Buf, dwtrans );
Wsabuf Buf;
Buf. Buf = pperhandle-> Buf;
Buf. Len = buffer_size; // parameter 2
Pperhandle-> noperationtype = 1;
DWORD nflags = 0; // parameter 4
: Wsarecv (pperhandle-> S, & Buf, 1, & dwtrans, & nflags, poverlapped, null); // continue to use wsarecv to receive data

Sleep (1 );
}

}

This is a simple program that uses the port to Implement UDP data reception.

 

 

 

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.