views:

2217

answers:

11

I've been having issues getting the C sockets API to work properly in C++. Specifically, 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 could this be related to the fact that I'm doing this coding on z/OS and my problems are much more complicated? ;)

A: 

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

Chris Jester-Young
z/OS is ALWAYS the complicating factor. Truer words were never spoken :-)
Nighthawk
A: 

Could you post some code?

(Mandatory 'post some code' attitude comment)

CodingWithoutComments
A: 

Sure I can post a 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 a the following:

#ifdef __cplusplus
extern "C" {
#endif

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

Jax
+6  A: 

Oh dear. Upon further investigation, 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 a little

#define _OE_SOCKETS

action. Now, I personally have no idea what this OE SOCKETS thingy is, and 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?

Jax
OE == OpenEdition == z/OS UNIX System Services. Everything was "Open" back when they named the mainframe UNIX product, so it was named "MVS OpenEdition"
Nighthawk
A: 

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

Chris Jester-Young
A: 

DISCLAIMER: I am not a C++ programmer, however I know C really well. I adapated these calls from some C code I have.

Also markdown put these strange _ as my underscores.

You should just be able to write an abstraction class around the C sockets with something like this:

class my_sock {
    private int sock;
    private int socket_type;
    private socklen_t sock_len;
    private struct sockaddr_in server_addr;
    public char *server_ip;
    public unsigned short server_port;
};

Then have methods for opening, closing, and sending packets down the socket.

For example, the open call might look something like this:

int my_socket_connect()
{
    int return_code = 0;

    if ( this->socket_type != CLIENT_SOCK ) {
        cout << "This is a not a client socket!\n";
        return -1;
    }

    return_code = connect( this->local_sock, (struct sockaddr *) &this->server_addr, sizeof(this->server_addr));

    if( return_code < 0 ) {
        cout << "Connect() failure! %s\n", strerror(errno);
        return return_code;
    }

    return return_code;
}
David Bryson
This has nothing to do with the the original question.
Nighthawk
+1  A: 

You may want to take a look to cpp-sockets, 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.

Federico
+1  A: 

So try

#define _OE_SOCKETS

before you include sys/socket.h

fizzer
+1  A: 

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

Fabio Ceconello
z/OS has as much in common with z/VM as Windows does with Linux, so I'm a little bemused why you posted that link.
paxdiablo
Notice that the _OE_SOCKETS macro appears in both, and seems to have the same purpose. Which is not surprising, since probably IBM used the same code base for socket support in both products. I did not intend to say that z/VM documentation applies to z/OS, it's just the most similar case I found.
Fabio Ceconello
I think it's just a coincidence. z/VM doesn't use the z/OS Language Environment product, which provides the relevant header files used to make socket calls.
Nighthawk
+2  A: 

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.

Robert
Your link went dead.
Nighthawk
Try this one.http://www-01.ibm.com/software/awdtools/czos/support/You'll have to search for "Programming Guide".
Robert
A: 

Keep a copy of the IBM manuals handy:

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

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);
Nighthawk