Common socket APIs in Windows

Source: Internet
Author: User

1. Winsock DLL

1. initialize Winsock DLL

Int wsastartup (

Word wversionrequested, // The maximum version supported by Windows Socket that the caller can use. Specifies the secondary version at the High Level and the primary version number at the low level.

Lpwsadata // pointer to wsadata, used to receive Implementation Details of Windows Socket

);

// This function initializes the use of ws2_32.dll in a process.

2. Release Winsock DLL Resources

Int wsacleanup (void );

 

Ii. Socket

1. Create a socket

Socket socket (

Int AF, // address family description

Int type, // description of the new socket type

Int protocol // specifies the SOCKET protocol, which is specific to the address family. 0 indicates the default protocol.

);

2. Close the socket with ease

Int Shutdown (

Socket S, // socket Descriptor

Int how // a flag indicating which operation type is not allowed

);

3. Disable socket

Int closesocket (

Socket s

);

 

Iii. Server Side

1. Bind

Int BIND (

Socket S, // a descriptor with no socket bound;

Const struct sockadd * Name, // allocate the socket address from the sockaddr Structure

The length of the int namelen // name in bytes.

);

2. Listening

Int listen (

Socket S, // indicates a socket bound but not connected

Int backlog // Maximum length of the queue to be connected

);

The listen function places the socket in the listening status and waits for the connection to enter.

3. Receive

Socket accept (

Socket S, // a socket descriptor that uses the listen function to place the listener status. The socket returned by the accept function is actually a connection.

Struct sockaddr * ADDR, // optional pointer, pointing to the buffer used to receive connected entities

Int * addrlen // an optional pointer pointing to an integer containing the length of ADDR

);

// The accept function allows a socket to accept client connections.

 

 

Iv. Client

1. Create a connection

Int connect (

Socket S, // calibrate an unbound socket Descriptor

Const struct sockaddr * Name, // name of the socket with which the connection should be established

Int namelen // Name Length

);

// Connect function creates a link to the specified socket

 

5. Data Transmission

1. Send data

Int send (

Socket S, // identifies a descriptor that has established a connection socket

Const char * Buf, // buffer that contains the data to be transmitted

Int Len, // The length of data in Buf

Int flags // The indicator that specifies the invocation method.

);

// The send function sends data on a socket with established connections.

2. send data

Int sendto (

Socket S, // indicates a mark that may have been connected

Const char * Buf,

Int Len,

Int flags,

Const struct sockaddr * To, // an optional pointer pointing to the address of the target socket

Int tolen

);

3. receive data

Int Recv (

Socket S, // identifies a descriptor that has established a connection socket

Char * Buf,

Int Len,

Int flags

);

// The resv function receives data from a connected or bound socket.

4. receive data from

Int recvfrom (

Socket s,

Char * Buf, // buffer for receiving incoming data

Int Len,

Int flags,

Struct sockaddr * From, // an optional pointer pointing to a buffer that saves the source address when returning

Int * fromlen

);

 

Vi. socket address information

1. Obtain the socket address of the other party

Int getpeername (

Socket s,

Struct sockaddr * Name,

Int * namelen

);

// The getpeername function gets the name of the computer connected to a socket.

2. obtain local socket address information

Int getsockname (

Socket S, // identifies the descriptor of a connected socket
Struct sockaddr far * Name, // used to obtain the local socket socketaddr Structure

Int far * namlen // point to the name structure size, in bytes

);

// Getsockname function to obtain the local socket name

 

VII. Miscellaneous

1. Get the latest error code

Int wsagetlasterror (void );

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.