<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="Copyright" content="Copyright (c) 2006 by IBM Corporation"> <title>socket()--Create Socket</title> <!-- All rights reserved. Licensed Materials Property of IBM --> <!-- US Government Users Restricted Rights --> <!-- Use, duplication or disclosure restricted by --> <!-- GSA ADP Schedule Contract with IBM Corp. --> <!-- Begin Header Records ========================================== --> <!-- Unix8 SCRIPT J converted by B2H R4.1 (346) (CMS) by V2KEA304 --> <!-- at RCHVMW2 on 17 Feb 1999 at 11:05:09 --> <!-- End Header Records ============================================ --> <!-- Edited by Kersten Feb 02 --> <link rel="stylesheet" type="text/css" href="../rzahg/ic.css"> </head> <body> <a name="Top_Of_Page"></a> <!--Java sync-link--> <script type="text/javascript" language="Javascript" src="../rzahg/synch.js"> </script> <h2>socket()--Create Socket</h2> <div class="box" style="width: 60%;"> <br> Syntax <pre> #include <sys/types.h> #include <sys/socket.h> int socket(int <em>address_family</em>, int <em>type</em>, int <em>protocol</em>) </pre> <br> Service Program Name: QSOSRV1<br> <!-- iddvc RMBR --> <br> Default Public Authority: *USE<br> <!-- iddvc RMBR --> <br> Threadsafe: Yes<br> <!-- iddvc RMBR --> <br> </div> <p>The <em>socket()</em> function is used to create an end point for communications. The end point is represented by the socket descriptor returned by the <em>socket()</em> function.</p> <br> <h3>Parameters</h3> <dl> <dt><strong>address_family</strong></dt> <dd>(Input) The address family to be used with the socket. Supported values are: <table cellpadding="5"> <!-- cols="15 85" --> <tr> <td align="left" valign="top"><samp>AF_INET</samp></td> <td align="left" valign="top">For interprocess communications between processes on the same system or different systems in the Internet domain using the Internet Protocol (IPv4).</td> </tr> <tr> <td align="left" valign="top"><samp>AF_INET6</samp></td> <td align="left" valign="top">For interprocess communications between processes on the same system or different systems in the Internet domain using the Internet Protocol (IPv6 or IPv4).</td> </tr> <tr> <td align="left" valign="top"><samp>AF_NS</samp></td> <td align="left" valign="top">For interprocess communications between processes on the same system or different systems in the domain defined by the Novell or Xerox protocol definitions. <p> <strong>Note:</strong> The <samp>AF_NS</samp> address family is no longer supported as of V5R2.</p> </td> </tr> <tr> <td align="left" valign="top"><samp>AF_UNIX</samp></td> <td align="left" valign="top">For interprocess communications between processes on the same system in the UNIX domain.</td> </tr> <tr> <td align="left" valign="top"><samp>AF_UNIX_CCSID</samp></td> <td align="left" valign="top">For interprocess communications between processes on the same system in the UNIX domain using the Qlg_Path_Name_T structure.</td> </tr> <tr> <td align="left" valign="top"><samp>AF_TELEPHONY</samp></td> <td align="left" valign="top">For interprocess communications between processes on the same system in the telephony domain. <p><strong>Note:</strong> The <samp>AF_TELEPHONY</samp> address family is no longer supported as of V5R3.</p> </td> </tr> </table> </dd> <dt><strong>type</strong></dt> <dd>(Input) The type of communications desired. Supported values are:<br> <br> <table cellpadding="5"> <!-- cols="15 85" --> <tr> <td align="left" valign="top"><samp>SOCK_DGRAM</samp></td> <td align="left" valign="top">Indicates a datagram socket is desired.</td> </tr> <tr> <td align="left" valign="top"><samp>SOCK_SEQPACKET</samp></td> <td align="left" valign="top">Indicates a full-duplex sequenced packet socket is desired. Each input and output operation consists of exactly one record.</td> </tr> <tr> <td align="left" valign="top"><samp>SOCK_STREAM</samp></td> <td align="left" valign="top">Indicates a full-duplex stream socket is desired.</td> </tr> <tr> <td align="left" valign="top"><samp>SOCK_RAW</samp></td> <td align="left" valign="top">Indicates communication is directly to the network protocols. A process must have the appropriate privilege <samp>*ALLOBJ</samp> to use this type of socket. Used by users who want to access the lower-level protocols directly.</td> </tr> </table> <br> <br> </dd> <dt><strong>protocol</strong></dt> <dd>(Input) The protocol to be used on the socket. Supported values are:<br> <br> <table cellpadding="5"> <!-- cols="15 85" --> <tr> <td align="left" valign="top">0</td> <td align="left" valign="top">Indicates that the default protocol for the <em>type</em> selected is to be used. For example, <samp>IPPROTO_TCP</samp> is chosen for the protocol if the <em>type</em> was set to <samp>SOCK_STREAM</samp> and the address family is <samp>AF_INET</samp>.</td> </tr> <tr> <td align="left" valign="top"><samp>IPPROTO_IP</samp></td> <td align="left" valign="top">Equivalent to specifying the value zero (0).</td> </tr> <tr> <td align="left" valign="top"><samp>IPPROTO_TCP</samp></td> <td align="left" valign="top">Indicates that the TCP protocol is to be used.</td> </tr> <tr> <td align="left" valign="top"><samp>IPPROTO_UDP</samp></td> <td align="left" valign="top">Indicates that the UDP protocol is to be used.</td> </tr> <tr> <td align="left" valign="top"><samp>IPPROTO_RAW</samp></td> <td align="left" valign="top">Indicates that communications is to the IP layer.</td> </tr> <tr> <td align="left" valign="top"><samp>IPPROTO_ICMP</samp></td> <td align="left" valign="top">Indicates that the Internet Control Message Protocol (ICMP) is to be used.</td> </tr> <tr> <td align="left" valign="top"><samp>IPPROTO_ICMPV6</samp></td> <td align="left" valign="top">Indicates that the Internet Control Message Protocol (ICMPv6) is to be used.</td> </tr> <tr> <td align="left" valign="top"><samp>TELPROTO_TEL</samp></td> <td align="left" valign="top">Equivalent to specifying the value zero (0).</td> </tr> </table> <br> <br> </dd> </dl> <p><strong>Note:</strong> When the <em>type</em> is <samp>SOCK_RAW</samp>, the <em>protocol</em> can be set to some predefined protocol number from 0-255. See <a href="#HDRSCKUS">Usage Notes</a> for further details.</p> <br> <h3>Authorities</h3> <p>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 <em>errno</em>.</p> <br> <h3>Return Value</h3> <p><em>socket()</em> returns an integer. Possible values are:</p> <ul> <li>-1 (unsuccessful)</li> <li>n (successful), where n is a socket descriptor.</li> </ul> <br> <h3>Error Conditions</h3> <p>When <em>socket()</em> fails, <em>errno</em> can be set to one of the following:</p> <table cellpadding="5"> <!-- cols="25 75" --> <tr> <td align="left" valign="top"><em>[EACCES]</em></td> <td align="left" valign="top">Permission denied. <p>Process does not have the appropriate privileges to create the socket with the specified type or protocol.</p> </td> </tr> <tr> <td align="left" valign="top"><em>[EAFNOSUPPORT]</em></td> <td align="left" valign="top">The type of socket is not supported in this protocol family.<br> <br> </td> </tr> <tr> <td align="left" valign="top"><em>[EIO]</em></td> <td align="left" valign="top">Input/output error.<br> <br> </td> </tr> <tr> <td align="left" valign="top"><em>[EMFILE]</em></td> <td align="left" valign="top">Too many descriptions for this process.<br> <br> </td> </tr> <tr> <td align="left" valign="top"><em>[ENFILE]</em></td> <td align="left" valign="top">Too many descriptions in system.<br> <br> </td> </tr> <tr> <td align="left" valign="top"><em>[ENOBUFS]</em></td> <td align="left" valign="top">There is not enough buffer space for the requested operation.<br> <br> </td> </tr> <tr> <td align="left" valign="top"><em>[EPROTOTYPE]</em></td> <td align="left" valign="top">The socket type or protocols are not compatible. <br> <br> </td> </tr> <tr> <td align="left" valign="top"><em>[EPROTONOSUPPORT]</em></td> <td align="left" valign="top">No protocol of the specified type and domain exists.<br> <br> </td> </tr> <tr> <td align="left" valign="top"><em>[ESOCKTNOSUPPORT]</em></td> <td align="left" valign="top">The specified socket type is not supported.<br> <br> </td> </tr> <tr> <td align="left" valign="top"><em>[EUNATCH]</em></td> <td align="left" valign="top">The protocol required to support the specified address family is not available at this time.<br> <br> </td> </tr> <tr> <td align="left" valign="top"><em>[EUNKNOWN]</em></td> <td align="left" valign="top">Unknown system state.</td> </tr> </table> <br> <br> <h3>Error Messages</h3> <table width="100%" cellpadding="5"> <!-- cols="15 85" --> <tr> <th align="left" valign="top" nowrap>Message ID</th> <th align="left" valign="top">Error Message Text</th> </tr> <tr> <td align="left" valign="top">CPE3418 E</td> <td align="left" valign="top">Possible APAR condition or hardware failure.</td> </tr> <tr> <td align="left" valign="top">CPF9872 E</td> <td align="left" valign="top">Program or service program &1 in library &2 ended. Reason code &3.</td> </tr> <tr> <td align="left" valign="top">CPFA081 E</td> <td align="left" valign="top">Unable to set return value or error code.</td> </tr> </table> <br> <br> <h3><a name="HDRSCKUS">Usage Notes</a></h3> <ol> <li>The socket address families and types supported by sockets are defined in <strong><sys/socket.h></strong>. The protocols are defined in <strong><netinet/in.h></strong> (Internet protocols).<br> <br> </li> <li>The <samp>AF_UNIX</samp> and <samp>AF_UNIX_CCSID</samp> address family supports a protocol of 0 for both <samp>SOCK_STREAM</samp> and <samp>SOCK_DGRAM</samp>.<br> <br> </li> <li> The <samp>AF_NS</samp> address family is no longer supported as of V5R2.<br> <br> </li> <li>The following tables list the combinations of types and protocols that are supported for <samp>AF_INET</samp> and the combinations of types and protocols that are supported for <samp>AF_INET6</samp>.<br><br> <table border cellpadding="5"> <!-- cols="15 85" --> <tr> <th align="left" valign="top" colspan="2"><em><a name="#TBLSOCKCOM">Supported Combinations of Types and Protocols for <samp>AF_INET</samp></a></em></th> </tr> <tr> <th align="left" valign="top">Socket Type</th> <th align="left" valign="top">Protocol</th> </tr> <tr> <td align="left" valign="top">STREAM</td> <td align="left" valign="top"><samp>IPPROTO_TCP</samp> (see Usage note 5)</td> </tr> <tr> <td align="left" valign="top">DGRAM</td> <td align="left" valign="top"><samp>IPPROTO_UDP</samp> </td> </tr> <tr> <td align="left" valign="top">RAW</td> <td align="left" valign="top"><samp>IPPROTO_RAW</samp>, <samp>IPPROTO_ICMP</samp>, <em>protocol_number</em>, (see Usage note 6)</td> </tr> </table> <br> <br> <table border cellpadding="5"> <!-- cols="15 85" --> <tr> <th align="left" valign="top" colspan="2"><em><a name="TBLSOCKCOM6">Supported Combinations of Types and Protocols for <samp>AF_INET6</samp></a></em></th> </tr> <tr> <th align="left" valign="top">Socket Type</th> <th align="left" valign="top">Protocol</th> </tr> <tr> <td align="left" valign="top">STREAM</td> <td align="left" valign="top"><samp>IPPROTO_TCP</samp> </td> </tr> <tr> <td align="left" valign="top">DGRAM</td> <td align="left" valign="top"><samp>IPPROTO_UDP</samp> </td> </tr> <tr> <td align="left" valign="top">RAW</td> <td align="left" valign="top"><samp>IPPROTO_RAW</samp>, <samp>IPPROTO_ICMPV6</samp>, <em>protocol_number</em>, (see Usage note 6)</td> </tr> </table> <br> <br> </li> <li>The <strong>ALWANYNET</strong> (Allow ANYNET support) network attribute allows a customer to select whether a SNA transport can be used for AF_INET socket applications. <p>The system administrator can see the current status of the <strong>ALWANYNET</strong> attribute and can change that status. (This can be done by using the Display Network Attributes (DSPNETA) and Change Network Attributes (CHGNETA) commands, respectively.)</p> <p>If the status is changed, the change takes effect immediately. Also, the state of the <strong>ALWANYNET</strong> 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 <samp>AF_INET</samp> over a transport other than TCP/IP is deactivated. If a system IPL is performed after this point, the use of <samp>AF_INET</samp> over a SNA transport remains deactivated after the system IPL.</p> <p>If AF_INET sockets will only be used over a TCP/IP transport, the <strong>ALWANYNET</strong> status should be set to *NO to improve CPU utilization.</p> <p><strong>Note:</strong> If you are also using APPC over TCP/IP <strong>ALWANYNET</strong> status needs to be set to *YES.</p> </li> <li>When the socket type is <samp>SOCK_RAW</samp>, you can specify any protocol number between 0-255. Two exceptions are the <samp>IPPROTO_TCP</samp> and <samp>IPPROTO_UDP</samp> protocols, which cannot be specified on a socket type of <samp>SOCK_RAW</samp> (if you issue <em>socket()</em>, you get an error with an error code of <samp>[EPROTONOSUPPORT]</samp>). 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. <p>Protocol numbers 0 (<samp>IPPROTO_IP</samp>) and 255 (<samp>IPPROTO_RAW</samp>) have some unique characteristics. If a protocol number of zero is specified, then IP sends all data received from all the protocol numbers (except <samp>IPPROTO_TCP</samp> and <samp>IPPROTO_UDP</samp> 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.</p> </li> <li>The <samp>AF_TELEPHONY</samp> address family is no longer supported as of V5R3. </li> </ol> <br> <h3>Related Information</h3> <ul> <li><a href="socketp.htm">socketpair()</a>--Create a Pair of Sockets</li> </ul> <br> <hr> API introduced: V3R1 <hr> <center> <table cellpadding="2" cellspacing="2"> <tr align="center"> <td valign="middle" align="center"><a href="#Top_Of_Page">Top</a> | <a href="unix.htm">UNIX-Type APIs</a> | <a href="aplist.htm">APIs by category</a> </td> </tr> </table> </center> </body> </html>