First, it's all a routine (-)
header file #include <WinSock.h>
#include <Winsock2.h> with this, this is the upgraded version.
Individual function explanations
1,WSAStartup:
Initializes the socket environment, which must be the first Windows Sockets function called by the application or DLL. It allows an application or DLL to indicate the version number of the Windows Sockets API and to obtain a specific windows Sockets implementation details. An application or DLL can call further Windows Sockets API functions only after a successful WSAStartup () call.
int WSAStartup (WORD wversionrequested, lpwsadata lpwsadata);
Actual Use Cases
// begin initializes the network environment int err = WSAStartup (Makeword (22), &wsadata); if 0 { printf ("WSAStartup failed with error:%d\n", err); return -1;} // End
2,WSACleanup:
Cleans up the socket environment, as opposed to the above WSAStartup, which is used to clean the socket environment after the program is not called on any Windows Sockets function.
int wsacleanup (void);
3,socket socket (int AF, int type, int protocol); Establish socket Parameters AF is used to specify the network address type, which is generally taken af_inet, which indicates that the socket is in the Internet domain for communication. The parameter type is used to know the type of socket, and if sock_stream means that the socket created is a stream socket, and SOCK_DGRAM creates a digital datagram socket. The parameter protocol is used to specify the network protocol, typically taking 0, which means that the TCP/IP protocol is default. If the socket is created successfully, the function returns the socket handle that was created, otherwise a invalid_socket error is generated. actual Use Cases
// begin socket A socket Htcpsocket = socket (af_inet, sock_stream, ipproto_tcp); if (Invalid_socket = = htcpsocket) {mjs_log_error ("SOCKET failed with ERROR: \ n") ; WSACleanup (); return -1 ;} // End
4,int Listen (SOCKET s, int backlog);
Place the socket in listening mode and prepare to accept the connection request. Where the parameter s is the server-side socket, specifying the maximum queue length that is waiting for the connection. If no error occurs, the Listen function returns 0, the failure returns a socket_error error, and the application can get the corresponding error code through WSAGETLASTERROR ().
5,socket accept (socket s, struct sockaddr* addr, int* addrlen); parameter s Ibid, addr is the address of a valid SOCKADDR_IN structure, and Addrlen is the length of the sockaddr_in result. When the Accept function returns, the addr parameter variable contains the IP address information of the client that made the connection request, while the Addrlen parameter indicates the length of the structure and returns a new socket descriptor that corresponds to the client connection that has been accepted.
6, int bind (SOCKET s, const struct sockaddr* name, int namlen); bind to local, the IP specified in name should be the IP that is currently running the program machine.
7,int Connect (SOCKET s, const struct SOCKADDR far* name, int namelen); Connect to Serveractual Use Cases
intserveport=20000;Charserveip[ +]="192.168.1.200"; sockaddr_in addr;addr.sin_family=Af_inet;addr.sin_port=htons (serveport); addr.sin_addr. S_un. S_addr=inet_addr (SERVEIP);//connecting to a serverif(Connect (Htcpsocket, (sockaddr*) &addr,sizeof(SOCKADDR)) == -1) {closesocket (htcpsocket); printf ("Connect Tradesys failed with error%d,%s,%d: \ n", Serveport,serveip,getlasterror ()); WSACleanup (); return-1;}Else{printf ("connect Tradesys sucessed,%d,%s: \ n", SERVEPORT,SERVEIP);}
If no error occurs, connect () returns 0. Otherwise, the SOCKET_ERROR error is returned, and the application can get the corresponding error code via WSAGETLASTERROR (). For a blocking socket interface, the application calls Wsagetlsaterror () if the return value is Socket_error. If it indicates that the error code is WSAEWOULDBLOCK, your application can:
1. Use Select () to determine whether the connection request is complete by checking whether the socket interface is writable. Or,2. If your application uses message-based wsaasynselect () to indicate interest in connection events, you will receive a FD_CONNECT message when the connection operation is complete. 8, int Send (SOCKET s, const char* buf, int len, int flags);
return -1/socket_error on Failure
SOCKFD: A send-side socket descriptor (not a listener descriptor) for a connection has been established
BUF: Applying a cache to send data
Len: The actual length of data to be sent
Flags: The general setting is 0. flags have the following values: 0, Msg_dontroute or Msg_oob, or the bitwise OR operation of these flags.
9,int recv (SOCKET s, char* buf, int len, int flags); S is the socket that prepares to receive data, BUF is the character buffer that is about to receive the data, and Len is the number of bytes prepared to accept or the length of the BUF buffer. Flags are generally specified as 0. The parameter can be 0, msg_peek, or Msg_oob or the bitwise OR operation of these flags.
Represents copying data from the receive buffer. On success, returns the number of bytes copied, and the failure returns-1. In blocking mode, the Recv/recvfrom will be blocked into the buffer with at least one byte (TCP)/At least one full UDP datagram to return, no data in hibernation. If it is not blocked, it returns immediately, with the data returning the copied data size, otherwise error 1 is returned, and the error code is ewouldblock.
10,int shutdown (SOCKET s, int how); where the How parameter is used to describe which operations are forbidden, and it has the desirable values: sd_receive, Sd_send, or Sd_both. If it is sd_receive, it means that the receive function is not allowed, and if you choose Sd_send, it means that the Send function is not allowed again, and if it is sd_both, it means canceling the send and receive operations at both ends of the connection. If no error occurs, shutdown () returns 0, otherwise the SOCKET_ERROR error is returned. 11, int closesocket (SOCKET s); S is the socket descriptor to be closed, and the call to execute with the socket fails. gethostname Int (char *name, size_t len): This function, when called, will save the hostname in name. And Len is the size of name. The function returns 0 for success, otherwise fails. struct hostent *gethostbyname (const char *name);
Hostent structure:
struct hostent { char *h_name; // *h_name represents the canonical name of the host char **h_aliases; // h_aliases represents the alias of the host int H_addrtype; // address type af_inet, or Af_inet6 int h_length; // ip address in bytes char **h_addr_list; // ip address List };
An int **h_addr_lisst indicates that the IP address of the host is stored in the network byte order.
14,const char *inet_ntop (int af, const void *SRC, char *dst, socklen_t cnt);
This function, which is the network address structure SRC, which is type AF, is converted into a string form of a host order, stored in a string of CNT length. The function, in fact, is to return a pointer to DST. If the function is called incorrectly, the return value is null.
15,struct hostent far *pascal far gethostbyaddr (const char FAR * addr, int len, int type); parameter addr: A pointer to the network byte-order address. Parameter len: The length of the address, which is 4 in the Pf_inet type address. Parameter type: Refers to address type af_inet, If_inet6
16,Inet_ntoa (char far* PASCAL far inet_ntoa (struct in_addr in); Convert the network address to "." The string format of the dots. This function converts an Internet address structure represented by an in parameter into a string of "." intervals, such as "a.b.c.d". Note that the string returned by Inet_ntoa () is stored in the memory allocated by the Windows socket implementation. The application should not assume how the memory is allocated. The data is guaranteed to be valid before the next Windows socket interface call on the same thread.
parameter in: A structure that represents an Internet host address.
Return value: If no error occurs, Inet_ntoa () returns a character pointer. Otherwise, return to NVLL. The data should be copied before the next Windows socket interface call.
17,unsigned long inet_addr (const char far *cp); converts a point-formatted IP address to an unsigned type store. 18,u_short PASCAL far htons (u_short hostshort); The function is to convert the unsigned short shape number of the host into a network byte order. 19, u_short PASCAL far ntohs (u_short netshort); 20,u_long PASCAL far htonl (U_long hostlong); This function converts a 32-digit number from host byte order to network byte order. actual Use Cases
//Converts the host's unsigned Long value to the network byte order (32-bit), using the function htonl ()//parameter Hostlong A number that identifies the host byte order, and the function returns a network byte order number#include <winsock.h>#include<stdio.h>#pragmaComment (lib, "Ws2_32.lib")voidMain () {U_long a=0x12345678; U_long b=htonl (a);p rintf ("%u/n", a);p rintf ("%x/n", a);p rintf ("%u/n", B);p rintf ("%x/n", b);}
21,U_long PASCAL far Ntohl (U_long netlong);
Converts a number of unsigned long shapes from network byte order to host byte order.
Parameter Netlong: A 32-digit number expressed in network byte order.
return value: Ntohl () returns a number expressed in host byte order.
//converts 32-bit network bytes to host bytes, using function Ntohl ()//defined as followsu_long Ntohl (u_long netlong); #include<winsock2. H>#include<stdio.h>#pragmaComment (lib, "Ws2_32.lib")voidMain () {U_long a=0x12345678; U_long b=Ntohl (a);p rintf ("%u/n", a);p rintf ("%x/n", a);p rintf ("%u/n", B);p rintf ("%x/n", b);}
Error code:wsanotinitialised: You should first successfully call WSAStartup () before using this API. the Wsaenetdown:windows socket interface realizes the network sub-system failure. Wsaeinval: The time-out value is illegal. Wsaeintr: Cancels a (blocked) call with a WSACancelBlockingCall (). wsaeinprogress: A blocked Windows Socket interface call is running. Wsaenotsock: An element in the description Word collection that contains a non-nested interface. Wsaeaddrinuse: The address referred to is already in use. Wsaeaddrnotavail: The indicated address could not be found on the local machine. Wsaenotsupport: Addresses in the indicated family cannot be used with this set of interfaces. WSAECONNREFUSED: The connection attempt was forcibly denied. Wsaedestaddreq: The destination address is required. The wsaefault:namelen parameter is incorrect. Wsaeinval: The socket interface is not ready with an address bundle. Wsaeisconn: The socket interface is already connected. wsaemfile: No extra file description word. Wsaenetunreach: The network cannot be accessed from this host at this moment. wsaenobufs: No buffer available. The socket interface is not connected. Wsaenotsock: The description word is not a set of interfaces. Wsaetimeout: Timeout time is up. Wsaewouldblock: The socket interface is set to non-blocking mode and the connection cannot be established immediately. A select () call is available to write to the socket, since select () is connected.
WinSocket < studied the results of the day >