Winsock API function Daquan 1

Source: Internet
Author: User
Tags htons

Title: Winsock API function Daquan 1 Author: Alias Name

Key words: socket, API, Winsock

In this paper, the conversion from C ++ builder research-http://www.ccrun.com/article/go.asp? I = 186 & D = 353200
The Windows Sockets API interface provided by the system (winking) is defined in the routine as defined in Winsock version January 20, 1993 (such as the appendix) published on April 9, 1.1 ); includes 30 Berkeley Software Distribution (BSD) and 16 Windows message-driven features.

BSD functions include:
Accept () bind () closesocket () connect ()
Getpeername () getsockname () getsockopt () htonl ()
Htons () inet_addr () inet_ntoa () ioctlsocket ()
Listen () ntohl () ntohs () Recv ()
Recvfrom () Select () Send () sendto ()
Setsockopt () Shutdown () socket () gethostname ()
Gethostbyaddr () gethostbyname ()
Getprotobyname () getprotobynumber ()
Getservbyname () getservbyport ()

Microsoft Windows-specific extensions:
Wsaasyncgethostbyaddr () wsaasyncgethostbyname ()
Wsaasyncgetprotobyname () wsaasyncgetprotobynumber ()
Wsaasyncgetservbyname () wsaasyncgetservbyport ()
Wsaasyncselect () wsacancelasyncrequest ()
Wsacancelblockingcall () wsacleanup ()
Wsagetlasterror () wsaisblocking ()
Wsasetblockinghook () wsasetlasterror ()
Wsastartup () wsaunhookblockinghook ()

These API interfaces are applicable to Internet Protocol Suite (IPs, usually known as TCP/IP ),
Supports stream (TCP) and datax (UDP) socket.

Stream (TCP) socket provides "two-way", "reliable", "ordered", and "no duplication" data transmission.
Datax (UDP) socket provides "two-way" communication, but it does not provide "reliable", "ordered", "No repeated" and other guarantees; therefore, the user may receive unordered, duplicated data, or even missing data during transmission.

[Blocking and non-blocking modes]

Blocking Mode: when a user calls a function of this mode, it enters the internal function until the conditions or information are fully met and then returns to the call point.

Non-blocking mode: when a user calls a function of this mode, the user enters the internal function and makes an appropriate reply according to the current conditions or materials, the response will not be responded after the conditions or materials are fully met within the form.

You must note that the blocking mode defined by Winsock is not the same as that defined by UNIX. The Winsock definition allows applications to process other messages, including keyboard and mouse events, while calling the blocking function. However, in addition to wsacancelblockingcall () function to cancel the previous locking action or use the wsaisblocking () function to check whether the current blocking action is still in progress, no. "Call other socket functions before the previously called blocking function is completed. Otherwise, the latter fails and produces a wsainprogress error.

If the blocking action canceled by the user's call wsacancelblockingcall () function is not accept () or select (), then the only socket function that can be called later is closesocket (), because canceling the blocking action of a socket changes it to an indeterminate state.

[Async (non-synchronous) mode]

The user does not immediately obtain required information when calling this mode. Instead, the system notifies the caller in another way after the requested action is completed. The advantage is that the user does not have to wait until the answer is answered before performing other actions or requirements.
The async mode defined by Winsock informs the user that the request has been completed in the form of "postmessage". Therefore, when calling such a function, you must inform Windows Sockets DLL of some information, including the window handle and message number for receiving messages.

[Function description]

[BSD socket library]

(1) accept (): Accept the connection requirements of a socket to complete the connection of stream socket.

Format: Socket Pascal far accept (scoket S, struct sockaddr far * ADDR, int far * addrlen);
parameter: s socket identifier ADDR stores the length of the end address addrlen ADDR connected to
return value: Success-new socket identifier failure-invalid_socket (call wsagetlasterror () to learn the cause)
note: the application on the server calls this function to accept the socket connection action required by the client. If the socket on the server is in blocking mode, and no one asks for the connection action, then this function will block the function and immediately reply to the error. The accept () statement is a new socket, which cannot be used to accept other connection requirements. However, the original socket can still accept the connection requirements of others.

(2) bind (): Specifies the local address (address) of the socket ).
Format: int Pascal far BIND (socket S, const struct sockaddr far * Name, int namelen );
Parameter: the IP address value of socket, the ID of S socket, in the format
Struct sockaddr
{
U_short sa_family;
Char sa_data [14];
};
Namelen Name Length
Return Value: Success-0
Failure-socket_error (call wsagetlasterror () to learn the cause)

Note: This function specifies the local address and port to an unnamed socket. If you do not care about the address or port value, you can set the address to inaddr_any and port to 0; then, Windows Sockets automatically sets the appropriate address and port (value between 1024 and 5000). After the socket is actually connected, the user can call getsockname () to obtain the set value.

(3) closesocket (): closes a socket.
Format: int Pascal far closesocket (socket S );
Parameter: s socket identifier
Return Value: Success-0
Failure-socket_error (call wsagetlasterror () to learn the cause)
Note: This function is used to close a socket.
If the user sets so_dontlinger for the socket to be closed, the interface will be overwritten immediately after the call to this function, but the information not delivered by this sokcet will be closed after it is sent.
If the user originally set this socket to so_linger, there are two situations:
(A) If timeout is set to 0, this socket will immediately reset (reset), and all the information that has not been transferred or received will be lost.
(B) If the timeout value is not 0, the data will be sent out, or the data will not be closed until the timeout occurs.

(4) connect (): connect a socket to the specified party.
Format: int Pascal far connect (socket S, const struct sockaddr far * Name, int namelen );
Parameter: s socket identifier
Name the target address of the socket
Namelen Name Length
Return Value: Success-0
Failure-socket_error (call wsagetlasterror () to learn the cause)
Note: This function is used to request the other party to establish a connection. If the specified peer address is 0, an error value is returned. After the connection is established, the user can use this socket for data transmission or receiving.

(5) getpeername (): obtains the address of the Peer address of the successfully connected socket.
Format: int Pascal far getpeername (socket S, struct sockaddr far * Name, int far * namelen );
Parameter: s socket identifier
Name the peer address of the socket connection
Namelen Name Length
Return Value: Success-0
Failure-socket_error (call wsagetlasterror () to learn the cause)
Note: this function can be used to obtain the address of a socket that has been successfully connected.

(6) getsockname (): Obtain the local address information of the socket.
Format: int Pascal far getsockname (socket S, struct sockaddr far * Name, int far * namelen );
Parameter: s socket identifier
Name the local address of this socket
Namelen Name Length
Return Value: Success-0
Failure-socket_error (call wsagetlasterror () to learn the cause)
Note: This function is used to obtain the local address information of the configured address or connected socket. If this socket is set to inaddr_any, the correct address must be returned after the connection is established successfully.

(7) getsockopt (): The current status of a socket is required.
Format: int Pascal far getsockopt (socket S, int level, int optname, char far * optval, int far * optlen);
parameter: S = socket identifier, level = option set, level = optname option name, optval = option set value, optlen = Option Set Value length
return value: success-0
failure-socket_error (call wsagetlasterror () to learn the cause)
Note: This function is used to obtain the status settings of the current socket. For the level provided by Winsock, only sol_socket and ipproto_tcp optname have the following options: (See Winsock 29th and 30)
Value Type
authorization
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
* handle int
so_reuseaddr bool
* so_sndbuf int
so_type int
tcp_nodelay bool
(* indicates that this function option is not provided currently)

(8) htonl (): converts a 32-bit u_long value from host to network.
Format: u_long Pascal far htonl (u_long hostlong );
Parameter: hostlong: the number of 32-bit meta-host sorting methods
Return Value: the number of 32-bit network arrangement modes.
Note: because the network arrangement and host arrangement may be different, we need this function for conversion.

(9) htons (): converts a 16-bit u_short value from host to network.
Format: u_short Pascal far htons (u_short hostshort );
Parameter number: the number of hostshort 16-bit meta-host sorting methods
Return Value: the number of 16-bit network arrangement Modes
Note: because the network arrangement and host arrangement may be different, we need this function for conversion.

(10) inet_addr (): converts the string format address to the 32-bit unsigned long format.
Format: Unsigned long Pascal far inet_addr (const char far * CP );
Parameter: CP: A "Point format" (dotted) string representing the address
Return Value: Success-an unsigned long representing the Internet address
Failed-inaddr_none
Note: This function converts an address string in point format to an appropriate intenet address.
The dot format string can be any of the following four methods:
(I) A. B. C. D (II) A. B. C (III) A. B (iv)

Title: Winsock API function Daquan 2 Author: Alias Name

Key words: socket, API, Winsock

In this paper, the conversion from C ++ builder research-http://www.ccrun.com/article/go.asp? I = 187 & D = 12u0hg
The Windows Sockets API interface provided by the system (winking) is defined in the routine as defined in Winsock version January 20, 1993 (such as the appendix) published on April 9, 1.1 ); includes 30 Berkeley Software Distribution (BSD) and 16 Windows message-driven features.

(11) inet_ntoa (): converts a network address to a "Point format" string.
Format: Char far * Pascal far inet_ntoa (struct in_addr in );
Parameter: in a structure that represents an Internet address
Return Value: Success-a "Point format" (dotted) string representing the address
Failed-Null
Note: This function converts an Internet address to the "A. B. C. D" string format.

(12) ioctlsocket (): controls the socket mode.
Format: int Pascal far ioctlsocket (socket s, long cmd, u_long far * argp );
Parameter: s socket identifier, CMD command name, argp indicates the indicator of the CMD Parameter
Return Value: Success-0
Failure-socket_error (call wsagetlasterror () to learn the cause)
Note: This function is used to obtain or set the operation parameters of the socket. Its Commands include:
Fionbio -- switch non-blocking mode
Fionread-the amount of data that can be read at a time from the socket
Siocatmark-whether the OOB data has been read (* this function is not provided currently)

(13) Listen (): Set the socket to the listening status and prepare to be connected.
Format: int Pascal far listen (socket S, int backlog );
Parameter: the ID of the S socket. the maximum number of connections required by the backlog before the connection is completed (before the accept () is called ).
Return Value: Success-0
Failure-socket_error (call wsagetlasterror () to learn the cause)
Note: The user can use this function to set the socket to enter the listening status, and set the maximum number of connection requirements for one end before the connection is completed. (Currently, the maximum value is 5 and the minimum value is 1)

(14) ntohl (): converts a 32-bit u_long value from network to host.
Format: u_long Pascal far ntohl (u_long netlong );
Parameter: netlong: the number of 32-bit network arrangement methods
Return Value: the number of 32-bit meta-host sorting methods
Note: because the network arrangement and host arrangement may be different, we need this function for conversion.

(15) ntohs (): converts a 16-bit u_short value from network to host.
Format: u_short Pascal far ntohs (u_short netshort );
Parameter: Number of netshort 16-bit network arrangement methods
Return Value: the number of 16-bit meta-host sorting methods
Note: because the network arrangement and host arrangement may be different, we need this function for conversion.

(16) Recv (): receives data from the socket.
Format: int Pascal far Recv (socket S, char far * Buf, int Len, int flags);
parameter: s socket identifier, buf stores the temporary storage area of received data, the length of Len Buf, flags the call method
return value: success-length of received data (0 if the socket of the other party is closed)
failure-socket_error (call wsagetlasterror () to learn the cause)
description: this function is used to receive data from the connected datasync socket or stream socket. For stream socket, we can receive the currently valid (available) data, but its quantity does not exceed the Len size. If so_oobinline is set for this socket and data with out-of-band is not read, only data with out-of-band is taken out.
for dataphin socket, only the first datax is retrieved. If the datax is larger than the storage space provided by the user, only the data of the specified size is retrieved, excessive information will be lost and an error message will be returned. The flags value can be a combination of msg_peek and MSG_OOB (* this function is not currently available).

(17) recvfrom (): Read a data source and store the address of the data source.
Format: int Pascal far recvfrom (socket S, char far * Buf, int Len, int flags, struct socketaddr far * From, int far * fromlen );
Parameter: the ID of S socket, the temporary storage area where the Buf stores the received data, the length of Len Buf, the method in which the flags function is called, the address of the from data source, fromlen from size
Return Value: Success-length of received data (0 if the recipient's socket is disabled)
Failure-socket_error (call wsagetlasterror () to learn the cause)
Note: This function is used to read data and record the address of the data source. For stream socket, the function is the same as Recv (), and The from and fromlen parameters are not used.

(18) Select (): checks whether one or more sockets are in a readable, writable, or wrong state.
Format: int Pascal far select (INT NFDs, fd_set far * readfds, fd_set far * writefds, fd_set far * limit TFDs, construst timeval far * timeout );
Parameter: NFDs does not work here. readfds must be checked for readable sockets, writefds must be checked for writable sockets, and effectfds must be checked for wrong sockets, timeout indicates the waiting time. If it is null, it indicates blocking. This function will wait until an event occurs.
Return Value: Success-Total number of ETS that meet the condition (0 if timeout occurs)
Failure-socket_error (call wsagetlasterror () to learn the cause)
Note: You can use this function to check whether sockets has data to be read, whether there is space for writing, or whether an error occurs.

(19) Send (): Use a connector socket to send data.
Format: int Pascal far send (socket S, const char far * Buf, int Len, int flags);
parameter: s socket identifier, buf stores the temporary storage area of the data to be transferred, the length of Len Buf, flags the call method
return value: success-length of the sent data
failure-socket_error (wsagetlasterror () can be used to learn the cause)
note: this function is applicable to connecting datav or stream socket for data transmission. For datax socket, if the size of datax exceeds the limit, no data is sent and an error value is returned. If the storage space in the transport system is insufficient to store the information to be transmitted, send () will be blocked unless the socket is set to non-blocking mode. The user must also pay attention to the completion of the send () operation, which does not indicate that the information has been successfully delivered to the other party. The value of flags can be set to msg_dontroute (* this function is not currently available) and a combination of MSG_OOB.

(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 struct sockaddr far * To, int tolen );
Parameter: s socket identifier, Buf is used to store the data to be transferred, and Len Buf length. Flags indicates the call method and the address of the data to be delivered, tolen to size
Return Value: success-the length of the sent data
Failure-socket_error (call wsagetlasterror () to learn the cause)
Note: This function applies to data transfer or stream socket to the specified address. For datax socket, if the size of datax exceeds the limit, no data is sent and an error value is returned. For stream socket, the function is the same as send (); The to and tolen parameters do not work here. If the storage space in the transport system is insufficient to store the data to be transferred, sendto () will be blocked unless the socket is set to non-blocking mode. The user must also note that the sendto () operation is completed, which does not indicate that the information has been successfully delivered to the other party. The flags value can be set to a combination of msg_dontroute (* this function is not currently available) and MSG_OOB.

Edited by: 2002-11-01 times: 4654

Title: Winsock API function Daquan 3 Author: Alias Name

Key words: socket, API, Winsock

In this paper, the conversion from C ++ builder research-http://www.ccrun.com/article/go.asp? I = 188 & D = s3lz2f
The Windows Sockets API interface provided by the system (winking) is defined in the routine as defined in Winsock version January 20, 1993 (such as the appendix) published on April 9, 1.1 ); includes 30 Berkeley Software Distribution (BSD) and 16 Windows message-driven features.

(21) setsockopt (): sets the socket status.
Format: int Pascal far setsockopt (socket S, int level, int optname, const char far * optval, int optlen );
Parameter: The identifier of the S socket, the level set by the level option, the optname option name, The optval option setting value, and the optlen option setting value length
Return Value: Success-0
Failure-socket_error (call wsagetlasterror () to learn the cause)
Note: This function is used to set some socket options to change the action.
The following options can be changed: (see page 54 of WinSock 1.1)
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

(22) Shutdown (): stops the socket receiving/transfer function.
Format: int Pascal far Shutdown (socket S, int how );
Parameter: s socket identifier. How indicates the identifier of the action to stop.
Return Value: Success-0
Failure-socket_error (call wsagetlasterror () to learn the cause)
Note: This function is used to stop the subsequent receiving or transmission of the socket.
If the value of how is 0, no data is received.
If the value of how is 1, data cannot be transmitted.
If the value of how is 2, data is no longer received and no longer transmitted.
The shutdown () function does not close the socket. Therefore, the resources occupied by the socket must be released after closesocket () is called.

(23) socket (): Create a socket.
Format: Socket Pascal far socket (int af, int type, int Protocol );
Parameter: AF currently only provides pf_inet (af_inet), type socket type (sock_stream, sock_dgram), Protocol Communication Protocol (set to 0 if not specified by the user)
Return Value: Success-socket identifier
Failed-invalid_socket (call wsagetlasterror () to find the cause)
Note: This function is used to establish a socket and establish the resources used by the socket.
The socket type can be stream socket or datemedisocket.

(24) gethostbyaddr (): uses the address of a host to obtain information about the host.
Format: struct hostent far * Pascal far gethostbyaddr (const char far * ADDR, int Len, int type );
Parameter: Address of the ADDR network arrangement, Len ADDR length, type pf_inet (af_inet)
Return Value: Success-indicator pointing to struct hostent
Struct hostent
{
Char far * h_name;
Char far * h_aliases;
Short h_addrtype;
Short h_length;
Char far * h_addr_list;
}
Failure-null (call wsagetlasterror () to learn the cause)
Note: This function uses the address to obtain other information about the host, such as the host name, alias, address type, and length.

(25) gethostbyname (): use the name of a host to obtain information about the host.
Format: struct hostent far * Pascal far gethostbyname (const char far * Name );
Parameter: name of the host
Return Value: Success-indicator pointing to struct hostent
Struct hostent
{
Char far * h_name;
Char far * h_aliases;
Short h_addrtype;
Short h_length;
Char far * h_addr_list;
}
Failure-null (call wsagetlasterror () to learn the cause)
Note: This function uses the host name to obtain other information, such as the host address, alias, address type, and length.

(26) gethostname (): get the name of the host currently used by the user.
Format: int Pascal far gethostname (char far * Name, int namelen );
Parameter: Name is used to store the temporary storage area of the host name. The namelen name size
Return Value: Success-0
Failure-socket_error (call wsagetlasterror () to learn the cause)
Description: This function is used to obtain the host name.

(27) getprotobyname (): obtains other information about the protocol according to the name of the Protocol.
Format: struct protoent far * Pascal far getprotobyname (const char far * Name );
Parameter: Name communication protocol name
Return Value: Success-1 indicator pointing to struct protoent
Struct protoent
{
Char far * p_name;
Char far * p_aliases;
Short p_proto;
}
Failure-null (call wsagetlasterror () to learn the cause)
Note: The name of the communication agreement is used to learn the alias and ID of the communication agreement.

(28) getprotobynumber (): obtains other information about the protocol according to the number of the Protocol.
Format: struct protoent far * Pascal far getprotobynumber (INT number );
Parameter: Number of communication protocols in host-based arrangement
Return Value: Success-1 indicator pointing to struct protoent
Struct protoent
{
Char far * p_name;
Char far * p_aliases;
Short p_proto;
}
Failure-null (call wsagetlasterror () to learn the cause)
Note: The name and alias of the communication agreement are known by the number of the communication agreement.

(29) getservbyname (): obtains other information about the service according to the service name and communication agreement.
Format: struct servent * Pascal far getservbyname (const char far * Name, const char far * PROTO );
Parameter: Name Service name, proto protocol name
Return Value: Success-1 indicator pointing to struct servent
Struct servent
{
Char far * s_name;
Char far * s_aliases;
Short s_port;
Char far * s_proto;
}
Failure-null (call wsagetlasterror () to learn the cause)
Note: The service name and communication agreement are used to obtain the alias and port number of the service.

(30) getservbyport (): obtains other information about the service according to the Service port number and communication agreement.
Format: struct servent * Pascal far getservbyport (INT port, const char far * PROTO);
parameter: Port Number of the port service, name of the proto protocol, return Value: Success-indicator pointing to struct servent
struct servent
{< br> char far * s_name;
char far * s_aliases;
short s_port;
char far * s_proto;
}< br> failure-null (call wsagetlasterror () to learn the cause)
description: use port numbers and communication protocols to obtain the service name and alias.

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.