accept

Jump to: navigation, search

Table of Contents >> Programmer's Reference


#include <trsocket.h>


int accept (
int listenSocketDescriptor,
struct sockaddr * peerAddressPtr,
int * addressLengthPtr
);


Function Description

The argument socketDescriptor is a socket that has been created with socket(), bound to an address with bind(), and that is listening for connections after a call to listen(). accept() extracts the first connection on the queue of pending connections, creates a new socket with the properties of socketDescriptor, and allocates a new socket descriptor for the socket. If no pending connections are present on the queue and the socket is not marked as non-blocking, accept() blocks the caller until a connection is present. If the socket is marked as non-blocking and no pending connections are present on the queue, accept() returns an error as described below. The accepted socket is used to send and recv data to and from the socket that it is connected to. It is not used to accept more connections. The original socket remains open for accepting further connections. accept() is used with connection-based socket types, currently with SOCK_STREAM.

Using select() (prior to calling accept()):

It is possible to select a listening socket for the purpose of an accept by selecting it for a read. However, this will only indicate when a connect indication is pending; it is still necessary to call accept().

Using tfRegisterSocketCB() (prior to calling accept()):

Alternatively, the user could issue a tfRegisterSocketCB() call on the listening socket with a TM_CB_ACCEPT event flag to get an asynchronous notification of an incoming connection request. Again, this will only indicate that a connection request is pending; it is still necessary to call accept() after having received the asynchronous notification.


Parameters

  • listenSocketDescriptor
    The socket descriptor that was created with socket() and bound to with bind() and is listening for connections with listen().
  • peerAddressPtr
    The structure to write the incoming address into. If peerAddressPtr and addressLengthPtr are NULL then no information about the remote address of the accepted socket is returned.
  • addressLengthPtr
    Initially, it contains the amount of space pointed to by peerAddressPtr. On return it contains the length (in bytes) of the address returned.


Returns

  • >= 0
    New socket descriptor
  • TM_SOCKET_ERROR
    Failure


Note Note: TM_SOCKET_ERROR means that this socket call has failed and the errorCode has been set on the socket itself.

To retrieve the socket error the user must call tfGetSocketError(socketDescriptor).


Possible socket errors

  • TM_EBADF
    The socket descriptor is invalid.
  • TM_EINVAL
    One of the following:
  • peerAddressPtr is 0.
  • addressLengthPtr is 0.
  • *addressLengthPtr is too small.
  • TM_EPERM
    Cannot call accept() without calling listen() first.
  • TM_EOPNOTSUPP
    The referenced socket is not of type SOCK_STREAM.
  • TM_EWOULDBLOCK
    The socket is marked as non-blocking and no connections are present to be accepted.


Table of Contents >> Programmer's Reference