How to use the C socket API in C++ on z/OS

I'm having issues getting the C sockets API to work properly in C++ on z/OS.

Although I am including sys/socket.h, I still get compile time errors telling me that AF_INET is not defined.

Am I missing something obvious, or is this related to the fact that being on z/OS makes my problems much more complicated?

I discovered that there is an #ifdef that I'm hitting. Apparently z/OS isn't happy unless I define which "type" of sockets I'm using with:

#define _OE_SOCKETS

Now, I personally have no idea what this _OE_SOCKETS 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?

Test App

#include <sys/socket.h>

int main()
{
    return AF_INET;
}

Compile/Link Output:

cxx -Wc,xplink -Wl,xplink -o inet_test inet.C

"./inet.C", line 5.16: CCN5274 (S) The name lookup for "AF_INET" did not find a declaration.
CCN0797(I) Compilation failed for file ./inet.C. Object file not created.

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 #ifdef statements.

I have however noticed it contains the following:

#ifdef __cplusplus
  extern "C" {
#endif

which encapsulates basically the whole file? Not sure if it matters.


Keep a copy of the IBM manuals handy:

  • z/OS V1R11.0 XL C/C++ Programming Guide
  • z/OS V1R11.0 XL C/C++ Run-Time Library Reference

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"

You should ask your friendly system programmer to install the XL C/C++ Run-Time Library Reference: Man Pages 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:

FORMAT

X/Open

#define _XOPEN_SOURCE_EXTENDED 1
#include <sys/socket.h>

int connect(int socket, const struct sockaddr *address, socklen_t address_len);

Berkeley Sockets

#define _OE_SOCKETS
#include <sys/types.h>
#include <sys/socket.h>

int connect(int socket, struct sockaddr *address, int address_len);

I've had no trouble using the BSD sockets API in C++, in GNU/Linux. Here's the sample program I used:

#include <sys/socket.h>

int
main()
{
    return AF_INET;
}

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


See the Using z/OS UNIX System Services sockets section in the z/OS XL C/C++ Programming Guide. Make sure you're including the necessary header files and using the appropriate #defines.

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 Support & Downloads section on ibm.com and searching the documentation by title.