Network Programming: how to obtain the current status of various network connections

Source: Internet
Author: User

I have received a task in recent days and want to build a GPRS traffic statistics software under wince 6.0. Because I have never touched this aspect before, it is a headache and I have no idea at all, however, after several days of searching online, I found two solutions.

The first is to use the Windows IP helper related functions, which are in the iphlpapi. dll. If you are more accustomed to static calls, you can find the iphlpapi. h and iphlpapi. Lib in your SDK. It can be used in your project.

The second is to use raw socket. Calculate the data packet size one by one.

First, let's look at the first one. If you use dynamic calling, you need to define two structures. For detailed descriptions of these two structures, we can find them on msdn.

Http://msdn.microsoft.com/en-us/library/aa366836 (V = vs.85). aspx

Typedef struct _ mib_ifrow
{
Wchar wszname [max_interface_name_len];
DWORD dwindex;
DWORD dwtype;
DWORD dwmtu;
DWORD dwspeed;
DWORD dwphysaddrlen;
Byte bphysaddr [maxlen_physaddr];
DWORD dwadminstatus;
DWORD dwoperstatus;
DWORD dwlastchange;
DWORD dwinoctets;
DWORD dwinucastpkts;
DWORD dwinnucastpkts;
DWORD dwindiscards;
DWORD dwinerrors;
DWORD dwinunknownprotos;
DWORD dwoutoctets;
DWORD dwoutucastpkts;
DWORD dwoutnucastpkts;
DWORD dwoutdiscards;
DWORD dwouterrors;
DWORD dwoutqlen;
DWORD dwdescrlen;
Byte bdescr [maxlen_ifdescr];
} Mib_ifrow, * pmib_ifrow;

Typedef struct _ mib_iftable
{
DWORD dwnumentries; // connection format
Mib_ifrow table [any_size]; // connection information
} Mib_iftable, * pmib_iftable;

Then define the variable

Mib_ifrow * pifrow;

# Define max_interface_name_len 256
# Define maxlen_physaddr 8
# Define maxlen_ifdescr 256
# Define any_size 1

Then, call the getiftable function of the lplpapi to fill up the data structure. Note that getiftable must be called twice. The size must be determined for the first time before the second operation can be correctly filled.

If (lpgtiftable (pmit, & dwsize, 0) = error_insufficient_buffer)
{
Delete [] pmit;
Pmit = new mib_iftable [dwsize];
}

// Dwnumentries alwayse> = 1, and the last entry is loopback interface.
If (lpgtiftable (pmit, & dwsize, 0) = no_error)
{
For (INT I = 0; I <(INT) pmit-> dwnumentries; I ++)
{
Pifrow = (mib_ifrow *) & pmit-> table [I];

}
}

Next, you can determine which connection information is needed based on the content in the pifrow and process the information.

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.