z / OS의 C ++에서 C 소켓 API를 사용하는 방법
C 소켓 API가 C ++에서 올바르게 작동하는 데 문제가 z/OS
있습니다.
를 포함하고 있지만 sys/socket.h
여전히 AF_INET
정의되지 않은 컴파일 시간 오류가 발생합니다 .
내가 명백한 것을 놓치고 z/OS
있습니까 , 아니면 켜져있는 것이 문제를 훨씬 더 복잡하게 만든다는 사실과 관련이 있습니까?
업데이트 : 추가 조사 결과, 내가 #ifdef
치고 있는 것이 발견되었습니다 . z/OS
내가 사용하는 소켓의 "유형"을 정의하지 않으면 분명히 행복하지 않습니다.
#define _OE_SOCKETS
이제 개인적으로 이것이 _OE_SOCKETS
실제로 무엇인지 알지 못 하므로 z/OS
소켓 프로그래머가 3 명 모두 있다면,이 모든 것이 어떻게 작동하는지 나에게 줄 수 있습니까?
테스트 앱
#include <sys/socket.h>
int main()
{
return AF_INET;
}
컴파일 / 링크 출력 :
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.
sys / sockets.h 검사에는 필요한 정의가 포함되어 있으며 알 수있는 한 #ifdef 문에 의해 차단되지 않습니다.
그러나 다음 내용이 포함되어 있습니다.
#ifdef __cplusplus
extern "C" {
#endif
기본적으로 전체 파일을 캡슐화합니다. 중요한지 확실하지 않습니다.
IBM 매뉴얼 사본을 편리하게 유지하십시오.
IBM 서적은 일반적으로 매우 우수하지만, 답변을 찾을 수있는 곳을 알고 있어야하며 형식에 익숙해 져야합니다. 사용하려는 기능이 "기능 테스트 매크로"에 의해 보호되는 경우가 종종 있습니다.
친숙한 시스템 프로그래머에게 시스템에 XL C / C ++ Run-Time Library Reference : Man Pages 를 설치하도록 요청해야 합니다. 그런 다음 "man connect"와 같은 작업을 수행하여 socket connect () API에 대한 매뉴얼 페이지를 가져올 수 있습니다. 내가 그렇게하면, 이것이 내가 보는 것입니다 :
체재
X / 오픈
#define _XOPEN_SOURCE_EXTENDED 1
#include <sys/socket.h>
int connect(int socket, const struct sockaddr *address, socklen_t address_len);
버클리 소켓
#define _OE_SOCKETS
#include <sys/types.h>
#include <sys/socket.h>
int connect(int socket, struct sockaddr *address, int address_len);
GNU / Linux의 C ++에서 BSD 소켓 API를 사용하는 데 아무런 문제가 없었습니다. 내가 사용한 샘플 프로그램은 다음과 같습니다.
#include <sys/socket.h>
int
main()
{
return AF_INET;
}
따라서 z / OS는 아마도 여기에서 복잡한 요소 일 것입니다. 그러나 이전에는 z / OS를 사용해 본 적이 없으며 프로그래밍이 훨씬 적기 때문에 결정적으로 말할 수는 없습니다. :-피
참고 항목 사용 Z / OS UNIX 시스템 서비스 소켓 은 z / OS XL C / C ++ 프로그래밍 안내서의 섹션을 참조하십시오. 필요한 헤더 파일을 포함하고 적절한 #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
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.
@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.
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;
}
The answer is use the c89 flag that follows:
-D_OE_SOCKETS
Example follows;
bash-2.03$ c89 -D_OE_SOCKETS [filename].c
For more information look for C89 Options in the z/OS XLC/C++ User's Guide.
참고URL : https://stackoverflow.com/questions/25/how-to-use-the-c-socket-api-in-c-on-z-os
'Programming' 카테고리의 다른 글
Ruby : 범위를 반복하는 방법은 있지만 설정된 단위로 증가합니까? (0) | 2020.06.16 |
---|---|
쉘 스크립트에서 백틱 대신 $ ()를 사용하면 어떤 이점이 있습니까? (0) | 2020.06.16 |
G ++를 사용하여 여러 .cpp 및 .h 파일 컴파일 (0) | 2020.06.15 |
ssh : 호스트 'hostname'의 신뢰성을 설정할 수 없습니다 (0) | 2020.06.15 |
루비 보내기 대 __send__ (0) | 2020.06.15 |