Server Sample
The server initializes itself, accepts a conversation, receives data, and prints diagnostic information. As in the client, we instantiate classes to hold the CPI-C parameters, many of which have only an integer as instance data. By using objects, we can mimic call by reference. We also allocate a byte array to hold the received data.
Note:
The following sample includes code interleaved with
commentary.
/*---------------------------------------------------------------
* Pipeline transaction, server side.
*---------------------------------------------------------------*/
import COM.ibm.eNetwork.cpic.*;
import Java.io.IOException;
public class PipeServer extends Object {
public static void main(String args[]) {
CPIC cpic_obj = new CPIC();
// Space to hold the received data
byte[] data_buffer;
data_buffer = new byte[101];
CPICLength requested_length = new CPICLength(101);
CPICDataReceivedType data_received =
new CPICDataReceivedType(0);
CPICLength received_length = new CPICLength(0);
CPICStatusReceived status_received =
new CPICStatusReceived(0);
CPICControlInformationReceived rts_received =
new CPICControlInformationReceived(0);
CPICReturnCode cpic_return_code =
new CPICReturnCode(0);
// Space to hold a conversation ID -- a bunch of bytes
// The first line declares conversation_ID to be a reference to
// a byte array object. The second line creates such an object,
// and assigns the reference to the byte array object.
byte[] conversation_ID;
conversation_ID = new byte[cpic_obj.CM_CID_SIZE];
The CPI-C receive call (cmrcv) returns a Java™ array of bytes while the pipe transaction expects a string. The programmer can translate the array of bytes into a string by using the string class-constructor that takes an array of bytes as an argument.
//
// ACCEPT
//
cpic_obj.cmaccp( /* Accept_Conversation */
conversation_ID, /* O: returned conversation ID */
cpic_return_code); /* O: return code */
//
// RECEIVE
//
cpic_obj.cmrcv( /* Receive */
conversation_ID, /* I: conversation ID */
data_buffer, /* I: where to put received data */
requested_length, /* I: maximum length to receive */
data_received, /* O: data complete or not? */
received_length, /* O: length of received data */
status_received, /* O: has status changed? */
rts_received, /* O: was RTS received? */
cpic_return_code); /* O: return code from this call */
//
// Do some return code processing
//
System.out.println(" Data from Receive:");
System.out.println(" cpic_return_code = " +
cpic_return_code.intValue());
System.out.println(" cpic_data_received = " +
data_received.intValue());
System.out.println(" cpic_received_length = " +
received_length.intValue());
System.out.println(" cpic_rts_received = " +
rts_received.intValue());
System.out.println(" cpic_status_received = " +
status_received.intValue());
// Create a Java String from the array of bytes that you received
// and print it out.
String receivedString = new String(data_buffer,0);
System.out.println(
" Recevied string = "
+ receivedString );
//
// BLOCK so that the Server Window doesn't disappear
//
try{
System.out.println("Press any key to continue");
System.in.read();
}
catch
(IOException e){ e.printStackTrace(); }
}
}