Windows Socket API Usage Experience

Source: Internet
Author: User
Tags socket

This article is I in the ms-windows, the Hp-unix network programming practice process summarizes some experience, only for everybody reference. The socket function mentioned in this article, if not specifically stated, refers to the Windows Socket API.

First, WSAStartup function

int WSAStartup(
    WORD wVersionRequested, 
    LPWSADATA lpWSAData 
   );

A program using the socket must call the WSAStartup function before using the socket. The first parameter of the function indicates the version of the socket that the program requests to use, where the high byte indicates the secondary version, the low byte indicates the major version, and the operating system uses the second parameter to return the version information of the requested socket. When an application calls the WSAStartup function, the operating system searches for the corresponding socket library based on the requested version of the socket, and then binds the found socket library to the application. The application can then invoke the other socket functions in the requested socket library. The function returns 0 after successful execution.

Example: If a program is to use the 2.1 version of the socket, then the program code is as follows

wversionrequested = Makeword (2, 1);

Err = WSAStartup (wversionrequested, &wsadata);

Second, WSACleanup function

int wsacleanup (void);

After the application completes the use of the requested socket library, it calls the WSACleanup function to unbind the socket library and free the system resources occupied by the socket library.

Third, the socket function

SOCKET socket(
    int af,    
    int type,   
    int protocol 
   );

The application calls the socket function to create a socket that can communicate on the network. The first parameter specifies the protocol family of the communication protocol used by the application, and for the TCP/IP protocol family, the parameter is set to Pf_inet; The second parameter specifies the type of socket to create, the stream socket type is sock_stream, and the datagram socket type is SOCK_DGRAM The third parameter specifies the communication protocol used by the application. The function returns the descriptor of the newly created socket if the call succeeds, and returns Invalid_socket if it fails. A socket descriptor is a value of an integer type. There is a Socket descriptor table in the process space of each process, which holds the corresponding relationship between the socket descriptor and the socket data structure. There is a field in the table that holds the descriptor for the newly created socket, and the other field holds the address of the socket data structure, so that its corresponding socket data structure can be found based on the socket descriptor. Each process has a socket descriptor in its own process space, but the socket data structure is in the kernel buffer of the operating system. The following is an example of creating a stream socket:

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.