3.2 Protocol features
3.2.5 easily close
The Windows Sockets API provides the shutdown () and wsasenddisconnect () functions to disable the connection.
Closesocket () is used to disable sockets, And the shutdown () function is also implicitly executed.
The process of closing the client with ease:
- The client uses se_send as the parameter to call Shutdown (), meaning that the client no longer sends data (but the client can still accept data ).
- The server received fd_close and learned that the client was being closed.
- The server sends data.
- The server calls Shutdown () with se_send as the parameter, and then calls closesocket () to close the socket.
- The client receives fd_read, receives data from the server, and then receives fd_close.
- The client calls closesocket () to close the socket.
3.3 IP Address
3.3.1 IP Address
// A means address
//F means family
# DefineAf_inet 2
In Windows Sockets, The sockaddr_in structure is used to specify the IP address and port number.
Struct Sockaddr_in {
Short Sin_family; // Address Family
U_short sin_port; // Service port number
Struct In_addr sin_addr; // In_add type IP Address
Char Sin_zero [ 8 ]; // Fill the size of the structure to make it the same as the size of the sockaddr Structure
};
Sin_family must be af_inet to inform the Windows Sockets ApplicationProgramUse the IP address family.
When developing a Windows Sockets application, select 1024 ~ 49151.
Sin_addr is used to save an IP address as a 4-byte value.
3.3.2 byte sequence Problems
Htonl () and htons () are used to convert the host bytes to the network bytes.
Htonl = host to net long
Htons = host to net short
U_long htonl (
U_long hostlong
);
U_short htons (
U_short hostshort
);
Likewise, ntohl () and ntohs ().
U_long ntohl (
U_long netlong
);
U_short ntohs (
U_short netshort
);