A Program to Read a Message


The next program, receive.c, reads and prints out the contents of the message being published from the send.c program described in the previous section.

Step 7

Copy the receive.c program

Copy the program receive.c into your tutorial directory. Under Windows also copy the associated makefile, recvw32m.mak, into your tutorial directory. Here is an example of the receive.c program:

    /* receive.c - read and print an INFO message */ 
    /* $RTHOME/examples/smrtsock/tutorial/lesson1/receive.c */ 
 
1   #include <rtworks/ipc.h> 
 
2   int main(int argc, char **argv) 
    {   
3     T_IPC_MSG msg; 
4     T_STR msg_text; 
  
5     TipcSrvSubjectSetSubscribe("/tutorial/lesson1", TRUE); 
 
6     msg = TipcSrvMsgNext(T_TIMEOUT_FOREVER); 
7     TipcMsgSetCurrent(msg, 0); 
8     TipcMsgNextStr(msg, &msg_text); 
9     TutOut("Text from message = %s\n", msg_text); 
 
10    TipcSrvDestroy(T_IPC_SRV_CONN_NONE); 
 
      } /* main */ 

As with the send.c program, notice how the program consists of so few lines of code. Compare this with a similar program written using pipes, sockets or shared memory. Not only are TIBCO SmartSockets programs much shorter, they also have many built-in features (such as automatic data conversion across heterogeneous platforms). Let’s examine this program on a line-by-line basis to understand in detail how the program works:

Line 1
The include file ipc.h is required.
Line 3
Declares a TIBCO SmartSockets message (this is eventually used to hold the incoming message).
Line 4
Declares a string into which you eventually transfers the data part of the message.
Line 5
The call to TipcSrvSubjectSetSubscribe performs two tasks:
  • Establishes a connection to RTserver (this might include auto-starting RTserver), because there was no explicit call to connect to RTserver.
  • Informs RTserver to forward any message that has been published to the /tutorial/lesson1 subject by any client in the project. The second parameter (TRUE) specifies that the program wishes to start subscribing to the subject.
Line 6
The call to TipcSrvMsgNext retrieves the next message in the program’s message queue from RTserver. The T_TIMEOUT_FOREVER constant specifies that the function blocks (waits) forever for a message. You can specify an actual time-out period for this argument; for example, 10.0 would mean the function returns a NULL value after 10 seconds if no message has become available.
Line 7
You now have a message to work with. The call to TipcMsgSetCurrent sets the message pointer (the current field) to the first field in the data part of the message.
Line 8
The call to TipcMsgNextStr gets the next string field from the data part of the message (in this case, it is the first and only field) and places it in msg_text. In this example it contains the string "Hello World!".
Line 9
This line calls the TIBCO SmartSockets utility function TutOut to output the contents of msg_text.
Line 10
The last line of the program gracefully disconnects from RTserver with a call to TipcSrvDestroy.

Step 8

Compile and link the program

Now you must compile and link the receiving program in a similar manner as before:

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

Step 9

Build the receiving program

Under Windows, to compile and link the receiving program within Developer Studio, select File > Open Workspace..., select filename recvw32m.mak from your tutorial directory, and then select Build > Build receive.

All further examples in this Tutorial will assume that you are familiar with compiling and linking programs as described above.


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