Problem solve Get help with specific problems with your technologies, process and projects.

Explore the opportunities of sockets programming in RPG

Explore the opportunities of sockets programming in RPG

From the "Explore the opportunities of sockets programming in RPG" by Ron Turull, AS/400 Experts Journal, Vol. 3. No.3, May/June 2000. Provided courtesy of The 400 Group.

Sockets is the standard for implementing client/server applications over TCP/IP and, thus, over the Internet. It is also a great communications protocol for use in an intranet, or even doing simple client/server programming between an AS/400 and PCs. You can even use sockets to allow two programs on an AS/400 to talk to each other.

How sockets (singular) can benefit you.

Sockets is a very robust API and communications vehicle. Here are just a few of the advantages of sockets:

  • Sockets provides for either connection-oriented or connectionless communications. Connection-oriented communications is like a telephone conversation; both ends of the communication stay connected for the duration of the call. Connectionless communication is more like the mail, just not as slow.
  • A sockets server program can serve multiple clients at the same time using either connection-oriented or connectionless communication.
  • On the server side, you can configure a socket to handle a backlog of incoming requests. When the server program is ready, the system will give it the next request in the backlog. You can think of this as a hold mechanism in a typical phone system. When you call into a company, for example, if the receptionist is currently on another call, your call gets placed in the hold queue. When the receptionist is done with the current call, he or she can pick up the next call of the hold queue.

You can use sockets to communicate at a very high, relatively simple level or at a very low, fairly complex level (for example, the raw IP level of the TCP/IP stack).

The simple approach

Because it is so robust, sockets is an extensive and fairly complex topic. We're going to try to take the simple approach. Here's the plan. We will develop a sample client/server sockets application that will do the following:

  • Operate at a high level in the TCP/IP stack.
  • Use the easiest options possible (i.e., use the options that require the least amount of programming effort).
  • Handle a backlog of three. This by no means complicates things; you just specify the number as one of the parameters to an API -- an API you have to call anyway -- the machine does the rest.
  • Have the server program simply echo back what the client sends. This will help illustrate how data is sent back and forth.
  • Use a connection-oriented stream socket. This is the easiest to use. A stream socket allows for a simple stream of bytes to flow across the communication line.
  • Have both the client program and the server program be RPG programs running on the same machine.

Sockets programming prerequisites

Because sockets grew up in a C environment, its development was influenced by C. Pointers are mandatory. You should think twice about trying sockets programming if you have never used pointers before. You don't need to be a pointer guru, but you do need some basic experience and need to know exactly what a pointer is.

Knowing the ins and outs of prototyping procedures and calling prototyped procedures is also a requirement. The names of all the sockets APIs are in lowercase letters. The only way to use them is to create prototypes for them, specifying the real names as the external names.

The main think you need to be aware of is that all parameters in the C language are passed by value -- period. If you want a parameter passed by reference, you have to do it manually by passing a pointer to it. The pointer itself is, of course, passed by value. What this boils down to is this: You'll see a lot of pointers passed by value in the procedure prototypes in the #SOCKETS include file.

Dig Deeper on RPG iSeries programming