Pegasus InfoCorp: Web site design and web software development company

SOCKET (7)

Linux socket interface

SYNOPSIS

    #include <sys/socket.h> mysocket " = socket(int " socket_family ", int " socket_type ", int " protocol );

DESCRIPTION

    This manual page describes the Linux networking socket layer user interface. The BSD compatible sockets are the uniform interface between the user process and the network protocol stacks in the kernel. The protocol modules are grouped into protocol families like PF_INET , PF_IPX , PF_PACKET and socket types like SOCK_STREAM or SOCK_DGRAM . See socket (2) for more information on families and types.

SOCKET LAYER FUNCTIONS

    These functions are used by the user process to send or receive packets and to do other socket operations. For more information see their respective manual pages.

    socket (2) creates a socket, connect (2) connects a socket to a remote socket address, the bind (2) function binds a socket to a local socket address, listen (2) tells the socket that new connections shall be accepted, and accept (2) is used to get a new socket with a new incomming connection. socketpair (2) returns two connected anonymous sockets (only implemented for a few local families like PF_UNIX )

    send (2), sendto (2), and sendmsg (2) send data over a socket, and recv (2), recvfrom (2), recvmsg (2) receive data from a socket. poll (2) and select (2) wait for arriving data or a readiness to send data. In addition, the standard I/O operations like write (2), writev (2), sendfile (2), read (2), and readv (2) can be used to read and write data.

    getsockname (2) returns the local socket address and getpeername (2) returns the remote socket address. getsockopt (2) and setsockopt (2) are used to set or get socket layer or protocol options. ioctl (2) can be used to set or read some other options.

    close (2) is used to close a socket. shutdown (2) closes parts of a full duplex socket connection.

    Seeking, or calling pread (2) or pwrite (2) with a non-zero position is not supported on sockets.

    It is possible to do non-blocking IO on sockets by setting the O_NONBLOCK flag on a socket file descriptor using fcntl (2). O_NONBLOCK is inherited through an accept. Then all operations that would normally block will (usually) return with EAGAIN ; connect (2) returns an EINPROGRESS error in this case. The user can then wait for various events via poll (2) or select (2).

    tab(:) allbox;
    c s s
    l l l.
    I/O events
    Event:Poll flag:Occurrence
    Read:POLLIN:T{
    New data arrived.
    T}
    Read:POLLIN:T{
    A connection setup has been completed
    (for connection-oriented sockets)
    T}
    Read:POLLHUP:T{
    A disconnection request has been initiated by the other end.
    T}
    Read:POLLHUP:T{
    A connection is broken (only for connection-oriented protocols).
    When the socket is writen
    SIGPIPE
    is also sent.
    T}
    Write:POLLOUT:T{
    Socket has enough send buffer space for writing new data.
    T}
    Read/Write:T{
    POLLIN|
       
    POLLOUT
    T}:T{
    An outgoing
      connect (2)  
    finished.
    T}
    Read/Write:POLLERR:An asynchronous error occured.
    Read/Write:POLLHUP:The other end has shut down one direction.
    Exception:POLLPRI:T{
    Urgent data arrived.
    SIGURG
    is sent then.
    T}