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

前端 未结 9 791
闹比i
闹比i 2021-02-01 11:23

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 m

9条回答
  •  再見小時候
    2021-02-01 12:07

    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;
    }
    

提交回复
热议问题