/* * Demonstration Java program for the PSPSOCK ISeries Sockets development * framework. Web site: http://www.prosperosoftware.co.uk * * This is a sockets client program that communicates with a sockets-based * service running on an IBM ISeries computer. In this case, the service number * is 2. This identifies a service provided as a default by our company when * you install the PSPSOCK framework on your ISeries. This service providees a * currency description lookup. For example if you give it the ISO currency * code 'USD', it will return the description 'United States Dollar'. * You will need to refer to documentation on our Web site and in the PSPSOCK * library you installed on your ISeries to understand what this client Java * program should do. That service is listening on port 12346 (unless it has * been reconfigured on ISeries to listen on a different port). */ import java.io.*; import java.net.*; public class PSPS00002C { /* Default static constructor with parameters. */ public static void main(String[] args) { new PSPS00002C(args); } public PSPS00002C(String[] args) { // This demonstration client runs on a PC attached to the ISeries. That ISeries // in our case has IP address below. That address can be pinged from the PC. // You will have to change the address to match your own ISeries. String host = "192.168.20.20"; // The ISeries service is listening on port 12346. Run the PSPSOCK command to // see the configuration for this service. int port = 12346; Socket s = null; PrintWriter out = null; BufferedReader in = null; char output[] = new char[8]; String read = null; int readLen = 0; // Step 1: Connect to the server. try { s = new Socket(host,port); } catch (Exception e) { System.err.println("Socket() failed with " + e.getMessage()); System.exit(1); } // If we get this far than the client Java program has successfully connected // with the ISeries service. If client read/write operations below fail, then // errors may be logged by the service on the ISeries. Run the PSPSOCK command // to see logged error messages for the service. try { out = new PrintWriter(s.getOutputStream(), true); } catch (Exception e) { System.err.println("PrintWriter() failed with " + e.getMessage()); System.exit(1); } try { in = new BufferedReader(new InputStreamReader(s.getInputStream())); } catch (Exception e) { System.err.println("BufferedReader() failed with " + e.getMessage()); System.exit(1); } // Step 2: Write data to the server. // The protocol for this service requires: // 1) a 4 byte integer value indicating the number of characters that follow on. // 2) a single byte indicating whether this client program is working with // ASCII or EBCDIC data. // 3) A 3 character ISO currency code. In this demo, 'USD' try { output[0] = 0X00; output[1] = 0X00; output[2] = 0X00; output[3] = 0X04; // 4 bytes follow this byte output[4] = 0X01; // Hex 0 means EBCDIC, hex 1 means ASCII output[5] = 'U'; // Hexadecimal 55 in ASCII output[6] = 'S'; // Hexadecimal 53 in ASCII output[7] = 'D'; // Hexadecimal 44 in ASCII out.write(output,0,8); out.flush(); } catch (Exception e) { System.err.println("write() failed with " + e.getMessage()); System.exit(1); } String write = new String(output); int writeLen = write.length(); System.out.println("Sent to server = " + writeLen + " characters:"); auditBytes(write,writeLen); // Step 3: Read data response back from the server. try { read = in.readLine(); } catch (Exception e) { System.err.println("readLine() failed with " + e.getMessage()); System.exit(1); } readLen = read.length(); if (readLen > 7) { System.out.println("Currency description is: " + read.substring(7)); } System.out.println("Returned from server = " + readLen + " characters:"); auditBytes(read,readLen); // Step 4: Close all open connections. try { out.close(); in.close(); s.close(); } catch (Exception e) { System.err.println("close() failed with " + e.getMessage()); System.exit(1); } } void auditBytes(String dta, int dtaLen) { for (int x = 0; x < dtaLen; x++) { System.out.println("Byte " + x + " = " + dta.charAt(x) + " hex " + Integer.toHexString(dta.charAt(x))); } } }