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
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
Project Files
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
Steps:
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.
How It Works
- The COBOL program initializes a port value (
8080
) and calls the C function (start_server
) passing this port.
- The C function (
start_server
) sets up a TCP server and waits for incoming connections.
- The Python script connects to the server and sends a message.
- 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.