Name

socket - create an endpoint for communication

Library

libc.lib

Synopsis

  #include <sys/socket.h>
  int socket (int domain, int type, int protocol);

Detailed description

The socket system call creates an endpoint for communication and returns a descriptor.

The domain argument specifies a communications domain within which communication will take place; this selects the protocol family which should be used. These families are defined in the include file

  #include <sys/socket.h.>The currently understood formats are:

PF_LOCAL        Host-internal protocols, formerly called PF_UNIX,
PF_INET         Internet version 4 protocols,

                     

The socket has the indicated type, which specifies the semantics of communication. Currently defined types are:

SOCK_STREAM     Stream socket,
SOCK_DGRAM      Datagram socket,
SOCK_SEQPACKET  Sequenced packet stream

                     

A SOCK_STREAM type provides sequenced, reliable, two-way connection based byte streams. An out-of-band data transmission mechanism may be supported. A SOCK_DGRAM socket supports datagrams (connectionless, unreliable messages of a fixed (typically small) maximum length). A SOCK_SEQPACKET socket may provide a sequenced, reliable, two-way connection-based data transmission path for datagrams of fixed maximum length; a consumer may be required to read an entire packet with each read system call. This facility is protocol specific, and presently unimplemented.

The protocol argument specifies a particular protocol to be used with the socket. Normally only a single protocol exists to support a particular socket 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 "communication domain" in which communication is to take place.

Sockets of type SOCK_STREAM are full-duplex byte streams, similar to pipes. A stream socket must be in a connected state before any data may be sent or received on it. A connection to another socket is created with a connect system call. Once connected, data may be transferred using read and write calls or some variant of the send and recv functions. (Some protocol families, such as the Internet family, support the notion of an "implied connect," which permits data to be sent piggybacked onto a connect operation by using the sendto system call.) When a session has been completed a close may be performed. Out-of-band data may also be transmitted as described in send and received as described in recv

The communications protocols used to implement a SOCK_STREAM insure that data is not lost or duplicated. If a piece of data for which the peer protocol has buffer space cannot be successfully transmitted within a reasonable length of time, then the connection is considered broken and calls will indicate an error with -1 returns and with ETIMEDOUT as the specific code in the global variable errno. The protocols optionally keep sockets "warm" by forcing transmissions roughly every minute in the absence of other activity. An error is then indicated if no response can be elicited on an otherwise idle connection for an extended period (e.g. 5 minutes).

SOCK_SEQPACKET sockets employ the same system calls as SOCK_STREAM sockets. The only difference is that read calls will return only the amount of data requested, and any remaining in the arriving packet will be discarded.

SOCK_DGRAM sockets allow sending of datagrams to correspondents named in send calls. Datagrams are generally received with recvfrom which returns the next datagram with its return address.


Examples

#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#inlcude <netinet/in.h>
void SocketExample()
{
    int sock_fd;
    sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    close(sock_fd);
}

         

Return values

The socket() function returns valid socket descriptor if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

Errors

The socket system call fails if:
[EPROTONOSUPPORT]
  The protocol type or the specified protocol is not supported within this domain.
[EMFILE]
  The per-process descriptor table is full.
[ENFILE]
  The system file table is full.
[EACCES]
  Permission to create a socket of the specified type and/or protocol is denied.
[ENOBUFS]
  Insufficient buffer space is available. The socket cannot be created until sufficient resources are freed.

See also

accept bind connect getpeername getsockname getsockopt ioctl listen read recv select send shutdown write
An Introductory 4.3 BSD Interprocess Communication Tutorial
BSD Interprocess Communication Tutorial
The socket system call appeared in BSD 4.2 .

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top