📄 socket.texi
字号:
@comment netinet/in.h@comment BSD@deftypevr Macro int IPPORT_USERRESERVEDPort numbers greater than or equal to @code{IPPORT_USERRESERVED} arereserved for explicit use; they will never be allocated automatically.@end deftypevr@node Services Database@subsection The Services Database@cindex services database@cindex converting service name to port number@cindex converting port number to service name@pindex /etc/servicesThe database that keeps track of ``well-known'' services is usuallyeither the file @file{/etc/services} or an equivalent from a name server.You can use these utilities, declared in @file{netdb.h}, to accessthe services database.@pindex netdb.h@comment netdb.h@comment BSD@deftp {Data Type} {struct servent}This data type holds information about entries from the services database.It has the following members:@table @code@item char *s_nameThis is the ``official'' name of the service.@item char **s_aliasesThese are alternate names for the service, represented as an array ofstrings. A null pointer terminates the array.@item int s_portThis is the port number for the service. Port numbers are given innetwork byte order; see @ref{Byte Order}.@item char *s_protoThis is the name of the protocol to use with this service.@xref{Protocols Database}.@end table@end deftpTo get information about a particular service, use the@code{getservbyname} or @code{getservbyport} functions. The informationis returned in a statically-allocated structure; you must copy theinformation if you need to save it across calls.@comment netdb.h@comment BSD@deftypefun {struct servent *} getservbyname (const char *@var{name}, const char *@var{proto})The @code{getservbyname} function returns information about theservice named @var{name} using protocol @var{proto}. If it can't findsuch a service, it returns a null pointer.This function is useful for servers as well as for clients; serversuse it to determine which port they should listen on (@pxref{Listening}).@end deftypefun@comment netdb.h@comment BSD@deftypefun {struct servent *} getservbyport (int @var{port}, const char *@var{proto})The @code{getservbyport} function returns information about theservice at port @var{port} using protocol @var{proto}. If it can'tfind such a service, it returns a null pointer.@end deftypefunYou can also scan the services database using @code{setservent},@code{getservent}, and @code{endservent}. Be careful in using thesefunctions, because they are not reentrant.@comment netdb.h@comment BSD@deftypefun void setservent (int @var{stayopen})This function opens the services database to begin scanning it.If the @var{stayopen} argument is nonzero, this sets a flag so thatsubsequent calls to @code{getservbyname} or @code{getservbyport} willnot close the database (as they usually would). This makes for moreefficiency if you call those functions several times, by avoidingreopening the database for each call.@end deftypefun@comment netdb.h@comment BSD@deftypefun {struct servent *} getservent (void)This function returns the next entry in the services database. Ifthere are no more entries, it returns a null pointer.@end deftypefun@comment netdb.h@comment BSD@deftypefun void endservent (void)This function closes the services database.@end deftypefun@node Byte Order@subsection Byte Order Conversion@cindex byte order conversion, for socket@cindex converting byte order@cindex big-endian@cindex little-endianDifferent kinds of computers use different conventions for theordering of bytes within a word. Some computers put the mostsignificant byte within a word first (this is called ``big-endian''order), and others put it last (``little-endian'' order).@cindex network byte orderSo that machines with different byte order conventions cancommunicate, the Internet protocols specify a canonical byte orderconvention for data transmitted over the network. This is knownas the @dfn{network byte order}.When establishing an Internet socket connection, you must make sure thatthe data in the @code{sin_port} and @code{sin_addr} members of the@code{sockaddr_in} structure are represented in the network byte order.If you are encoding integer data in the messages sent through thesocket, you should convert this to network byte order too. If you don'tdo this, your program may fail when running on or talking to other kindsof machines.If you use @code{getservbyname} and @code{gethostbyname} or@code{inet_addr} to get the port number and host address, the values arealready in the network byte order, and you can copy them directly intothe @code{sockaddr_in} structure.Otherwise, you have to convert the values explicitly. Use@code{htons} and @code{ntohs} to convert values for the @code{sin_port}member. Use @code{htonl} and @code{ntohl} to convert values for the@code{sin_addr} member. (Remember, @code{struct in_addr} is equivalentto @code{unsigned long int}.) These functions are declared in@file{netinet/in.h}.@pindex netinet/in.h@comment netinet/in.h@comment BSD@deftypefun {unsigned short int} htons (unsigned short int @var{hostshort})This function converts the @code{short} integer @var{hostshort} fromhost byte order to network byte order.@end deftypefun@comment netinet/in.h@comment BSD@deftypefun {unsigned short int} ntohs (unsigned short int @var{netshort})This function converts the @code{short} integer @var{netshort} fromnetwork byte order to host byte order.@end deftypefun@comment netinet/in.h@comment BSD@deftypefun {unsigned long int} htonl (unsigned long int @var{hostlong})This function converts the @code{long} integer @var{hostlong} fromhost byte order to network byte order.@end deftypefun@comment netinet/in.h@comment BSD@deftypefun {unsigned long int} ntohl (unsigned long int @var{netlong})This function converts the @code{long} integer @var{netlong} fromnetwork byte order to host byte order.@end deftypefun@node Protocols Database@subsection Protocols Database@cindex protocols databaseThe communications protocol used with a socket controls low-leveldetails of how data is exchanged. For example, the protocol implementsthings like checksums to detect errors in transmissions, and routinginstructions for messages. Normal user programs have little reason tomess with these details directly.@cindex TCP (Internet protocol)The default communications protocol for the Internet namespace depends onthe communication style. For stream communication, the default is TCP(``transmission control protocol''). For datagram communication, thedefault is UDP (``user datagram protocol''). For reliable datagramcommunication, the default is RDP (``reliable datagram protocol'').You should nearly always use the default.@pindex /etc/protocolsInternet protocols are generally specified by a name instead of anumber. The network protocols that a host knows about are stored in adatabase. This is usually either derived from the file@file{/etc/protocols}, or it may be an equivalent provided by a nameserver. You look up the protocol number associated with a namedprotocol in the database using the @code{getprotobyname} function.Here are detailed descriptions of the utilities for accessing theprotocols database. These are declared in @file{netdb.h}.@pindex netdb.h@comment netdb.h@comment BSD@deftp {Data Type} {struct protoent}This data type is used to represent entries in the network protocolsdatabase. It has the following members:@table @code@item char *p_nameThis is the official name of the protocol.@item char **p_aliasesThese are alternate names for the protocol, specified as an array ofstrings. The last element of the array is a null pointer.@item int p_protoThis is the protocol number (in host byte order); use this member as the@var{protocol} argument to @code{socket}.@end table@end deftpYou can use @code{getprotobyname} and @code{getprotobynumber} to searchthe protocols database for a specific protocol. The information isreturned in a statically-allocated structure; you must copy theinformation if you need to save it across calls.@comment netdb.h@comment BSD@deftypefun {struct protoent *} getprotobyname (const char *@var{name})The @code{getprotobyname} function returns information about thenetwork protocol named @var{name}. If there is no such protocol, itreturns a null pointer.@end deftypefun@comment netdb.h@comment BSD@deftypefun {struct protoent *} getprotobynumber (int @var{protocol})The @code{getprotobynumber} function returns information about thenetwork protocol with number @var{protocol}. If there is no suchprotocol, it returns a null pointer.@end deftypefunYou can also scan the whole protocols database one protocol at a time byusing @code{setprotoent}, @code{getprotoent}, and @code{endprotoent}.Be careful in using these functions, because they are not reentrant.@comment netdb.h@comment BSD@deftypefun void setprotoent (int @var{stayopen})This function opens the protocols database to begin scanning it.If the @var{stayopen} argument is nonzero, this sets a flag so thatsubsequent calls to @code{getprotobyname} or @code{getprotobynumber} willnot close the database (as they usually would). This makes for moreefficiency if you call those functions several times, by avoidingreopening the database for each call.@end deftypefun@comment netdb.h@comment BSD@deftypefun {struct protoent *} getprotoent (void)This function returns the next entry in the protocols database. Itreturns a null pointer if there are no more entries.@end deftypefun@comment netdb.h@comment BSD@deftypefun void endprotoent (void)This function closes the protocols database.@end deftypefun@node Inet Example@subsection Internet Socket ExampleHere is an example showing how to create and name a socket in theInternet namespace. The newly created socket exists on the machine thatthe program is running on. Rather than finding and using the machine'sInternet address, this example specifies @code{INADDR_ANY} as the hostaddress; the system replaces that with the machine's actual address.@smallexample@include mkisock.c.texi@end smallexampleHere is another example, showing how you can fill in a @code{sockaddr_in}structure, given a host name string and a port number:@smallexample@include isockad.c.texi@end smallexample@node Misc Namespaces@section Other Namespaces@vindex PF_NS@vindex PF_ISO@vindex PF_CCITT@vindex PF_IMPLINK@vindex PF_ROUTECertain other namespaces and associated protocol families are supportedbut not documented yet because they are not often used. @code{PF_NS}refers to the Xerox Network Software protocols. @code{PF_ISO} standsfor Open Systems Interconnect. @code{PF_CCITT} refers to protocols fromCCITT. @file{socket.h} defines these symbols and others naming protocolsnot actually implemented.@code{PF_IMPLINK} is used for communicating between hosts and InternetMessage Processors. For information on this, and on @code{PF_ROUTE}, anoccasionally-used local area routing protocol, see the GNU Hurd Manual(to appear in the future).@node Open/Close Sockets@section Opening and Closing SocketsThis section describes the actual library functions for opening andclosing sockets. The same functions work for all namespaces andconnection styles.@menu* Creating a Socket:: How to open a socket.* Closing a Socket:: How to close a socket.* Socket Pairs:: These are created like pipes.@end menu@node Creating a Socket@subsection Creating a Socket@cindex creating a socket@cindex socket, creating@cindex opening a socketThe primitive for creating a socket is the @code{socket} function,declared in @file{sys/socket.h}.@pindex sys/socket.h@comment sys/socket.h@comment BSD@deftypefun int socket (int @var{namespace}, int @var{style}, int @var{protocol})This function creates a socket and specifies communication style@var{style}, which should be one of the socket styles listed in@ref{Communication Styles}. The @var{namespace} argument specifiesthe namespace; it must be @code{PF_FILE} (@pxref{File Namespace}) or@code{PF_INET} (@pxref{Internet Namespace}). @var{protocol}designates the specific protocol (@pxref{Socket Concepts}); zero isusually right for @var{protocol}.The return value from @code{socket} is the file descriptor for the newsocket, or @code{-1} in case of error. The following @code{errno} errorconditions are defined for this function:@table @code@item EPROTONOSUPPORTThe @var{protocol} or @var{style} is not supported by the@var{namespace} specified.@item EMFILEThe process already has too many file descriptors open.@item ENFILE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -