Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

What does socket () do in C?

The socket() function shall create an unbound socket in a communications domain, and return a file descriptor that can be used in later function calls that operate on sockets. The socket() function takes the following arguments: domain. Specifies the communications domain in which a socket is to be created.

Which API is used in socket programming?

Textbook descriptions of socket calls are usually given in C, and most socket programmers are familiar with the C interface to TCP/IP. For these reasons, TCP/IP Services includes a C language API.

Can socket programming be done in C?

Socket Programming in C/C++ What is socket programming? Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while the other socket reaches out to the other to form a connection.

What is socket type in C?

Type C (electrical socket/electrical plug) The Type C plug (also called the Europlug) has two round pins. The pins are 4 to 4.8 mm wide with centers that are spaced 19 mm apart; the plug fits any socket that conforms to these dimensions.


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.


So try

#define _OE_SOCKETS

before you include sys/socket.h


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.

Take a look at this page:
Compiling and Linking a z/VM C Sockets Program


@Jax: The extern "C" 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 #include with it:

extern "C" {
#include <sys/socket.h>
// include other similarly non-compliant header files
}

Basically, anytime where a C++ program wants to link to C-based facilities, the extern "C" is vital. In practical terms, it means that the names used in external references will not be mangled, like normal C++ names would. Reference.