Windows Socket API usage experience

Source: Internet
Author: User

 

 

I,WSAStartup Function

Int WSAStartup (

WORD wVersionRequested,

LPWSADATA lpWSAData

);

The program using the Socket must call the WSAStartup function before using the Socket. The first parameter of this function indicates the Socket version requested by the program. The high byte indicates the secondary version and the low byte indicates the primary version; the operating system uses the second parameter to return the request's Socket version. When an application calls the WSAStartup function, the operating system searches for the corresponding Socket Library Based on the requested Socket version, and then binds the Socket Library to the application. In the future, the application can call other Socket functions in the requested Socket library. After the function is successfully executed, 0 is returned.

For example, if a program needs to useFor Socket 2.1, the program code is as follows:

 WVersionRequested = MAKEWORD (2, 1 );

Err = WSAStartup (wVersionRequested, & wsaData );

 

II,WSACleanup Function

Int WSACleanup (void );

After the application completes the use of the requested Socket library, it needs to call the WSACleanup function to unbind from the Socket library and release the system resources occupied by the Socket library.

 

III,Socket Functions

SOCKET socket (

Int af,

Int type,

Int protocol

);

The application calls the socket function to create a socket capable of network communication. The first parameter specifies the protocol family used by the application. For the TCP/IP protocol family, this parameter is set to PF_INET. The second parameter specifies the socket type to be created, 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. This function returns the descriptor of the newly created socket if the call is successful, and INVALID_SOCKET if the call fails. The socket descriptor is an integer value. Each process has a socket Descriptor Table in the process space, which stores the correspondence between the socket Descriptor and the socket data structure. In this table, one field stores the descriptor of the newly created socket, and the other field stores the address of the socket data structure. Therefore, the corresponding socket data structure can be found based on the socket descriptor. Each process has a socket Descriptor Table 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:

Struct protoent * ppe;

Ppe = getprotobyname ("tcp ");

SOCKET ListenSocket = socket (PF_INET, SOCK_STREAM, ppe-> p_proto );

 

IV,Closesocket Function

Int closesocket (

SOCKET s

);

The closesocket function is used to close a socket whose descriptor is s. Each process has a socket Descriptor Table. Each socket descriptor in the table corresponds to a socket Data Structure located in the operating system buffer, therefore, several socket descriptors may point to the same socket data structure. There is a field in the socket data structure that stores the number of times this structure is referenced, that is, how many socket descriptors point to this structure. When the closesocket function is called, the operating system first checks the value of this field in the socket data structure. If it is 1, it indicates that only one socket descriptor points to it, therefore, the operating system first clears the entry corresponding to s in the socket Descriptor Table and releases the socket data structure corresponding to s. If this field is greater than 1, then, the operating system only clears the corresponding table items of s in the socket Descriptor Table, and reduces the number of references to the socket data structure of s by 1.

If the closesocket function is successfully executed, 0 is returned; otherwise, SOCKET_ERROR is returned.

 

V,Send Function

Int send (

SOCKET s,

Const char FAR * buf,

Int len,

Int flags

);

Both the client and server applications use the send function to send data to the other end of the TCP connection. The client program generally uses the send function to send requests to the server, while the server uses the send function to send responses to the client program. The first parameter of this function specifies the sender socket descriptor. The second parameter specifies a buffer zone for storing data to be sent by the application. The third parameter specifies the number of bytes of data to be sent; the fourth parameter is usually set to 0. Only the execution process of the send function for Synchronous Socket is described here.

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.