socket()--Create Socket


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

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


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.
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 OS/400 implementation only supports the Novell protocols.
AF_UNIX
For interprocess communications between processes on the same system or different systems in the UNIX domain.
AF_TELEPHONY
For interprocess communications between processes on the same system in the telephony domain.
Note: The OS/400 implementation supports communication over ISDN telephone networks only.

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.
Note: You must explicitly specify the protocol to be used when the address family is AF_NS.
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.
NNSPROTO_IPX
Indicates that the Novell IPX protocol is to be used.
NNSPROTO_SPX
Indicates that the Novell SPX protocol 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.

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.
[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

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) and <netns/ns.h> (Novell protocols).

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

  3. Figure 1-18 lists the combinations of types and protocols that are supported for AF_NS.

    Figure 1-18. Supported Combinations of Types and Protocols for AF_NS
    Socket Type Protocol
    SEQPACKET NNSPROTO_SPX
    STREAM NNSPROTO_SPX
    DGRAM NNSPROTO_IPX

  4. Figure 1-19 lists the combinations of types and protocols that are supported for AF_INET.

    Figure 1-19. 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)

  5. The ALWANYNET (Allow ANYNET support) network attribute allows a customer to select whether an SNA or IPX 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 an SNA or IPX 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 or IPX, 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.

Related Information
socketpair()--Create a Pair of Sockets


Top | Sockets APIs | APIs by category


[Information Center Home Page | Feedback ] [Legal | AS/400 Glossary]