Reputation: 49
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:
192.168.1.23:283
.Upvotes: 4
Views: 6550
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:
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.
Install COBOL compiler: OpenCOBOL (also known as GnuCOBOL) is a free COBOL compiler. To install:
sudo apt update
sudo apt install gnucobol3
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
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
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.
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]
}
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)
Compile Script (compile.sh):
gcc -c server.c
cobc -x TCPServer.cob server.o -lc
Create the project files:
Use a text editor like nano
to create each of the files detailed above in the directory ~/cobol_tcp
.
Compile the code: First, make the compile script executable and then run it.
chmod +x compile.sh
./compile.sh
Run the TCP Server:
./TCPServer
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.
8080
) and calls the C function (start_server
) passing this port.start_server
) sets up a TCP server and waits for incoming connections.And that's it! This project showcases the interoperability of COBOL, C, and Python in a basic client-server communication setup.
Upvotes: 0
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
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
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
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