A Hello World! Program


In this section, the complete source code for your first SmartSockets Java program is presented. Be sure SmartSockets and the SmartSockets Java Class Library are installed properly on your system.

The files for this lesson are located in the directories:

Windows:
%RTHOME%\java\tutorial\lesson1 
UNIX:
$RTHOME/java/tutorial/lesson1 

Step 1

Create a working directory

Before you begin writing your first program, create a working directory where you have read and write access to store the examples.

Step 2

Copy the tutorial files

Copy the tutorial files from the lesson1 directory into your working directory.

Line numbers appear on the far left margins of code examples. Note that these numbers are not part of the program but are used to refer to different lines in the source code. This is the send.java program:

//------------------------------------------------------------ 
// Program 1: send.java 
 
1 import java.io.*; 
2 import com.smartsockets.*; 
 
3 public class send { 
 
4   public static void main(String[] argv) { 
5     TipcSrv srv = TipcSvc.getSrv(); 
6     TipcMsg msg = TipcSvc.createMsg(TipcMt.INFO); 
7     msg.setDest("/ss/tutorial/lesson1"); 
8     msg.appendStr("Hello World!"); 
 
      try { 
9       srv.send(msg); 
10      srv.flush(); 
11      srv.destroy(); 
12    } catch (TipcException te) { 
13      Tut.warning(te); 
      } // catch 
    } // main 
  } // send 

It might be difficult to believe that a complete SmartSockets program can be contained in so few lines; this is one of the main benefits of SmartSockets. Hundreds of lines of interprocess communication code (such as sockets or RPCs) can be reduced to just a few lines of SmartSockets Java code.

Let’s take a look at the key lines of this program:

Line 2
The SmartSockets Java package, com.smartsockets, is imported. This step is required.
Line 5
A reference to the RTserver object is placed in the srv object. Using TipcSvc.getSrv() allows a single global RTserver connection to be created when needed.
Line 6
A TipcMsg object (msg) is created using the INFO message type.
Line 7
Sets the subject to which the message is being published. In this case the subject is /ss/tutorial/lesson1.
Line 8
The text message "Hello, World!" is appended as the first data field in the msg message object.
Line 9
The message is sent to RTserver using the send method. Note that the send method automatically created a connection to RTserver, which must already be running.
Line 10
The connection to RTserver is flushed, ensuring the message is sent immediately.
Line 11
The connection to RTserver is closed. It is a good practice that all programs sending data to RTserver destroy their connection when it is no longer needed.

The SmartSockets methods referred to in this program are:

From the API naming conventions, you can see that the methods TipcSrv.send and TipcSrv.flush are used to communicate with RTserver, because they are part of the TipcSrv class.

Compiling

Once the program has been written, it must be compiled with an appropriate Java compiler for your platform. The examples in this manual are compiled with Sun Microsystems Java Development Kit.

Step 3

Compile the sending program

To compile the send.java program, use this command:

$ javac send.java 

Once compiled, the sending program is ready to run. Before running it, however, you need to create a second program, receive.java, to read and print the message that you are going to send using the send program.


TIBCO SmartSockets™ Java Library User’s Guide and Tutorial
Software Release 6.8, July 2006
Copyright © TIBCO Software Inc. All rights reserved
www.tibco.com