socket()--Create Socket


  Syntax
  #include <sys/types.h>
  #include <sys/socket.h>

  int socket(int address_family,
             int type,
             int protocol)

  Service Program Name: QSOSRV1

  Default Public Authority: *USE

  Threadsafe: Yes

The socket() function is used to create an end point for communications. The end point is represented by the socket descriptor returned by the socket() function.


Parameters

address_family
(Input) The address family to be used with the socket. Supported values are:
AF_INET For interprocess communications between processes on the same system or different systems in the Internet domain using the Internet Protocol (IPv4).
AF_INET6 For interprocess communications between processes on the same system or different systems in the Internet domain using the Internet Protocol (IPv6 or IPv4).
AF_NS For interprocess communications between processes on the same system or different systems in the domain defined by the Novell or Xerox protocol definitions.

Note: The AF_NS address family is no longer supported as of V5R2.

AF_UNIX For interprocess communications between processes on the same system in the UNIX domain.
AF_UNIX_CCSID For interprocess communications between processes on the same system in the UNIX domain using the Qlg_Path_Name_T structure.
AF_TELEPHONY For interprocess communications between processes on the same system in the telephony domain.

Note: The AF_TELEPHONY address family is no longer supported as of V5R3.

type
(Input) The type of communications desired. Supported values are:

SOCK_DGRAM Indicates a datagram socket is desired.
SOCK_SEQPACKET Indicates a full-duplex sequenced packet socket is desired. Each input and output operation consists of exactly one record.
SOCK_STREAM Indicates a full-duplex stream socket is desired.
SOCK_RAW Indicates communication is directly to the network protocols. A process must have the appropriate privilege *ALLOBJ to use this type of socket. Used by users who want to access the lower-level protocols directly.


protocol
(Input) The protocol to be used on the socket. Supported values are:

0 Indicates that the default protocol for the type selected is to be used. For example, IPPROTO_TCP is chosen for the protocol if the type was set to SOCK_STREAM and the address family is AF_INET.
IPPROTO_IP Equivalent to specifying the value zero (0).
IPPROTO_TCP Indicates that the TCP protocol is to be used.
IPPROTO_UDP Indicates that the UDP protocol is to be used.
IPPROTO_RAW Indicates that communications is to the IP layer.
IPPROTO_ICMP Indicates that the Internet Control Message Protocol (ICMP) is to be used.
IPPROTO_ICMPV6 Indicates that the Internet Control Message Protocol (ICMPv6) is to be used.
TELPROTO_TEL Equivalent to specifying the value zero (0).


Note: When the type is SOCK_RAW, the protocol can be set to some predefined protocol number from 0-255. See Usage Notes for further details.


Authorities

When the SOCKET being created is of type SOCK_RAW, the thread must have *ALLOBJ special authority. When the thread does not have this authority, the EACCES is returned for errno.


Return Value

socket() returns an integer. Possible values are:


Error Conditions

When socket() fails, errno can be set to one of the following:

[EACCES] Permission denied.

Process does not have the appropriate privileges to create the socket with the specified type or protocol.

[EAFNOSUPPORT] The type of socket is not supported in this protocol family.

[EIO] Input/output error.

[EMFILE] Too many descriptions for this process.

[ENFILE] Too many descriptions in system.

[ENOBUFS] There is not enough buffer space for the requested operation.

[EPROTOTYPE] The socket type or protocols are not compatible.

[EPROTONOSUPPORT] No protocol of the specified type and domain exists.

[ESOCKTNOSUPPORT] The specified socket type is not supported.

[EUNATCH] The protocol required to support the specified address family is not available at this time.

[EUNKNOWN] Unknown system state.


Error Messages

Message ID Error Message Text
CPE3418 E Possible APAR condition or hardware failure.
CPF9872 E Program or service program &1 in library &2 ended. Reason code &3.
CPFA081 E Unable to set return value or error code.


Usage Notes

  1. The socket address families and types supported by sockets are defined in <sys/socket.h>. The protocols are defined in <netinet/in.h> (Internet protocols).

  2. The AF_UNIX and AF_UNIX_CCSID address family supports a protocol of 0 for both SOCK_STREAM and SOCK_DGRAM.

  3. The AF_NS address family is no longer supported as of V5R2.

  4. The following tables list the combinations of types and protocols that are supported for AF_INET and the combinations of types and protocols that are supported for AF_INET6.

    Supported Combinations of Types and Protocols for AF_INET
    Socket Type Protocol
    STREAM IPPROTO_TCP (see Usage note 5)
    DGRAM IPPROTO_UDP
    RAW IPPROTO_RAW, IPPROTO_ICMP, protocol_number, (see Usage note 6)


    Supported Combinations of Types and Protocols for AF_INET6
    Socket Type Protocol
    STREAM IPPROTO_TCP
    DGRAM IPPROTO_UDP
    RAW IPPROTO_RAW, IPPROTO_ICMPV6, protocol_number, (see Usage note 6)


  5. The ALWANYNET (Allow ANYNET support) network attribute allows a customer to select whether a SNA transport can be used for AF_INET socket applications.

    The system administrator can see the current status of the ALWANYNET attribute and can change that status. (This can be done by using the Display Network Attributes (DSPNETA) and Change Network Attributes (CHGNETA) commands, respectively.)

    If the status is changed, the change takes effect immediately. Also, the state of the ALWANYNET stays the same across IPLs. For example, if the current status is *YES and the system administrator changes the value to *NO, the use of AF_INET over a transport other than TCP/IP is deactivated. If a system IPL is performed after this point, the use of AF_INET over a SNA transport remains deactivated after the system IPL.

    If AF_INET sockets will only be used over a TCP/IP transport, the ALWANYNET status should be set to *NO to improve CPU utilization.

    Note: If you are also using APPC over TCP/IP ALWANYNET status needs to be set to *YES.

  6. When the socket type is SOCK_RAW, you can specify any protocol number between 0-255. Two exceptions are the IPPROTO_TCP and IPPROTO_UDP protocols, which cannot be specified on a socket type of SOCK_RAW (if you issue socket(), you get an error with an error code of [EPROTONOSUPPORT]). Each raw socket is associated with one IP protocol number, and receives all data for that protocol. For example, if two processes create a raw socket with the same protocol number, and data is received for the protocol, then both processes get copies of the data.

    Protocol numbers 0 (IPPROTO_IP) and 255 (IPPROTO_RAW) have some unique characteristics. If a protocol number of zero is specified, then IP sends all data received from all the protocol numbers (except IPPROTO_TCP and IPPROTO_UDP protocols). If a protocol number of 255 is specified, a user must ensure that the IP header data is included in the data sent out on an output operation.

  7. The AF_TELEPHONY address family is no longer supported as of V5R3.

Related Information



API introduced: V3R1
Top | UNIX-Type APIs | APIs by category