Knowledge of Windows network programming winsock

Network programming in fact, I have been stuck in there udp tcp backgammon page was written in a local area network TCP implementations in large semester when painting directly on canvas

Then click the mouse is captured and then directly to complete a backgammon was thinking of network programming is very simple now I feel I still feel it is still too tender some difficulty speaking at the forum some time ago a lot of analysis of the virus found a lot network programming on a lot of things still do not know

For example, many models do not quite understand what exactly kepler then my cousin bought two books of which is the windows network and communication design

Then examples of which have very good (I'll just pasted)

#include <windows.h>
#include <stdio.h>

#include "Iphlpapi.h"
#pragma comment(lib, "Iphlpapi.lib")
#pragma comment(lib, "WS2_32.lib")

/////////////////////////////////////////
// 全局数据
u_char	g_ucLocalMac[6];	// 本地MAC地址
DWORD	g_dwGatewayIP;		// 网关IP地址
DWORD	g_dwLocalIP;		// 本地IP地址
DWORD	g_dwMask;			// 子网掩码

BOOL GetGlobalData()
{
	PIP_ADAPTER_INFO pAdapterInfo = NULL;
	ULONG ulLen = 0;

	// 为适配器结构申请内存
	::GetAdaptersInfo(pAdapterInfo,&ulLen);
	pAdapterInfo = (PIP_ADAPTER_INFO)::GlobalAlloc(GPTR, ulLen);

	// 取得本地适配器结构信息
	if(::GetAdaptersInfo(pAdapterInfo,&ulLen) ==  ERROR_SUCCESS)
	{
		if(pAdapterInfo != NULL)
		{
			memcpy(g_ucLocalMac, pAdapterInfo->Address, 6);
			g_dwGatewayIP = ::inet_addr(pAdapterInfo->GatewayList.IpAddress.String);
			g_dwLocalIP = ::inet_addr(pAdapterInfo->IpAddressList.IpAddress.String);
			g_dwMask = ::inet_addr(pAdapterInfo->IpAddressList.IpMask.String);
		}
	}

	printf(" \n -------------------- 本地主机信息 -----------------------\n\n");
	in_addr in;
	in.S_un.S_addr = g_dwLocalIP;
	printf("      IP Address : %s \n", ::inet_ntoa(in));

	in.S_un.S_addr = g_dwMask;
	printf("     Subnet Mask : %s \n", ::inet_ntoa(in));

	in.S_un.S_addr = g_dwGatewayIP;
	printf(" Default Gateway : %s \n", ::inet_ntoa(in));

	u_char *p = g_ucLocalMac;
	printf("     MAC Address : %02X-%02X-%02X-%02X-%02X-%02X \n", p[0], p[1], p[2], p[3], p[4], p[5]);

	printf(" \n \n ");

	return TRUE;
}

int main()
{
	// 获取全局数据
	GetGlobalData();

	getchar();
	return 0;
}

 Get Local Network Information

Then the Windows Sockets I / O model

It introduced several such books

The first mode is a socket

Is blocking and non-blocking and non-blocking I did not look easy

The second is to select the model

Select the model of this thing is to own a good package of a set of sockets and then we can determine the state by an iterative function they provide

Such benefits such as a single-threaded process really simple to debug it is also simple but limited the number of sockets

And I feel if the socket, then it makes the program more efficiency becomes slow. .

The third model is the model WSAAsyncSelect beginning of fact, I imagined

Really did not expect to have this model is to use the Windows messaging mechanism to transfer socket idea but it is still a little amazing

But the nagging question is this message mechanism if the number of words on a cool

And this is the case only with the gui page ~~~

The fourth model is the model WSAEventSelect

This one

Guess you like

Origin blog.csdn.net/qq_41071646/article/details/90414289