In this module, we will explore how to use Control Language (CL) to handle network communication. This includes understanding the basics of network protocols, sending and receiving data over a network, and integrating network communication into your CL programs.

Objectives

By the end of this module, you will:

  • Understand the basics of network communication.
  • Learn how to send and receive data over a network using CL.
  • Integrate network communication into your CL programs.

  1. Basics of Network Communication

What is Network Communication?

Network communication involves the exchange of data between devices over a network. This can include local area networks (LANs), wide area networks (WANs), and the internet.

Key Concepts

  • Protocols: Rules that define how data is transmitted and received over a network (e.g., TCP/IP, HTTP, FTP).
  • Sockets: Endpoints for sending and receiving data.
  • Ports: Logical channels through which data is sent and received.

  1. Sending Data Over a Network

Example: Sending Data Using TCP/IP

To send data over a network using TCP/IP in CL, you can use the SNDSRVTBLE command. This command sends data to a specified IP address and port.

/* Example: Sending data to a server */
DCL VAR(&DATA) TYPE(*CHAR) LEN(100)
DCL VAR(&IPADDR) TYPE(*CHAR) LEN(15) VALUE('192.168.1.100')
DCL VAR(&PORT) TYPE(*DEC) LEN(5 0) VALUE(8080)

CHGVAR VAR(&DATA) VALUE('Hello, this is a test message.')

SNDSRVTBLE DATA(&DATA) IPADDR(&IPADDR) PORT(&PORT)

Explanation

  • DCL VAR(&DATA) TYPE(*CHAR) LEN(100): Declares a variable to hold the data to be sent.
  • DCL VAR(&IPADDR) TYPE(*CHAR) LEN(15) VALUE('192.168.1.100'): Declares a variable for the IP address of the server.
  • DCL VAR(&PORT) TYPE(*DEC) LEN(5 0) VALUE(8080): Declares a variable for the port number.
  • CHGVAR VAR(&DATA) VALUE('Hello, this is a test message.'): Assigns a value to the data variable.
  • SNDSRVTBLE DATA(&DATA) IPADDR(&IPADDR) PORT(&PORT): Sends the data to the specified IP address and port.

  1. Receiving Data Over a Network

Example: Receiving Data Using TCP/IP

To receive data over a network using TCP/IP in CL, you can use the RCVSRVTBLE command. This command listens for incoming data on a specified port.

/* Example: Receiving data from a client */
DCL VAR(&DATA) TYPE(*CHAR) LEN(100)
DCL VAR(&PORT) TYPE(*DEC) LEN(5 0) VALUE(8080)

RCVSRVTBLE DATA(&DATA) PORT(&PORT)

SNDPGMMSG MSG(&DATA)

Explanation

  • DCL VAR(&DATA) TYPE(*CHAR) LEN(100): Declares a variable to hold the received data.
  • DCL VAR(&PORT) TYPE(*DEC) LEN(5 0) VALUE(8080): Declares a variable for the port number.
  • RCVSRVTBLE DATA(&DATA) PORT(&PORT): Listens for incoming data on the specified port and stores it in the data variable.
  • SNDPGMMSG MSG(&DATA): Sends a program message with the received data.

  1. Integrating Network Communication into CL Programs

Practical Example: Chat Application

Let's create a simple chat application where one CL program sends messages and another receives them.

Sender Program

/* Sender Program */
PGM
DCL VAR(&DATA) TYPE(*CHAR) LEN(100)
DCL VAR(&IPADDR) TYPE(*CHAR) LEN(15) VALUE('192.168.1.100')
DCL VAR(&PORT) TYPE(*DEC) LEN(5 0) VALUE(8080)

CHGVAR VAR(&DATA) VALUE('Hello, this is a chat message.')

SNDSRVTBLE DATA(&DATA) IPADDR(&IPADDR) PORT(&PORT)
ENDPGM

Receiver Program

/* Receiver Program */
PGM
DCL VAR(&DATA) TYPE(*CHAR) LEN(100)
DCL VAR(&PORT) TYPE(*DEC) LEN(5 0) VALUE(8080)

RCVSRVTBLE DATA(&DATA) PORT(&PORT)

SNDPGMMSG MSG(&DATA)
ENDPGM

Explanation

  • The sender program sends a chat message to the specified IP address and port.
  • The receiver program listens for incoming messages on the specified port and displays them.

Exercises

Exercise 1: Modify the Sender Program

Modify the sender program to allow the user to input the message to be sent.

Solution

/* Modified Sender Program */
PGM
DCL VAR(&DATA) TYPE(*CHAR) LEN(100)
DCL VAR(&IPADDR) TYPE(*CHAR) LEN(15) VALUE('192.168.1.100')
DCL VAR(&PORT) TYPE(*DEC) LEN(5 0) VALUE(8080)

SNDPGMMSG MSG('Enter your message:') TOPGMQ(*EXT) MSGTYPE(*INQ)
RCVMSG MSG(&DATA)

SNDSRVTBLE DATA(&DATA) IPADDR(&IPADDR) PORT(&PORT)
ENDPGM

Exercise 2: Implement Error Handling

Add error handling to the receiver program to handle cases where no data is received.

Solution

/* Receiver Program with Error Handling */
PGM
DCL VAR(&DATA) TYPE(*CHAR) LEN(100)
DCL VAR(&PORT) TYPE(*DEC) LEN(5 0) VALUE(8080)
DCL VAR(&MSGID) TYPE(*CHAR) LEN(7)

RCVSRVTBLE DATA(&DATA) PORT(&PORT) MSGID(&MSGID)

IF COND(&MSGID *NE ' ') THEN(DO)
    SNDPGMMSG MSG('No data received.') MSGTYPE(*DIAG)
    RETURN
ENDDO

SNDPGMMSG MSG(&DATA)
ENDPGM

Summary

In this module, we covered the basics of network communication in CL, including sending and receiving data over a network. We also integrated network communication into CL programs with practical examples and exercises. Understanding these concepts will enable you to create more dynamic and interactive CL programs that can communicate over a network.

© Copyright 2024. All rights reserved