This section will demonstrate how to access an ADR card connected to a serial port on a remote computer. Communication is via TCP/IP sockets. The example uses ANSI C code running in a Microsoft Windows environment. Both the client and server ends of the link are shown. This example uses a slightly modified version of the serial com port code that is shown on the programming page for Visual C++ with MFC.
I am a Windows programmer at one of Ontrak's clients. After I wrote the web page showing how we access an ADR card using Visual C++, I was requested to write a page about TCP/IP. This code works with all current ADR cards and commands.
This example is based on code that I use whenever I am trying out TCP/IP in a new environment. Adding support for the ADR card was simple, especially since the code does not include rigorous error handling.
The sample code was tested on Windows NT, 95 and 98. I make no effort to explain all of the parameters used in the various API calls. Please refer to the documentation provided with your compiler.
Note that the description of Internet components has been greatly simplified for readability. Never-the-less readers cognizant of networking concepts may find it ponderous; please bear with me as I try to guide the neophytes through this topic. Feel free to skip ahead.
This section explains what the sample programs are trying to achieve.
A user on a remote client computer types in a command for an ADR card. The command is transmitted across the network to a server computer. The server computer passes the command to a locally attached ADR card. The response from the ADR card is sent back across the network for display to the user of the remote client computer.
Click on the items in the picture for more details.
The server program is called ADRSockSrv.exe. The server program runs on the computer that has the ADR card attached.
ADRSockSrv looks up the port number for ADRlink in the Services file, creates a socket and listens for client connections. When a client connection is accepted ADRSockSrv receives the command that was sent by the client. The command is passed to the ADR card by writing it to the serial port. The response is read from the serial port and sent back to the client. The server then waits for the next client connection.
The client program is cleverly called ADRSockClient.exe. The client program runs on a remote computer that is connected to the server computer by an Internetwork.
ADRSockClient looks in the Hosts file for the IP address of the server. ADRSockClient looks in the Services file for the port number of the ADRlink service. A socket-address structure is loaded with the server IP address and ADRlink port number. A socket is created and bound to the server's socket-address. ADRSockClient connects to the server using the socket. The response parameter and the command parameter that the user typed are concatenated together to become a message. The message is sent to the server. ADRSockClient then receives the response and displays it.
The Services file provides the port numbers for services. It contains entries for "well-known" services. We add our service, ADRlink, to this file as port number 20025/tcp. Both the client and server computers must have the same port number in the Services file on each machine. ADRSockSrv will listen for clients that try to connect to the server computer on port number 20025.
The Hosts file provides IP addresses for computer names. It allows us to refer to a computer by its name instead of its IP address. We add the name of our server computer to this file along with its IP address. (Huh? what's my IP address)
Only the Hosts file on the client computer needs to change. The server computer returns the response over the connection that the client created.
Even though this will seem like a verbose description, believe me, it is only the nickel tour about using the TCP/IP protocol.
I will explain the server example program first.
Now we can look at the client example program.
If you have difficulties with the ADR Sockets example you may find some assistance on the trouble shooting page.
You can skip the brief discussion of a few advanced topics. The page merely expands on items that have already been introduced.
I hope that this assists you in your programming efforts.
John Homppi ![]()
To retrieve the source code used in this example in ZIP format, click ADRSocket Demo.