VC recipes detailed - network articles

xiaoxiao2021-03-06  43

Designing a basic web server has the following steps:

1, initialize Windows Socket

2. Create a monitored socket

3. Set the server address information and bind the listener port to this address.

4, start listening

5, accept the client connection

6, and client communication

7, end the service and clean up Windows Socket and related data, or return to step 4

Windows Socket is inheriting from UNIX Socket, and the latest version is 2.2. Perform Windows Network Programming, you need to include Winsock2.h or mswsock.h in your program, and you need to add introduction library WS2_32. Lib or wsock32.lib. After you are ready, you can start to build your first web program.

Socket program has two types of blocking and non-blocking. The following models are divided into the following models on the I / O level of the operating system: SELECT, WSAASYNCSELECT, WSAEVENTSELECT, IO overlap model, complete port, etc. The blocking mode contains the basic concepts of network programming, compares beginners, but if you want to develop large-scale applications, you must design the system into non-blocking mode (it is difficult to imagine a large server with blocking mode Communication). When selecting an I / O model, beginners should choose the WSaasyncselect model because it is relatively simple and has certain practicality. However, almost everyone recognizes that it is the best choice to complete the port model to complete the network program to respond to thousands of users. Does it think that what I said? Oh, don't panic! Let's take a look at the knowledge you need to do a simple web program! 1.WSADATA data type typedef struct WSAData {WORD wVersion; WORD wHighVersion; char szDescription [WSADESCRIPTION_LEN 1]; char szSystemStatus [WSASYS_STATUS_LEN 1]; unsigned short iMaxSockets; unsigned short iMaxUdpDg; char FAR * lpVendorInfo;} WSADATA, FAR * LPWSADATA The following we will analyze the parameters of the parameters: WVersion: The version of the dynamic link library WS2_32.dll will use; WHighversion: The highest version of the Window socket supported by the dynamic link; SZDescription: Window socket implementation description. SzsystemStatus: The relevant status of the system and configuration information. The latter three is just to maintain certain compatibility with Window Socket. 2. Socket Data Type Each MFC Socket object contains a pointer, which points to the Socket object of Windows. The data type of this pointer is Socket. 3.SOCKADDR_IN data type struct sockaddr_in {short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero [8];}; struct in_addr {union {struct {u_char s_b1, s_b2, s_b3, s_b4;} S_un_b; struct {u_short s_w1, S_W2;} s_un_w; u_long s_addr;} s_un; 4.wsastartup function int wsastartup (Word WVersionRequested, Wondwo Socket, which can be used by the caller); Note: In an application or dynamic link library, The function is the first WINDOW Socket function called.

By using this function, the program or dynamic link library can determine which version of the Window Socket is used and the specific implementation details of the version are obtained. Only after the function is successfully called, we can use other Windwo Socket functions. 5. Htons Function U_SHORT HTONS (U_SHORT HOSTSHORT) Transforms the U_SHORT type variable on the host to the byte order of the TCP / IP network 6.htons function u_long htonl (u_long hostlong); transform the U_LONG type on the host to TCP / IP network Byte sequence 7.. Socket function socket socket (INT AF, address type INT TYPE, new socket type. There are two types in Socket1.1: socket_stream and socket_dgram. There are many new types in Socket2 and It is not necessary to determine here. Applications can discover the properties of the available transport protocol through the WSAenumProtocls function. INT protocol determines what protocol will use, the protocol corresponds to the specified address class. 8..bind function int bind (this function For the binding listening port Socket S, a Socket Const SockAddr Far * name, the SocketAddr structure of the Socket Const SockAddr Far * name, the length of the name of the INT Namelen;

转载请注明原文地址:https://www.9cbs.com/read-80684.html

New Post(0)