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:
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.
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:
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.
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.
Compile the sending program
To compile the send.java
program, use this command:
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 |