<p>I'm having issues getting the C sockets API to work properly in C++ on z/OS.</p>
<p>Although I am including <code>sys/socket.h</code>, I still get compile time errors telling me that <code>AF_INET</code> is not defined.</p>
<p>Am I missing something obvious, or is this related to the fact that being on z/OS makes my problems much more complicated?</p>
<p>I discovered that there is an <code>#ifdef</code> that I'm hitting. Apparently z/OS isn't happy unless I define which &quot;type&quot; of sockets I'm using with:</p>
<pre><code>#define _OE_SOCKETS
</code></pre>
<p>Now, I personally have no idea what this <code>_OE_SOCKETS</code> is actually for, so if any z/OS sockets programmers are out there (all 3 of you), perhaps you could give me a rundown of how this all works?</p>
<p>Test App</p>
<pre><code>#include &lt;sys/socket.h&gt;

int main()
{
    return AF_INET;
}
</code></pre>
<p>Compile/Link Output:</p>
<pre><code>cxx -Wc,xplink -Wl,xplink -o inet_test inet.C

&quot;./inet.C&quot;, line 5.16: CCN5274 (S) The name lookup for &quot;AF_INET&quot; did not find a declaration.
CCN0797(I) Compilation failed for file ./inet.C. Object file not created.
</code></pre>
<p>A check of sys/sockets.h does include the definition I need, and as far as I can tell, it is not being blocked by any <code>#ifdef</code> statements.</p>
<p>I have however noticed it contains the following:</p>
<pre><code>#ifdef __cplusplus
  extern &quot;C&quot; {
#endif
</code></pre>
<p>which encapsulates basically the whole file? Not sure if it matters.</p>

[{'answer_id': 30, 'author': 'C. K. Young', 'author_id': 13, 'author_profile': 'https://Stackoverflow.com/users/13', 'pm_score': 5, 'selected': False, 'text': "<p>I've had no trouble using the BSD sockets API in C++, in GNU/Linux. Here's the sample program I used:</p>\n\n<pre><code>#include &lt;sys/socket.h&gt;\n\nint\nmain()\n{\n    return AF_INET;\n}\n</code></pre>\n\n<p>So my take on this is that z/OS is probably the complicating factor here, however, because I've never used z/OS before, much less programmed in it, I can't say this definitively. :-P</p>\n"}, {'answer_id': 73, 'author': 'C. K. Young', 'author_id': 13, 'author_profile': 'https://Stackoverflow.com/users/13', 'pm_score': 5, 'selected': False, 'text': '<p>@Jax: The <code>extern "C"</code> thing matters, very very much. If a header file doesn\'t have one, then (unless it\'s a C++-only header file), you would have to enclose your <code>#include</code> with it:</p>\n\n<pre><code>extern "C" {\n#include &lt;sys/socket.h&gt;\n// include other similarly non-compliant header files\n}\n</code></pre>\n\n<p>Basically, anytime where a C++ program wants to link to C-based facilities, the <code>extern "C"</code> is vital. In practical terms, it means that the names used in external references will not be mangled, like normal C++ names would. <a href="http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html" rel="noreferrer">Reference.</a></p>\n'}, {'answer_id': 35029, 'author': 'David Bryson', 'author_id': 3663, 'author_profile': 'https://Stackoverflow.com/users/3663', 'pm_score': 4, 'selected': False, 'text': '<p>DISCLAIMER: I am not a C++ programmer, however I know C really well.  I\nadapated these calls from some C code I have.</p>\n\n<p>Also markdown put these strange &#95; as my underscores.</p>\n\n<p>You should just be able to write an abstraction class around the C sockets with something like this:</p>\n\n<pre><code>class my_sock {\n    private int sock;\n    private int socket_type;\n    private socklen_t sock_len;\n    private struct sockaddr_in server_addr;\n    public char *server_ip;\n    public unsigned short server_port;\n};\n</code></pre>\n\n<p>Then have methods for opening, closing, and sending packets down the socket.</p>\n\n<p>For example, the open call might look something like this:</p>\n\n<pre><code>int my_socket_connect()\n{\n    int return_code = 0;\n\n    if ( this-&gt;socket_type != CLIENT_SOCK ) {\n        cout &lt;&lt; "This is a not a client socket!\\n";\n        return -1;\n    }\n\n    return_code = connect( this-&gt;local_sock, (struct sockaddr *) &amp;this-&gt;server_addr, sizeof(this-&gt;server_addr));\n\n    if( return_code &lt; 0 ) {\n        cout &lt;&lt; "Connect() failure! %s\\n", strerror(errno);\n        return return_code;\n    }\n\n    return return_code;\n}\n</code></pre>\n'}, {'answer_id': 48248, 'author': 'Federico', 'author_id': 4981, 'author_profile': 'https://Stackoverflow.com/users/4981', 'pm_score': 5, 'selected': False, 'text': '<p>You may want to take a look to <a href="http://sourceforge.net/projects/cpp-sockets/" rel="noreferrer">cpp-sockets</a>, a C++ wrapper for the sockets system calls. It works with many operating systems (Win32, POSIX, Linux, *BSD). I don\'t think it will work with z/OS but you can take a look at the include files it uses and you\'ll have many examples of tested code that works well on other OSs.</p>\n'}, {'answer_id': 110917, 'author': 'fizzer', 'author_id': 18167, 'author_profile': 'https://Stackoverflow.com/users/18167', 'pm_score': 5, 'selected': False, 'text': '<p>So try</p>\n\n<pre><code>#define _OE_SOCKETS\n</code></pre>\n\n<p>before you include sys/socket.h</p>\n'}, {'answer_id': 193478, 'author': 'Fabio Ceconello', 'author_id': 8999, 'author_profile': 'https://Stackoverflow.com/users/8999', 'pm_score': 5, 'selected': False, 'text': '<p>The _OE_SOCKETS appears to be simply to enable/disable the definition of socket-related symbols. It is not uncommon in some libraries to have a bunch of macros to do that, to assure that you\'re not compiling/linking parts not needed. The macro is not standard in other sockets implementations, it appears to be something specific to z/OS.</p>\n\n<p>Take a look at this page:<br>\n<a href="http://publib.boulder.ibm.com/infocenter/zvm/v5r3/index.jsp?topic=/com.ibm.zvm.v53.kiml0/sktnew.htm" rel="noreferrer">Compiling and Linking a z/VM C Sockets Program</a></p>\n'}, {'answer_id': 867088, 'author': 'Robert Groves', 'author_id': 3534, 'author_profile': 'https://Stackoverflow.com/users/3534', 'pm_score': 5, 'selected': False, 'text': '<p>See the <em>Using z/OS UNIX System Services sockets</em> section in the z/OS XL C/C++ Programming Guide.  Make sure you\'re including the necessary header files and using the appropriate #defines.</p>\n\n<p>The link to the doc has changed over the years, but you should be able to get to it easily enough by finding the current location of the <a href="http://ibm.com/support/entry/portal/documentation" rel="noreferrer">Support &amp; Downloads section</a> on <a href="http://www.ibm.com/" rel="noreferrer">ibm.com</a> and searching the documentation by title.</p>\n'}, {'answer_id': 1443907, 'author': 'Anthony Giorgio', 'author_id': 9816, 'author_profile': 'https://Stackoverflow.com/users/9816', 'pm_score': 8, 'selected': True, 'text': '<p>Keep a copy of the IBM manuals handy:</p>\n\n<ul>\n<li><a href="http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/CBCPG1A0/CCONTENTS?DT=20090606065305" rel="noreferrer"> z/OS V1R11.0 XL C/C++ Programming Guide</a>  </li>\n<li><a href="http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/EDCLB1A0/CCONTENTS?DT=20090607203246" rel="noreferrer"> z/OS V1R11.0 XL C/C++ Run-Time Library Reference</a>  </li>\n</ul>\n\n<p>The IBM publications are generally very good, but you need to get used to their format, as well as knowing where to look for an answer.  You\'ll find quite often that a feature that you want to use is guarded by a "feature test macro"</p>\n\n<p>You should ask your friendly system programmer to install the <a href="http://www-03.ibm.com/servers/eserver/zseries/zos/le/manpgs.html" rel="noreferrer">XL C/C++ Run-Time Library Reference: Man Pages\n</a> on your system.  Then you can do things like "man connect" to pull up the man page for the socket connect() API.  When I do that, this is what I see:</p>\n\n<p>FORMAT</p>\n\n<p>X/Open</p>\n\n<pre><code>#define _XOPEN_SOURCE_EXTENDED 1\n#include &lt;sys/socket.h&gt;\n\nint connect(int socket, const struct sockaddr *address, socklen_t address_len);\n</code></pre>\n\n<p>Berkeley Sockets</p>\n\n<pre><code>#define _OE_SOCKETS\n#include &lt;sys/types.h&gt;\n#include &lt;sys/socket.h&gt;\n\nint connect(int socket, struct sockaddr *address, int address_len);\n</code></pre>\n'}, {'answer_id': 5626132, 'author': 'Hope T. Helps', 'author_id': 702754, 'author_profile': 'https://Stackoverflow.com/users/702754', 'pm_score': 4, 'selected': False, 'text': "<p>Use the following c89 flag:</p>\n<pre><code> -D_OE_SOCKETS\n</code></pre>\n<p>Example:</p>\n<pre><code> bash-2.03$ c89 -D_OE_SOCKETS [filename].c\n</code></pre>\n<p>For more information look for c89 Options in the z/OS XLC/C++ User's Guide.</p>\n"}]
['https://Stackoverflow.com/questions/25', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/23/']