Thread: accept() for unix

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    103

    accept() for unix

    I am reading beej's network guide (Beej's Guide to Network Programming) and I'm on on the section where he explains the accept system call.

    What I don't understand is that how does he know the connecting person's address before using accept()?!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't. Why should you? accept() gives you the address of the other end in the second parameter.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    So, what should I pass for the arguments?

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by man 2 accept
    ACCEPT(2) Linux Programmer's Manual ACCEPT(2)

    NAME
    accept - accept a connection on a socket

    SYNOPSIS
    #include <sys/types.h> /* See NOTES */
    #include <sys/socket.h>

    int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

    #define _GNU_SOURCE
    #include <sys/socket.h>

    int accept4(int sockfd, struct sockaddr *addr,
    socklen_t *addrlen, int flags);

    DESCRIPTION
    The accept() system call is used with connection-based socket types (SOCK_STREAM, SOCK_SEQPACKET). It extracts the first connection
    request on the queue of pending connections for the listening socket, sockfd, creates a new connected socket, and returns a new file
    descriptor referring to that socket. The newly created socket is not in the listening state. The original socket sockfd is unaf-
    fected by this call.

    The argument sockfd is a socket that has been created with socket(2), bound to a local address with bind(2), and is listening for
    connections after a listen(2).

    The argument addr is a pointer to a sockaddr structure. This structure is filled in with the address of the peer socket, as known
    to the communications layer. The exact format of the address returned addr is determined by the socket's address family (see
    socket(2) and the respective protocol man pages). When addr is NULL, nothing is filled in; in this case, addrlen is not used, and
    should also be NULL.

    The addrlen argument is a value-result argument: the caller must initialize it to contain the size (in bytes) of the structure
    pointed to by addr; on return it will contain the actual size of the peer address.

    The returned address is truncated if the buffer provided is too small; in this case, addrlen will return a value greater than was
    supplied to the call.

    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 non-blocking and no pending connections are present on the queue, accept() fails
    with the error EAGAIN.

    In order to be notified of incoming connections on a socket, you can use select(2) or poll(2). A readable event will be delivered
    when a new connection is attempted and you may then call accept() to get a socket for that connection. Alternatively, you can set
    the socket to deliver SIGIO when activity occurs on a socket; see socket(7) for details.

    For certain protocols which require an explicit confirmation, such as DECNet, accept() can be thought of as merely dequeuing the
    next connection request and not implying confirmation. Confirmation can be implied by a normal read or write on the new file
    I didn't copy the whole thing, but here ya go!

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Poincare View Post
    So, what should I pass for the arguments?
    Did you read the page you linked to?
    addr will usually be a pointer to a local struct sockaddr_storage. This is where the information about the incoming connection will go (and with it you can determine which host is calling you from which port). addrlen is a local integer variable that should be set to sizeof(struct sockaddr_storage) before its address is passed to accept(). accept() will not put more than that many bytes into addr. If it puts fewer in, it'll change the value of addrlen to reflect that.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    So, what should I pass for the arguments?
    I think what's confusing you is the second parameter. That's actually being used as a form of output, rather than a form of input. You supply a pointer to the function, and the function will fill the location pointed to with the information about the sender's address. So you allocate some space, get a pointer to it, and give that to the function so the function can use it. That's actually not an uncommon practice when functions need to return more complex data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. could not accept socket
    By Elkvis in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2008, 08:42 AM
  2. accept() fails
    By Desolation in forum Networking/Device Communication
    Replies: 3
    Last Post: 05-16-2006, 07:37 AM
  3. accept() Hangs
    By Epo in forum Networking/Device Communication
    Replies: 14
    Last Post: 09-09-2005, 11:53 AM
  4. async Client/Server app, accept() stalls?
    By JaWiB in forum Networking/Device Communication
    Replies: 14
    Last Post: 01-31-2005, 05:59 PM
  5. Make accept() stop
    By b00l34n in forum Networking/Device Communication
    Replies: 28
    Last Post: 12-20-2004, 06:50 PM