Using C$SOCKET

You can also use the C$SOCKET library routine to facilitate interprocess communication between Java and COBOL programs via sockets. C$SOCKET is a low-level option, but it is very flexible.

When calling Java from COBOL:

  1. The Java programmer creates a server socket and waits for and accepts a connection to the COBOL client.
  2. The COBOL programmer uses the C$SOCKET routine to create a client socket (op-code 1), connect via TCP/IP to the port of the Java program, and write data to the socket (op-code 5).
  3. The Java program reads the data, processes it, returns data to the socket.
  4. The COBOL program uses C$SOCKET to read the data (op-code 6).

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

The following sample code demonstrates this capability:

*Create a Client Socket. 
CALL "C$SOCKET" USING AGS-CREATE-CLIENT, 8765, SERVER-NAME
GIVING SOCKET-HANDLE. 

*Write data to socket. 
CALL "C$SOCKET" USING AGS-WRITE, SOCKET-HANDLE,
DATA-FROM-CLIENT, DATA-LENGTH. 

*Read the return data from the socket. 
CALL "C$SOCKET" USING AGS-READ, SOCKET-HANDLE,
DATA-FROM-CLIENT, DATA-LENGTH. 

*Close the socket. 
CALL "C$SOCKET" USING AGS-CLOSE, SOCKET-HANDLE.

See C$SOCKET for complete information on the C$SOCKET library routine.