1.\" Copyright (c) 1983, 1990, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" %sccs.include.redist.roff% 5.\" 6.\" @(#)accept.2 8.1 (Berkeley) 06/04/93 7.\" 8.Dd 9.Dt ACCEPT 2 10.Os BSD 4.2 11.Sh NAME 12.Nm accept 13.Nd accept a connection on a socket 14.Sh SYNOPSIS 15.Fd #include <sys/types.h> 16.Fd #include <sys/socket.h> 17.Ft int 18.Fn accept "int s" "struct sockaddr *addr" "int *addrlen" 19.Sh DESCRIPTION 20The argument 21.Fa s 22is a socket that has been created with 23.Xr socket 2 , 24bound to an address with 25.Xr bind 2 , 26and is listening for connections after a 27.Xr listen 2 . 28The 29.Fn accept 30argument 31extracts the first connection request 32on the queue of pending connections, creates 33a new socket with the same properties of 34.Fa s 35and allocates a new file descriptor 36for the socket. If no pending connections are 37present on the queue, and the socket is not marked 38as non-blocking, 39.Fn accept 40blocks the caller until a connection is present. 41If the socket is marked non-blocking and no pending 42connections are present on the queue, 43.Fn accept 44returns an error as described below. 45The accepted socket 46may not be used 47to accept more connections. The original socket 48.Fa s 49remains open. 50.Pp 51The argument 52.Fa addr 53is a result parameter that is filled in with 54the address of the connecting entity, 55as known to the communications layer. 56The exact format of the 57.Fa addr 58parameter is determined by the domain in which the communication 59is occurring. 60The 61.Fa addrlen 62is a value-result parameter; it should initially contain the 63amount of space pointed to by 64.Fa addr ; 65on return it will contain the actual length (in bytes) of the 66address returned. 67This call 68is used with connection-based socket types, currently with 69.Dv SOCK_STREAM . 70.Pp 71It is possible to 72.Xr select 2 73a socket for the purposes of doing an 74.Fn accept 75by selecting it for read. 76.Pp 77For certain protocols which require an explicit confirmation, 78such as 79.Tn ISO 80or 81.Tn DATAKIT , 82.Fn accept 83can be thought of 84as merely dequeueing the next connection 85request and not implying confirmation. 86Confirmation can be implied by a normal read or write on the new 87file desciptor, and rejection can be implied by closing the 88new socket. 89.Pp 90One can obtain user connection request data without confirming 91the connection by issuing a 92.Xr recvmsg 2 93call with an 94.Fa msg_iovlen 95of 0 and a non-zero 96.Fa msg_controllen , 97or by issuing a 98.Xr getsockopt 2 99request. 100Similarly, one can provide user connection rejection information 101by issuing a 102.Xr sendmsg 2 103call with providing only the control information, 104or by calling 105.Xr setsockopt 2 . 106.Sh RETURN VALUES 107The call returns \-1 on error. If it succeeds, it returns a non-negative 108integer that is a descriptor for the accepted socket. 109.Sh ERRORS 110The 111.Fn accept 112will fail if: 113.Bl -tag -width EWOULDBLOCK 114.It Bq Er EBADF 115The descriptor is invalid. 116.It Bq Er ENOTSOCK 117The descriptor references a file, not a socket. 118.It Bq Er EOPNOTSUPP 119The referenced socket is not of type 120.Dv SOCK_STREAM . 121.It Bq Er EFAULT 122The 123.Fa addr 124parameter is not in a writable part of the 125user address space. 126.It Bq Er EWOULDBLOCK 127The socket is marked non-blocking and no connections 128are present to be accepted. 129.El 130.Sh SEE ALSO 131.Xr bind 2 , 132.Xr connect 2 , 133.Xr listen 2 , 134.Xr select 2 , 135.Xr socket 2 136.Sh HISTORY 137The 138.Nm 139function appeared in 140.Bx 4.2 . 141