Windows Sockets, in order to support the Windows message-driven mechanism, enables application developers to easily handle network traffic, using a message-based asynchronous access strategy for network events;
1, WSAAsyncSelect
This function requests a Windows message-based network event notification for the specified socket (socket) and automatically sets the socket to non-blocking mode;
int WSAAsyncSelect (
SOCKET S,
HWND hwnd,
unsigned int wmsg,
Long lEvent
);
S: The socket that identifies the request for Network event notification;
HWnd: A handle that identifies a window that receives a message when a network event occurs;
WMSG: Specifies the message that the window will receive when a network event occurs;
LEvent: Specifies the network events that the application is interested in, including:
Fd_read--about whether a readable notification is available to read the data
Fd_write--about whether writable notifications are available to send data
Fd_oob--whether out-of-band (OOB) data arrival notification
Fd_accept--Notifications related to entering the connection
Fd_connect--Notification that the connection operation has been completed
Fd_close--Notifications related to socket closure
Fd_qos--Notification that the socket "quality of service" has changed
FD_GROUP_QOS-Notification of changes to the "quality of service" of the socket group
Fd_routing_interface_change-Notification of changes in the routing interface in the specified direction
Fd_address_list_change-Notification of changes to the local address list for the protocol family of the socket
2, Wsaenumprotocols
This function can obtain information about the network protocol installed in the system, the function cannot be called repeatedly, and the buffer must be large enough to hold all the elements;
int Wsaenumprotocols (
Lpint Lpiprotocols,
Lpwsaprotocol_info Lpprotocolbuffer,
Ilpdword lpdwBufferLength
);
Lpiprotocols: A null-terminated protocol identifier group, or NULL, which returns information about all available protocols, otherwise only the protocol information listed in the array is returned;
Lpprotocolbuffer: Used as a return value, a buffer filled with wsaprotocol_info structure, wsaprotocol_info structure is used to store or obtain the complete information of a specified protocol;
lpdwBufferLength: Specifies the length of the parameter Lpprotocolbuffer buffer to be passed to the function at input, and the length of the minimum buffer to be passed to the function for all request information at output;
3, WSAStartup
The function initializes the socket library used by the process;
int WSAStartup (
WORD wversionrequested,
Lpwsadata Lpwsadata
);
Wversionrequested: The version of the socket library that can be used, the secondary version of the high-level byte, and the low-level byte as the main version;
Lpwsadata: Returns information about the available library, which is a pointer to a variable of the WSADATA data structure type;
4, WSACleanup
The function terminates the application of the socket library;
int WSACleanup ();
5, WSASocket
The function creates the socket;
SOCKET WSASocket (
int AF,
int type,
int protocol,
Lpwsaprotocol_info Lpprotocolinfo,
GROUP G,
DWORD DwFlags
);
AF: Specify address family, af_unix/af_local/af_file--Local communication, af_inet--network communication IPv4 (main), af_inet6--network communication IPv6 (prefix AF replaces PF effect);
Type: Specify socket type, sock_stream--stream socket, sock_dgram--datagram socket;
Protocol: recommended for 0;
Lpprotocolinfo: A pointer to the Wsaprotocol_info struct that defines the properties of the socket that is created, which can be null;
G: Reserved parameters;
DwFlags: Specifies the description of the socket property, or, if set to wsa_flag_overlapped, creates an overlapping socket;
6. WSARecv---Extended version of the RECV function
int WSARecv (
SOCKET S,
Lpwsabuf Lpbuffers,
DWORD Dwbuffercount,
Lpdword LPNUMBEROFBYTESRECVD,
Lpdword Lpflags,
Lpwsaoverlapped lpoverlapped,
Lpwsaoverlapped_completion_routine lpCompletionRoutine
);
7. WSASend---Extended version of the Send function
int WSASend (
SOCKET S,
Lpwsabuf Lpbuffers,
DWORD Dwbuffercount,
Lpdword lpNumberOfBytesSent,
DWORD DwFlags,
Lpwsaoverlapped lpoverlapped,
Lpwsaoverlapped_completion_routine lpCompletionRoutine
);
8. WSARecvFrom---Extended version of the Recvfrom function
The function receives the data of the datagram type and holds the address of the sender of the data, through the 2nd parameter of the function, we can define multiple WSABUF structure variables to receive data when the function is called, and specify the number of WSABUF structure by the 3rd parameter of the function.
int WSARecvFrom (
SOCKET S,
Lpwsabuf Lpbuffers,
DWORD Dwbuffercount,
Lpdword LPNUMBEROFBYTESRECVD,
Lpdword Lpflags,
struct sockaddr FAR * lpfrom,
Lpint Lpfromlen,
Lpwsaoverlapped lpoverlapped,
Lpwsaoverlapped_completion_routine lpCompletionRoutine
);
S: Identify sockets;
Lpbuffers: A pointer to an array of WSABUF structures defined as:
typedef struct __wsabuf{
U_long Len; The length of the buffer
char far *buf; Pointer to buffer
}wsabuf, FAR * LPWSABUF;
The number of WSABUF structures in the dwbuffercount:lpbuffers array;
LPNUMBEROFBYTESRECVD: Pointer to the number of bytes received by this call;
Lpflags: values include:
Msg_peek--browses the incoming data, which is copied to the buffer, but not removed from the input queue; only valid for non-overlapping sockets;
Msg_oob-handling out-of-band (OOB) data
Msg_partial--for message-oriented sockets only; As an output parameter, the data is part of the message sent by the sender, and the remainder of the message is transmitted in subsequent receive operations, indicating that this is the end of the message sent by the sender if a subsequent receive operation does not have this flag , as an input parameter, indicates that the receive operation is complete, even if only part of the data for a message has been received by the service provider;
Lpfrom: An optional pointer to the buffer that holds the source address when the overlap operation is complete;
Lpfromlen: A pointer to the buffer size specified by Lpfrom, which must be used only if the Lpfrom parameter is specified;
lpoverlapped: A pointer to the wsaoverlapped struct, and the non-overlapping socket ignores this parameter;
lpCompletionRoutine: A pointer to the completion routine called when the receive operation is completed, which is actually a callback function as shown below, and the non-overlapping socket ignores this parameter;
void CALLBACK Completionroutine (
In DWORD dwerror,
In DWORD cbtransferred,
In lpwsaoverlapped lpoverlapped,
In DWORD dwFlags
);
9. WSASendTo---Extended version of the SendTo function
int WSASendTo (
SOCKET S,
Lpwsabuf Lpbuffers,
DWORD Dwbuffercount,
Lpdword lpNumberOfBytesSent,
DWORD DwFlags,
const struct SOCKADDR FAR * lpto,
int Itolen,
Lpwsaoverlapped lpoverlapped,
Lpwsaoverlapped_completion_routine lpCompletionRoutine
);
S: Identifies a socket
Lpbuffers: A pointer to the WSABUF structure;
The number of WSABUF structures in the dwbuffercount:lpbuffers array;
lpNumberOfBytesSent: A pointer to the number of bytes sent by this call;
DwFlags: Indicates the flag bit that affects the operation behavior, set to 0;
Lpto: An optional pointer to the address of the target socket;
The length of the address in the Itolen:lpto;
lpoverlapped: A pointer to the wsaoverlapped struct, and the non-overlapping socket ignores the parameter;
lpCompletionRoutine: A pointer to the completion routine that is called when the receive operation completes, and the non-overlapping socket ignores the parameter;
API starting with the WSA, combined with Windows platform features