⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socket.2

📁 ecos实时嵌入式操作系统
💻 2
字号:
.\"	$OpenBSD: socket.2,v 1.18 2000/10/18 05:12:12 aaron Exp $.\"	$NetBSD: socket.2,v 1.5 1995/02/27 12:37:53 cgd Exp $.\".\" Copyright (c) 1983, 1991, 1993.\"	The Regents of the University of California.  All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\"    notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\"    notice, this list of conditions and the following disclaimer in the.\"    documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\"    must display the following acknowledgement:.\"	This product includes software developed by the University of.\"	California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\"    may be used to endorse or promote products derived from this software.\"    without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\"     @(#)socket.2	8.1 (Berkeley) 6/4/93.\".Dd June 4, 1993.Dt SOCKET 2.Os.Sh NAME.Nm socket.Nd create an endpoint for communication.Sh SYNOPSIS.Fd #include <sys/types.h>.Fd #include <sys/socket.h>.Ft int.Fn socket "int domain" "int type" "int protocol".Sh DESCRIPTION.Fn socketcreates an endpoint for communication and returns a descriptor..PpThe.Fa domainparameter specifies a communications domain within whichcommunication will take place; this selects the protocol familywhich should be used.These families are defined in the include file.Ao Pa sys/socket.h Ac .The currently understood formats are.Pp.Bd -literal -offset indent -compactAF_UNIX		(UNIX internal protocols),AF_INET		(ARPA Internet protocols),AF_INET6	(ARPA IPv6 protocols),AF_ISO		(ISO protocols),AF_NS		(Xerox Network Systems protocols),AF_IPX		(Internetwork Packet Exchange), andAF_IMPLINK	(IMP \*(lqhost at IMP\*(rq link layer)..Ed.PpThe socket has the indicated.Fa type ,which specifies the semantics of communication.Currently defined types are:.Pp.Bd -literal -offset indent -compactSOCK_STREAMSOCK_DGRAMSOCK_RAWSOCK_SEQPACKETSOCK_RDM.Ed.PpA.Dv SOCK_STREAMtype provides sequenced, reliable,two-way connection based byte streams.An out-of-band data transmission mechanism may be supported.A.Dv SOCK_DGRAMsocket supportsdatagrams (connectionless, unreliable messages ofa fixed (typically small) maximum length).A.Dv SOCK_SEQPACKETsocket may provide a sequenced, reliable,two-way connection-based data transmission path for datagramsof fixed maximum length; a consumer may be required to readan entire packet with each read system call.This facility is protocol specific, and presently implementedonly for.Dv PF_NS ..Dv SOCK_RAWsockets provide access to internal network protocols and interfaces.The types.Dv SOCK_RAW ,which is available only to the superuser, and.Dv SOCK_RDM ,which is planned,but not yet implemented, are not described here..PpThe.Fa protocolspecifies a particular protocol to be used with the socket.Normally only a single protocol exists to support a particularsocket type within a given protocol family.However, it is possible that many protocols may exist,in which case a particular protocol must be specified in this manner.The protocol number to use is particular to the \*(lqcommunication domain\*(rqin which communication is to take place; see.Xr protocols 5 .A value of 0 for.Fa protocolwill let the system select an appropriate protocol for the requestedsocket type..PpSockets of type.Dv SOCK_STREAMare full-duplex byte streams, similar to pipes.A stream socket must be in a.Em connectedstate before any data may be sent or received on it.A connection to another socket is created with a.Xr connect 2call.Once connected, data may be transferred using.Xr read 2and.Xr write 2calls or some variant of the.Xr send 2and.Xr recv 2calls.When a session has been completed a.Xr close 2may be performed.Out-of-band data may also be transmitted as described in.Xr send 2and received as described in.Xr recv 2 ..PpThe communications protocols used to implement a.Dv SOCK_STREAMensure that data is not lost or duplicated.If a piece of data for which the peer protocol has buffer space cannotbe successfully transmitted within a reasonable length of time, then theconnection is considered broken and calls will indicate an error with \-1returns and with.Er ETIMEDOUTas the specific code in the global variable.Va errno .The protocols optionally keep sockets.Dq warmby forcing transmissions roughly every minute in the absence of other activity.An error is then indicated if no response can be elicited on an otherwiseidle connection for a extended period (e.g., 5 minutes).A.Dv SIGPIPEsignal is raised if a process sends on a broken stream; this causesnaive processes, which do not handle the signal, to exit..Pp.Dv SOCK_SEQPACKETsockets employ the same system callsas.Dv SOCK_STREAMsockets.The only difference is that.Xr read 2calls will return only the amount of data requested,and any remaining in the arriving packet will be discarded..Pp.Dv SOCK_DGRAMand.Dv SOCK_RAWsockets allow sending of datagrams to correspondents named in.Xr send 2calls.Datagrams are generally received with.Xr recvfrom 2 ,which returns the next datagram with its return address..PpAn.Xr fcntl 2call can be used to specify a process group to receivea.Dv SIGURGsignal when the out-of-band data arrives.It may also enable non-blocking I/O and asynchronous notificationof I/O events via.Dv SIGIO ..PpThe operation of sockets is controlled by socket level.Em options .These options are defined in the file.Ao Pa sys/socket.h Ac ..Xr setsockopt 2and.Xr getsockopt 2are used to set and get options, respectively..Sh RETURN VALUESA \-1 is returned if an error occurs, otherwise the returnvalue is a descriptor referencing the socket..Sh ERRORSThe.Fn socketcall fails if:.Bl -tag -width Er.It Bq Er EPROTONOSUPPORTThe protocol type or the specified protocol is not supportedwithin this domain..It Bq Er EMFILEThe per-process descriptor table is full..It Bq Er ENFILEThe system file table is full..It Bq Er EACCESPermission to create a socket of the specified type and/or protocolis denied..It Bq Er ENOBUFSInsufficient buffer space is available.The socket cannot be created until sufficient resources are freed..El.Sh SEE ALSO.Xr accept 2 ,.Xr bind 2 ,.Xr connect 2 ,.Xr getsockname 2 ,.Xr getsockopt 2 ,.Xr ioctl 2 ,.Xr listen 2 ,.Xr poll 2 ,.Xr read 2 ,.Xr recv 2 ,.Xr select 2 ,.Xr send 2 ,.Xr setsockopt 2 ,.Xr shutdown 2 ,.Xr socketpair 2 ,.Xr write 2 ,.Xr getprotoent 3 ,.Xr netintro 4.Rs.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial".%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1".Re.Rs.%T "BSD Interprocess Communication Tutorial".%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1".Re.Sh HISTORYThe.Fn socketfunction call appeared in.Bx 4.2 .

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -