socket()--Create Socket
socket()--Create Socket
#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_INETFor interprocess communications between processes on the same system or different systems in the Internet domain using the Internet Protocol (IPv4). AF_INET6For interprocess communications between processes on the same system or different systems in the Internet domain using the Internet Protocol (IPv6 or IPv4). AF_NSFor 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_NSaddress family is no longer supported as of V5R2.AF_UNIXFor interprocess communications between processes on the same system in the UNIX® domain. AF_UNIX_CCSIDFor interprocess communications between processes on the same system in the UNIX domain using the Qlg_Path_Name_T structure. AF_TELEPHONYFor interprocess communications between processes on the same system in the telephony domain. Note: The
AF_TELEPHONYaddress family is no longer supported as of V5R3. - type
- (Input) The type of communications desired. Supported values are:
SOCK_DGRAMIndicates a datagram socket is desired. SOCK_SEQPACKETIndicates a full-duplex sequenced packet socket is desired. Each input and output operation consists of exactly one record. SOCK_STREAMIndicates a full-duplex stream socket is desired. SOCK_RAWIndicates communication is directly to the network protocols. A process must have the appropriate privilege *ALLOBJto 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_TCPis chosen for the protocol if the type was set toSOCK_STREAMand the address family isAF_INET.IPPROTO_IPEquivalent to specifying the value zero (0). IPPROTO_TCPIndicates that the TCP protocol is to be used. IPPROTO_UDPIndicates that the UDP protocol is to be used. IPPROTO_RAWIndicates that communications is to the IP layer. IPPROTO_ICMPIndicates that the Internet Control Message Protocol (ICMP) is to be used. IPPROTO_ICMPV6Indicates that the Internet Control Message Protocol (ICMPv6) is to be used. TELPROTO_TELEquivalent 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:
- -1 (unsuccessful)
- n (successful), where n is a socket descriptor.
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
- 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).
- The
AF_UNIXandAF_UNIX_CCSIDaddress family supports a protocol of 0 for bothSOCK_STREAMandSOCK_DGRAM. - The
AF_NSaddress family is no longer supported as of V5R2. - The following tables list the combinations of types and protocols that are
supported for
AF_INETand the combinations of types and protocols that are supported forAF_INET6.Supported Combinations of Types and Protocols for AF_INETSocket Type Protocol STREAM IPPROTO_TCP(see Usage note 5)DGRAM IPPROTO_UDPRAW IPPROTO_RAW,IPPROTO_ICMP, protocol_number, (see Usage note 6)Supported Combinations of Types and Protocols for AF_INET6Socket Type Protocol STREAM IPPROTO_TCPDGRAM IPPROTO_UDPRAW IPPROTO_RAW,IPPROTO_ICMPV6, protocol_number, (see Usage note 6) - 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_INETover a transport other than TCP/IP is deactivated. If a system IPL is performed after this point, the use ofAF_INETover 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.
- When the socket type is
SOCK_RAW, you can specify any protocol number between 0-255. Two exceptions are theIPPROTO_TCPandIPPROTO_UDPprotocols, which cannot be specified on a socket type ofSOCK_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 (exceptIPPROTO_TCPandIPPROTO_UDPprotocols). 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. - The
AF_TELEPHONYaddress family is no longer supported as of V5R3.
Related Information
- socketpair()--Create a Pair of Sockets
Additional Information
API introduced: V3R1
[ | UNIX-Type APIs | APIs by category ]