A "Hello World!" Program


In this section, you see the complete C source code for your first program.

Step 3

Copy the send.c program

Copy the send.c program into your working directory. Under Windows you must also copy the makefile sendw32m.mak into your tutorial directory. Under Windows you can work with an editor in a Command Prompt window or use the Visual C++ Developer Studio to edit and build your tutorial programs. Use the command chmod 777 * to allow write access to any sample programs you copy.

Line numbers appear on the far left margins of code examples. They are not part of the program but are used throughout this tutorial to refer back to different lines of source code. There is no error handling shown in the examples in this tutorial. Error handling is briefly explained later in this lesson. Here is an example of the file send.c program:

    /* send.c - send an INFO message to /lesson1 subject */ 
    /* $RTHOME/examples/smrtsock/tutorial/lesson1/send.c */ 
 
1   #include <rtworks/ipc.h> 
 
2   int main(int argc, char **argv) 
    { 
3      TipcSrvMsgWrite("/tutorial/lesson1", 
                       TipcMtLookupByNum(T_MT_INFO), TRUE,  
                        _IPC_FT_STR, "Hello World!", NULL); 
4       TipcSrvFlush(); 
5       TipcSrvDestroy(T_IPC_SRV_CONN_NONE); 
     } /* main */ 

You are probably finding it hard to believe that a complete TIBCO SmartSockets program can be only a few lines of code. This is one of the main benefits of TIBCO SmartSockets; hundreds of lines of interprocess communication code, such as sockets or RPCs, can be reduced to just a few lines of TIBCO SmartSockets code.

The first thing you should notice about your sending program is that the header file, ipc.h, is required to compile and link TIBCO SmartSockets programs. It must be part of any file that refers to the TIBCO SmartSockets API. For more information on TIBCO SmartSockets include files see the TIBCO SmartSockets Application Programming Interface reference.

There are only four calls to the TIBCO SmartSockets API in this program: TipcSrvMsgWrite, TipcMtLookupByNum, TipcSrvFlush, and TipcSrvDestroy. From the API naming conventions, you can infer that the called functions TipcSrvMsgWrite, TipcSrvFlush, and TipcSrvDestroy are all used to communicate with an RTserver, because they start with the prefix TipcSrv. TipcMtLookupByNum is used to get information about a message type, consistent with its TipcMt prefix.

This message publishing program consists of only three lines of code:

Line 3
A call to TipcSrvMsgWrite, performs a variety of functions, including:
  • The process first establishes a connection to RTserver (this might include auto-starting RTserver if it is not already running), because there was no explicit call to connect to RTserver. You will learn more about RTserver in the next lesson.
  • A message of type INFO is created. The second argument to TipcSrvMsgWrite is the message type. Here you call TipcMtLookupByNum to get a pointer to the INFO message type. See the messages chapter in the TIBCO SmartSockets User’s Guide for more details on message types.
  • The data part of the new message is constructed to contain the text "Hello World!".
  • The message is published to the subject /tutorial/lesson1. Any process that has subscribed to /tutorial/lesson1 receives the message.
  • The connection to RTserver is then terminated.
The TIBCO SmartSockets API function TipcSrvMsgWrite is referred to as a convenience function. Calling it results in a series of tasks being completed. There is a set of API functions that can be called to do the same thing as TipcSrvMsgWrite, giving you more granularity of control. These API functions are described in later lessons of this tutorial.
Line 4
Flushes the buffered message out of the program to be delivered to RTserver. The call to TipcSrvFlush ensures the message is sent immediately, rather than relying on the built-in message buffering mechanisms of TIBCO SmartSockets. TIBCO SmartSockets provides you with very flexible message buffering. Message buffering is used to hold messages that are not ready to be processed yet (or are not ready to be sent).
Line 5
Disconnects from RTserver before the program exits.

Compiling and Linking

Step 4

Compile and link the program

Now the program must be compiled and linked. This is accomplished using the TIBCO SmartSockets shell script rtlink under UNIX or OpenVMS, and a TIBCO SmartSockets-supplied makefile invoked from either the command line or Developer Studio under Windows, as shown:

UNIX:
$ rtlink send.c -o send.x 
OpenVMS:
$ cc send.c 
$ rtlink /exec=send send.obj 
Windows:
$ nmake /f sendw32m.mak 

Step 5

Set paths for the include files and libraries

Under Windows, if you are using TIBCO SmartSockets for the first time, you need to set the paths for your include files and libraries so Developer Studio will find the TIBCO SmartSockets include files and libraries. You’ll also need to set the Project settings within Developer Studio. See the TIBCO SmartSockets Installation Guide for Windows for details on this.

Step 6

Build your program

Once you’ve set the include, library and project settings, to compile and link the program within Developer Studio, select File > Open Workspace..., select filename sendw32m.mak from your tutorial directory, and then select Build > Build send.

The rtlink shell script compiles and links a program with the TIBCO SmartSockets libraries. This example compiles the program send.c, links it, and creates the executable send.x (on OpenVMS and Windows: send). Compiling and linking can be done in separate steps, and there are many options available. You can also define your own make file and choose your own compiler.

Once compiled, the program is ready to run. Before running it, you need to create a second program, receive.c, to read and print the message that is being sent.


TIBCO SmartSockets™ Tutorial
Software Release 6.8, July 2006
Copyright © TIBCO Software Inc. All rights reserved
www.tibco.com