Windows Sockets Study Notes [Chapter 1 Windows Sockets Basics] (part I)

Source: Internet
Author: User

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:

    1. 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 ).
    2. The server received fd_close and learned that the client was being closed.
    3. The server sends data.
    4. The server calls Shutdown () with se_send as the parameter, and then calls closesocket () to close the socket.
    5. The client receives fd_read, receives data from the server, and then receives fd_close.
    6. 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
);

 

Related Article

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.