kati
kati

Reputation: 49

Sockets and COBOL

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:

Upvotes: 4

Views: 6550

Answers (5)

Ronen
Ronen

Reputation: 311

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.

Upvotes: 0

Joe Zitzelberger
Joe Zitzelberger

Reputation: 4263

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?

Upvotes: 5

James Anderson
James Anderson

Reputation: 27478

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

Upvotes: 1

John R. Strohm
John R. Strohm

Reputation: 7667

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.

Upvotes: 0

Steve
Steve

Reputation: 1

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.

Upvotes: 0

Related Questions