Connecting with C$SOCKET

If desired, you can facilitate communication between C and COBOL programs at a socket level. ACUCOBOL-GT includes a C$SOCKET library routine, which supports socket-level interprocess communication.

When communicating with sockets between COBOL and C, or any other language, you must:

  1. Determine which side is the client and which is the server.
  2. Open a listening socket from the server process.
  3. Open a connecting socket from the client process.

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

The following sample code demonstrates 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 you 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 C$SOCKET for more information on the C$SOCKET library routine.