套接字和 COBOL

发布于 2024-08-28 18:50:57 字数 530 浏览 9 评论 0原文

我在一家医院找到了一份工作,该医院仍然使用 COBOL 进行所有组织工作,整个数据库(现在 20 TB)(这是一个自制软件,猜猜看,COBOL)充满了自过去 45 年以来每位患者的数据(或大约)年。

这就是我的故事。现在我的问题是:

目前,所有套接字(据我所知)都是由 COBOL 程序将数据写入文件来实现的。然后,这些文件由 C++ 程序(这是 20 世纪 80 年代末添加的附加模块)读取,并使用发送到数据库的 C++ 套接字。

现在这个解决方案已经停止工作,因为他们正在将数据库从 COBOL 移动到 COBOL,是的 - 他们没有使用 MySQL 等 - 他们实现了一个新的数据库 - 再次在 COBOL 中。我问在我之前在那里工作的那个人(他现在大约 70 岁)为什么有人会这样做,他告诉我他非常擅长 COBOL,以至于他不想用任何其他语言编写它。

到目前为止,我的问题是:

  • 如何在 COBOL 中实现套接字连接?我需要创建一个指向外部 COBOL 数据库的接口,例如,192.168.1.23:283

I have received a job at a hospital which still uses COBOL for all organizational work, the whole (now 20 Terabyte) database (Which was a homebrew in, guess what, COBOL) is filled with the data of every patient since the last 45 (or so) years.

So that was my story. Now to my question:

Currently, all sockets were (from what I've seen) implemented by COBOL programs writing their data into files. These files then were read out by C++ programs (That was an additional module added in the late 1980s) and using C++ sockets sent to the database.

Now this solution has stopped working as they are moving the database from COBOL to COBOL, yes - they didn't use MySQL or so - they implemented a new database - again in COBOL. I asked the guy that worked there before me (hes around 70 now) why the hell someone would do that and he told me that he is so good at COBOL that he doesn't want to write it in any other language.

So far so good now my question:

  • How can I implement socket connections in COBOL? I need to create an interface to the external COBOL database located at, for example, 192.168.1.23:283.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

您需要提供有关操作系统和编译器的更多信息。

如果您使用的是具有支持语言环境的编译器的 IBM z/OS,则只需从 z/OS 通信服务调用 EZASOCK 函数即可。这些调用在其参考文献中有详细记录,并且有很好的 Cobol 示例。

其他平台会有其他选择。

在大多数情况下,您可以“调用”用您需要的任何语言编写的外部模块,但可以是 DLL 或共享库或其他任何语言。

您能提供有关您的环境的更多详细信息吗?

为什么不直接从 Cobol 程序写入数据库呢?

You need to give more information about your OS and compiler.

If you are on IBM z/OS with a Language Environment supported compiler, you can just call the EZASOCK functions from the z/OS communications services. The calls are well documented in their references and have good Cobol examples.

Other platforms will have other options.

In most cases, you can just "CALL" an external module written in whatever language you need, but that a DLL or a sharedLib or whatever.

Can you give some more detail about your environment?

Why don't you just write directly to the database from the Cobol program?

奢欲 2024-09-04 18:50:57

IBM 大型机有两个可用于 COBOL 的套接字 API。
一种用于 CICS 程序(其中有特殊的线程安全和环境注意事项),另一种用于普通 Batch 或 IMS 程序。

完整的 TCP/IP 功能已实现,并且足够可靠,可以按照 MVA 标准处理信用卡协议(我知道,因为我已经做到了)。

IBM mainframes has two sockets APIs that can be used form COBOL.
One for use inside a CICS programs (where there are special thread safety and envrinment considerations) and one for use in ordinary Batch or IMS programs.

The complete TCP/IP functionality is implemented and its reliable enough to handle Credit Card protocols to MVA standards ( I know 'cos Ive done it).

北城孤痞 2024-09-04 18:50:57

大多数 COBOL 编译器将允许您链接和调用对象模块或 DLL。正如卡蒂所说,我知道我可以提供帮助,但需要更多信息。我之前已经从 Windows 到 DEC 完成过此操作,所以我知道它可以完成。

Most COBOL compiler will allow you to link and call in an object module or DLL. As Kati says I know I can help but need the additional information. I've done this previously from windows to DEC so i know it can be done.

人疚 2024-09-04 18:50:57

请记住,Google 是您的朋友。

答案在很大程度上取决于您的执行环境。

IBM 确实声称拥有 套接字 API 可从 COBOL 调用,作为 z/OS 的 CICS 的一部分。

Micro Focus 似乎有一些东西。

Recall that Google is your FRIEND.

The answer will depend heavily on your execution environment.

IBM does claim to have a Sockets API callable from COBOL, as part of CICS for z/OS.

Micro Focus appears to have something.

青瓷清茶倾城歌 2024-09-04 18:50:57

COBOL 不支持套接字(从技术上讲,某些 COBOL 环境具有各种非标准 COBOL 套接字库)。

相反,您必须用 C 编写任何功能,然后从 Cobol 调用它。

如果您使用的是 Linux 系统,例如 GNU Cobol 3,则说明如下:

COBOL-C-Python TCP 通信指南

该项目演示了使用 COBOL 和 C 组合实现的简单 TCP 服务器。 COBOL 主程序将端口号传递给 C 函数 (start_server),该函数设置侦听所提供端口的基本 TCP 服务器。一个单独的 Python 脚本充当客户端,将消息发送到该服务器。

Ubuntu 22.04 安装说明

先决条件

  1. 安装 COBOL 编译器
    OpenCOBOL(也称为 GnuCOBOL)是一个免费的 COBOL 编译器。安装:

    sudo apt update
    sudo apt 安装 gnucobol3
    
  2. 安装 GCC
    GCC 是一种流行的 C 编译器,我们将使用它来编译项目的 C 部分。

    sudo apt install build-essential
    
  3. 安装Python
    您还需要使用 Python 作为项目的客户端部分。安装 Python 3 和 pip:

    sudo apt install python3 python3-pip
    

项目文件

  1. COBOL 文件 (TCPServer.cob)

     识别部门。
        程序 ID。 TCP 服务器。
        数据部门。
        工作存储部分。
        01 服务器端口 PIC 9(5) COMP 值 8080。
    
        程序部门。
            DISPLAY “即将调用 C 函数。”
            使用服务器端口调用“start_server”。
            停止运行。
    
  2. C 文件 (server.c)

    #include ;
    #include 
    #include ;
    #include ;
    #include ;
    
    无效start_server(int * port_ptr){
        无符号 char* 字节 = (无符号 char*)port_ptr;
        printf("字节数:%02x %02x %02x %02x\n", 字节[0], 字节[1], 字节[2], 字节[3]);
        const int 端口 = (字节[0] << 24) | (字节[1] << 16)| (字节[2] << 8) |字节[3];
        printf("正在端口 %d\n 上运行 TCP 服务器", port);
        fflush(标准输出);
        // ... [其余代码]
    }
    
  3. Python 客户端 (client.py)

    导入套接字
    
    def send_message_to_server(主机、端口、消息):
        与socket.socket(socket.AF_INET,socket.SOCK_STREAM)作为s:
            s.connect((主机,端口))
            s.sendall(消息.encode())
    
    如果 __name__ == "__main__":
        主机=“127.0.0.1”
        端口=8080
        MESSAGE =“来自 Python 的问候!”
        发送消息到服务器(主机、端口、消息)
    
  4. 编译脚本 (compile.sh):

    gcc -c server.c
    cobc -x TCPServer.cob 服务器.o -lc
    

步骤:

  1. 创建项目文件
    使用 nano 等文本编辑器在目录 ~/cobol_tcp 中创建上面详细介绍的每个文件。

  2. 编译代码
    首先,使编译脚本可执行,然后运行它。

    chmod +xcompile.sh
    ./编译.sh
    
  3. 运行 TCP 服务器

    ./TCPServer
    
  4. 运行 Python 客户端
    在单独的终端中,导航到包含 client.py 的目录并运行:

    python3 client.py
    

如果一切设置正确,服务器(COBOL+C 代码)将显示从 Python 客户端收到的消息。

工作原理

  1. COBOL 程序初始化端口值 (8080) 并调用传递此端口的 C 函数 (start_server)。
  2. C 函数 (start_server) 设置 TCP 服务器并等待传入​​连接。
  3. Python 脚本连接到服务器并发送消息。
  4. C 函数接收消息,显示它,然后程序结束。

就是这样!该项目展示了 COBOL、C 和 Python 在基本客户端-服务器通信设置中的互操作性。

COBOL doesn't support sockets (technically, some COBOL environments have various non-standard COBOL socket libraries).

Instead, you have to write any functionality in C, and then call it from Cobol.

If you're using a Linux system, such as with GNU Cobol 3, then instructions are as follows:

COBOL-C-Python TCP Communication Guide

This project demonstrates a simple TCP server implemented using a combination of COBOL and C. The COBOL main program passes a port number to a C function (start_server) that sets up a basic TCP server listening on the provided port. A separate Python script acts as a client to send a message to this server.

Setup Instructions for Ubuntu 22.04

Prerequisites

  1. Install COBOL compiler:
    OpenCOBOL (also known as GnuCOBOL) is a free COBOL compiler. To install:

    sudo apt update
    sudo apt install gnucobol3
    
  2. Install GCC:
    GCC is a popular C compiler that we'll use to compile the C portion of our project.

    sudo apt install build-essential
    
  3. Install Python:
    You'll also need Python for the client part of the project. Install Python 3 and pip:

    sudo apt install python3 python3-pip
    

Project Files

  1. COBOL File (TCPServer.cob):

        IDENTIFICATION DIVISION.
        PROGRAM-ID. TCPServer.
        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01 SERVER-PORT            PIC 9(5) COMP VALUE 8080.
    
        PROCEDURE DIVISION.
            DISPLAY "About to call the C function."
            CALL "start_server" USING SERVER-PORT.
            STOP RUN.
    
  2. C File (server.c):

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    
    void start_server(int* port_ptr) {
        unsigned char* bytes = (unsigned char*)port_ptr;
        printf("Bytes: %02x %02x %02x %02x\n", bytes[0], bytes[1], bytes[2], bytes[3]);
        const int port = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
        printf("Running TCP server on port %d\n", port);
        fflush(stdout);
        // ... [rest of your code]
    }
    
  3. Python Client (client.py):

    import socket
    
    def send_message_to_server(host, port, message):
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.connect((host, port))
            s.sendall(message.encode())
    
    if __name__ == "__main__":
        HOST = "127.0.0.1"
        PORT = 8080
        MESSAGE = "Hello from Python!"
        send_message_to_server(HOST, PORT, MESSAGE)
    
  4. Compile Script (compile.sh):

    gcc -c server.c
    cobc -x TCPServer.cob server.o -lc
    

Steps:

  1. Create the project files:
    Use a text editor like nano to create each of the files detailed above in the directory ~/cobol_tcp.

  2. Compile the code:
    First, make the compile script executable and then run it.

    chmod +x compile.sh
    ./compile.sh
    
  3. Run the TCP Server:

    ./TCPServer
    
  4. Run the Python Client:
    In a separate terminal, navigate to the directory containing client.py and run:

    python3 client.py
    

If everything is set up correctly, the server (COBOL+C code) will display a received message from the Python client.

How It Works

  1. The COBOL program initializes a port value (8080) and calls the C function (start_server) passing this port.
  2. The C function (start_server) sets up a TCP server and waits for incoming connections.
  3. The Python script connects to the server and sends a message.
  4. The C function receives the message, displays it, and the program ends.

And that's it! This project showcases the interoperability of COBOL, C, and Python in a basic client-server communication setup.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文