Winsock functions (1)

xiaoxiao2021-03-06  82

The Windows Sockets API interface provided by this system is a letter defined in Wins 2 (such as Appendix), which is announced on January 20, 1993; includes 30 Berkeley Software Distribution (BSD) The function and 16 functions that conform to Windows Message-Driven features. BSD function includes: Accept () bind () clossoSocket () connect () getpeername () getsockname () getsockt () htonl () htons () inet_addr () inet_ntoa () ioctlsocket () inet_NTOA () ioctlsocket () () recv () recvfrom () select () send () sendto () setsockopt () shutdown () socket () gethostname () gethostbyaddr () gethostbyname () getprotobyname () getprotobynumber () getservbyname () getservbyport () Microsoft Windows-specific Extensions function comprising: WSAAsyncGetHostByAddr () WSAAsyncGetHostByName () WSAAsyncGetProtoByName () WSAAsyncGetProtoByNumber () WSAAsyncGetServByName () WSAAsyncGetServByPort () WSAAsyncSelect () WSACancelAsyncRequest () WSACancelBlockingCall () WSACleanup () WSAGetLastError () WSAIsBlocking () WSASetBlockingHook () WSASetLastError () WSAStartup () WSAUnhookBlockingHook () These API interfaces apply to Internet Protocol Suite (IPS, usually called TCP / IP), support Stream (TCP) and DataGram (UDP) Socket. Stream (TCP) Socket provides "two-way", "reliable", "order", and "non-revitalization" data transmission. DataGram (UDP) Socket provides "two-way" communication, but there is no guarantee that "reliable", "sequence", "no heavy remover", etc. The information may also be omission during transmission. [Blocking and Non-Blocking Mode] Blocking mode: When the user calls this mode, you will enter the inside of this function until the condition or data is completely matched to the call point. Non-Blocking mode: When the user calls this mode, enter the inside of this function, and make appropriate reply according to the conditions or information at the time, and will not stay in the function or data is fully compliant. Typ the response. Users must pay attention to that the block definition of Winsock is not too similar to general Unix.

Winsock definitions Allow applications while calling the blocking function, still able to handle its message (Messages), including keyboard and mouse events; however, in addition to canceling the original Locking action in addition to WSACANCELBLOCKINGCALL () Use the wsaisblocking () to check if there is a blocking action is still in progress, and "No" The other Socket is called before the Blocking in the original call, otherwise, will fail and generate WSAINPROGRESS errors. Users call wsacancelBlockingCall () written down Blocking Action If it is not accept () or select (), then the only calling Socket () is only closesocket (), because the Blocking action that is canceled will make it become INDETERMINATE state. [Async (Non-synchronous) mode] When the user calls this mode, it is not immediately requested; but when the required action is completed, the system will notify the caller again through another manner. The benefit is that the user does not need to wait until the reply will only do other actions or requirements. Winsock defined async mode is to inform the user in "PostMessage" to do its request has been completed; so when you call such a form, you must inform Windows Sockets DLL Some information, including the Windows Handle and the message number that accept the message. [BSD Socket] [BSD Socket Primary Library] (1) Accept (): Accepting a Socket connection requirement to complete the connection of the Stream Socket. Format: Socket Pascal Far Accept (Scoket S, Struct SockAddr Far * Addr, Int Far * AddRlen); Parameters: S socket identifies the length of the end address of the connection of the ADDR storage connection, the length of the end of the address Addr: Success - New Socket identification code failed - Invalid_Socket (Call Wsagetlasterror () Reason: The application of the Server end call this one-in-one SOCKET connection action; if the SERVER SOCKET is Blocking mode, And no one requires the connection action, then this one of the block functions will have an error immediately. The ACCEPT () letter is a new Socket, which is no longer used to accept other connection requirements; but the original Socket can still accept other people's connection requirements. (2) Bind (): Specifies the local location of the socket (Address).

Format: Int Pascal Far Bind (Socket S, Const Struct Sockaddr Far * Name, INT Namelen); Paramese: s Socket's Additive Name Socket Address Value, Format is Struct SockAddr {U_SHORT SA_FAMILY; CHAR SA_DATA [14 ];}; Namelen Name Length Passage: Success - 0 Failed - Socket_ERROR (Call WsageTlasterror () Reason: This one is specified to specify the local location and port gives an unnamed Socket. If the user does not at the address or port value, then he can set the location to INADDR_Addr_any, and port is 0; then Windows Sockets will automatically set it appropriate and port (1024 to 5000) ), The user can make the value () to know the value set by the user can be used after this Socket true connection. (3) CloseSocket (): Close a socket. Format: INT Pascal Far CloseSocket (Socket S); parameter: s socket identification code pass-back value: success - 0 failed - socket_ERROR (Call wsagetlasterror () can know why: This one is used to close A socket. If the user originally sets SO_DONTLINGER to the SOCKET to be closed, it will reply immediately after calling this one, but this SOKCET has not been transferred after the delivery will be closed. If the user originally set this socket as SO_Linger, there are two cases: (a) Timeout is set to 0, this Socket is reset immediately, and the not received or the unrecovered information is lost. (B) If Timeout is not 0, the data will be delivered, or wait until TIMEOUT occurs. (4) Connect (): Requires to connect to a socket to the specified other party. Format: Int Pascal Far Connect (Socket S, Const Struct Sockaddr Far * Name, INT Namelen); Paramese: s Socket Identification Code Name This Socket wants to connect to the long-term transmission value of the Namelen Name: Success - 0 Failure - Socket_ERROR (Call WsageTlasterror () can beware of this) Description: This file is used to establish a connection to the other party. If the specified object address is 0, the error value will be returned. When the connection is completed, the user can use this socket to do the use of transfer or reception. (5) getPeername (): Get the other party address of the Socket connected successfully.

Format: Int Pascal Far getPeername (socket S, Struct Sockaddr Far * Name, INT FAR * NAMELEN); parameter: s socket identification code NAME This Socket connection of the other party site name Namelen Name length transmission value: success - 0 Fail-Socket_ERROR (Call WsageTlasterror () can beware of this) Description: This one can be used to obtain the site data of the connected successful socket. (6) getSockName (): Get the local location of the socket. Format: Int Pascal Far getSockName (Socket S, Struct Sockaddr Far * Name, Int Far * Namelen); Paramese: s S socket ID Name This Socket LOCAL address Namelen Name length pass-back value: success - 0 failed - SOCKET_ERROR (Call WsageTlasterror () can beware of this) Description: This file is used to obtain the local order information of the set location or connected SOCKET. If this Socket is set to INADDR_Addr_any, the correct location will be sent back after the actual establishment is successful. (7) getsockopt (): Requires information on a Socket current status setting. Format: Int Pascal Far getSockOpt (socket s, int level, int iptname, char far * optval, int far * optlen); parameter: S socket identification code level LEVEL option set the level Optname option name Optval option setting value Optlen option set value length passive: Success - 0 failed - socket_ERROR (Call WsageTlasterror () Reason: This file is used to get some state settings of the current socket.

Winsock provides Level only SOL_Socket and Ipproto_TCP Optname have the following choices: (see Winsock 29, 30) Value Type ---------------------- ------------------------- SO_ACCEPTCONN BOOL SO_BROADCAST BOOL * SO_DEBUG BOOL SO_DONTLINGER BOOL * SO_DONTROUTE BOOL * SO_ERROR int * SO_KEEPALIVE BOOL SO_LINGER struct linger FAR * SO_OOBINLINE BOOL * SO_RCVBUF INT SO_REUSEADDR BOOL * SO_SNDBUF INT SO_TYPE IN_SNDBUF INT SO_TYPE INT TCP_NODELAY BOOL (* Table Not This function option is provided) (8) HTONL (): Converts one of the 32-bit di U_LONGs by Host to NetWork's arrangement. Format: U_LONG PASCAL FAR HTONL (U_LONG HOSTLONG); Number: Hostlong A 32-bit Host Multiple Transfer Value: A 32-bit NetWork Arrangement Number Description: Because NetWork's arrangement is arranged in Host It may be different, so we need this one to do transformation. (9) htons (): Converts the value of 16-bit dimb U_Short by Host's arrangement to NETWORK. Format: U_SHORT PASCAL FAR HTONS (U_SHORT HOSTSHORT); Number: HostShort A 16-bit Host Host Multiple Transfer Value: A 16-bit NETWORK Arrangement Number Description: Because NetWork is arranged in a way with Host's arrangement It may be different, so we need this one to do transformation. (10) INET_ADDR (): Converts the location of the string format to a 32-bit Unsigned Long format. Format: unsigned long pascal far inet_addr (const char far * cp); parameter: CP a representative address "Dotted) string pass-forward value: success - a unsigned long failed for a representative Internet address - INADDR_NONE Description: This card translates a "point format" address to the applicable INTENET address. "Point format" strings can be one of the following four ways: (i) ABCD (II) ABC (III) AB (IV) A 11) inet_ntoa (): Convert a network location into "point format" String.

Format: CHAR FAR * PASCAL FAR inet_ntoa (STRUCT IN_ADDR IN); parameter: in one represents the structure of the Internet Address: Success - a "Dotted) string failed - NULL description : This card transfers an INTERNET address to the "ABCD" string format. (12) IOCTLSocket (): Controls the mode of the socket. Format: Int Pascal Far IOCTLSOCKET (Socket S, Long CMD, U_LONG FAR * ARGP); Parameters: s Socket Identification Code CMD Directive Name Argp Number Review Value: Success - 0 Failed - Socket_ERROR (Call WsageTlasterror () Available in the reason) Description: This file is used to get or set the operational parameters of the socket. The instructions provided are: (See Winsock Item 1.1 35, 36) Fionbio - Switch Non-Blocking Mode FionRead - Siocatmark - OOB data from Socket Siocatmark - OOB has been read ( * This feature is not provided) (13) Listen (): Sets Socket to the listening state, ready to be connected. Format: Int Pascal Far Listen (Socket S, INT Backlog); Parameters: s Socket Identification Code Backlog Unspeakable before the connection (not yet call accept () before the maximum number of passages of the connection requirements: success - 0 Failure - Socket_ERROR (Call WsageTlasterror () can be learned) Description: The user can use this file to set the socket to enter the listening state, and set up to how many of the portions that do not really complete the connection before the connection Claim. (At present, the maximum value is limited to 5, the minimum value is 1) (14) NTOHL (): The value of one 32-bit di U_LONG is converted into a Host arrangement in the NetWork. Format: U_LONG PASCAL FAR NTOHL (U_LONG NETLON); NETLONG A 32-bit NetWork Arrangement Number: A 32-bit Host Host Arrangement Number Description: Because NetWork's arrangement is arranged in Host It may be different, so we need this one to do transformation. (15) NTOHS (): Converts a 16-bit dimb U_Short by the NetWork arrangement into a Host arrangement.

Format: U_SHORT PASCAL FAR NTOHS (U_SHORT NETSHORT); Netshort A 16-bit NetWork Arrange Number: A 16-bit HOST Multiple Description: Because NetWork is arranged in a way with Host's arrangement It may be different, so we need this one to do transformation. (16) RECV (): Receive information from Socket. Format: Int Pascal Far Recv (Socket S, CHAR FAR * BUF, INT LEN, INT FLAGS); Parameters: s Socket Identification Code BUF Storage Received Texture Area Len BUF Length Flags This Function Call way passive: success - received data length (if the other Socket is closed, 0) failed - Socket_ERROR (Call wsagetlasterror () can beware of the reason: this franchel with DataGram Socket from the connection Or STREAM Socket Receive Information. For Stream Socket, we can receive currently valid (Available) data, but its quantity does not exceed the size of the LEN. If this Socket is set to SO_OOBINLINE, and the data of Out-of-band is not read, only the data of Out-of-band is taken out. For the DataGram Socket, only the first DataGram is taken out; if the DataGram is greater than the storage space provided by the user, only the information of the space size is removed, and the reduced information will be lost, and the wrong message will be replied. The value of Flags can be Msg_Peek, MSG_OOB (* does not provide this feature). (Refer to Winsock 1.1 version 41) (17) Recvfrom (): Read a DataGram and store the location where the data source is stored. Format: Int Pascal Far Recvfrom (Socket S, Char Far * BUF, INT LEN, INT FLAGS, STRUCT Socketaddr Far * from, INT FAR * "); parameter: Socket identification code BUF stores received information Region LEN BUF Length Flags This Function This Function The way from the source from the source, the source of the source, the size of the version of the source, the size of the source: success - the length of the received data (if the other Socket is closed, 0) failed - Socket_ERROR Call wsagetlasterror () can be learned) Description: This file is used to read the data and record the location where the data source is recorded. For Stream Socket, its role is the same as RECV (), parameter from and fromlen will not be used. (18) SELECT (): Check if one or more sockets are in a readable, writable or erroneous state.

Format: Int Pascal Far Select (int NFDS, FD_SET FAR * READFDS, FD_SET FAR * WRITEFDS, FD_SET FAR * EXCEPTFDS, FD_SET FAR * EXCEPTFDS, Const Struct TimeVal Far * Timeout); Parameters: NFDS This parameter is here in this parameter. ReadFDS is to be checked. Readable sockets WriteFDS To be checked if the Sockets ExceptFDs can be written to be checked if there is a wrong sockets (* No action) Timeout This function is waiting. When null, it means blocking, this file will wait until an event occurs. Passage: Success - the total number of Sockets (if timeout, 0) failed - Socket_ERROR (Call wsagetlasterror () can be learned: Users can use this function to check if Sockets can be available Read, or have spaces can be written, or there is an error. (19) Send (): Use the connection type Socket to transfer information. Format: Int Pascal Far Send (Socket S, Const Char Far * BUF, INT LEN, INT FLAGS); Parameters: S Socket Identification Code BUF Staves the Textual Send Region of the Material To Transfer Len BUF Length Flags This Voice Calling ways to pass the value: Successful - Delivery length failed - Socket_ERROR (Call WsageTlasterror () can be learned) Description: This one is suitable for connection-type DataGram or Stream socket to transfer information. For the DataGram Socket, if the size of the DataGram exceeds the limit, any information will not be sent and the error value will be sent back. If the storage space within the transfer system is not enough to save the information to be transmitted, send () will be held by Block unless the socket is set to the non-blocking mode. Users must also note that the send () written function is complete, and the information has been successfully sent to each other. The value of the FLAGS can be set to MSG_DONTROUTE (* No this feature is provided) and a combination of MSG_oob. (See Winsock 1.1 version 48 (20) Sendto (): Send the data to the specified destination.

Format: Int Pascal Far Sendto (Socket S, Const Char Far * BUF, INT LEN, INT FLAGS, CONST STRUCKADDR FAR * TO, INT TOLEN); parameter: s Socket identification code BUF stores the information to be transmitted Region LEN BUF Length Flags This function is called by To Data To serve the address toleN to the size of the TOLEN TO: Successful - Delivery length failed - Socket_ERROR (Call WsageTlasteError () can know the reason) : This one is suitable for DataGram or Stream Socket to transfer data to the specified address. For the DataGram Socket, if the size of the DataGram exceeds the limit, any information will not be sent and the error value will be sent back. For the Stream Socket, its effect is the same as send (); the parameters TO and TOLEN have no effect. If the storage space in the Transport system is not stored in these to be transferred, sendTo () will be held by Block unless the socket is set to the non-blocking mode. Users should also note that the Sendto () letter execution is completed, does not mean that the information has been successfully sent to each other. The value of the FLAGS can be set to MSG_DONTROUTE (* No this feature is provided) and a combination of MSG_oob. (See Winsock 1.1 version 51) (21) setsockopt (): Set the status of the socket. Format: Int Pascal Far Setsockopt (Socket S, INT Level, Int Optname, Const Char Far * OptVal, int Optlen); parameter: s Socket identifies the set value of the Level Optname option name OptVal option The length of the Optlen option set value: Success - 0 failed - Socket_ERROR (Call WsageTlasterror () can beware of the reason) Description: This one is used to set some options for Socket to change its actions. Options that can be changed are: (See Winsock No. 1.1 Edition 54) Value Type --------------------------------- -------------- SO_BROADCAST BOOL * SO_DEBUG BOOL SO_DONTLINGER BOOL * SO_DONTROUTE BOOL * SO_KEEPALIVE BOOL SO_LINGER struct linger FAR * SO_OOBINLINE BOOL * SO_RCVBUF int SO_REUSEADDR BOOL * SO_SNDBUF int TCP_NODELAY BOOL (* table temporarily provided This feature option) (22) shutdown (): Stop the function of the socket to receive / transmit.

Format: Int Pascal Far Shutdown (Socket S, INTHOW); parameter: s socket identification code how represents the Stop those actions, the flag passing value: success - 0 failed - Socket_ERROR (Call wsagetlasterror () can know Cause) Description: This file is used to stop the functions of subsequent reception or transfer of sockets. If the value is 0, the information is no longer received. If the value is 1, the information is not allowed. If the value is 2, it is no longer received and the data is not transferred. The shutdown () function does not turn down Socket, so the resource occupied by this socket must be released after calling CloseSocket (). (23) Socket (): Establish Socket. Format: Socket Pascal Far Socket (int AF, INT TYPE, INT Protocol); parameter: AF currently only provides PF_INET (AF_INET) TYPE SOCKET (SOCK_STREAM, SOCK_DGRAM) Protocol Communication Agreement (if the user does not specify, set 0) Passage: Success - Socket identification code failed - Invalid_Socket (Call WsageTlasterror () Reason: This one is used to create a socket and build the resources it uses for this socket. Socket types can be Stream Socket or DataGram Socket. (24) gethostbyaddr (): Utilize the location of the Host to get the information of this host. Format: Struct Hostent Far * Pascal Far GeTHostbyAddr (Const Char Far * Addr, INT LEN, INT TYPE); parameter: AddR NetWork Arrangement Line Len Addr Length Type Pf_Inet (AF_INET) Passage: Success - Point Struct Hostent {char far * h_name; char far * far * h_aliases; short h_addrtype; short h_legth; char far * far * h_addr_list;} failed - NULL (Call WsageTlasterror () Description: This letter The formula is to use the location to get other information of Host, such as the name of Host, alias, location, length, etc. (25) gethostByname (): Use a host name to get the information of the host.

转载请注明原文地址:https://www.9cbs.com/read-121577.html

New Post(0)