Using C$SOCKET

If desired, you can facilitate communication between Java and COBOL programs on a socket level. ACUCOBOL-GT includes a library routine, known as C$SOCKET, to perform interprocess communication.

When calling COBOL from Java:

  1. The COBOL programmer uses the C$SOCKET routine to create a server socket (op-code 1) and wait for and accept a connection from the Java client (op-code 2).
  2. The Java programmer creates a socket, connects via TCP/IP to the port of the COBOL program, and writes data to it.
  3. Via C$SOCKET, the COBOL program reads the data (op-code 6), processes it, and returns data to the socket (op-code 5).

Of course, because the data format is totally open and undefined, the COBOL and Java programmers must agree on a common format.

Following is sample code to demonstrate this capability:

*The following code creates a server socket. 
CALL "C$SOCKET" USING AGS-CREATE-SERVER, 8765
GIVING SOCKET-HANDLE-1. 
*The following code waits for a connection. 
CALL "C$SOCKET" USING AGS-NEXT-READ, SOCKET-HANDLE-1,
TIMEOUT.

*If have a connection request. Accept the connection. 
CALL "C$SOCKET" USING AGS-ACCEPT, SOCKET-HANDLE-1.

*Read data from the connecting socket. 
CALL "C$SOCKET" USING AGS_READ, SOCKET-HANDLE-2,
SOCKET-IN, IN-DATA-LENGTH
GIVING READ-AMOUNT.

*Write outgoing data back to the client socket: 
CALL "C$SOCKET" USING AGS-WRITE, SOCKET-HANDLE-2,
SOCKET-OUT, OUT-DATA-LENGTH.

See General Syntax and Library List for information on the C$SOCKET library routine.