/* * 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 1. This identifies a service provided as a default by our company when * you install the PSPSOCK framework on your ISeries. This service simply echoes * the data sent by this - or any other - client program back to the client program. * 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 12345 (unless it has * been reconfigured on ISeries to listen on a different port). */ import java.io.*; import java.net.*; public class PSPS00001C { /* Default static constructor with parameters. */ public static void main(String[] args) { new PSPS00001C(args); } public PSPS00001C(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 12345. Run the PSPSOCK command to // see the configuration for this service. int port = 12345; Socket s = null; PrintWriter out = null; BufferedReader in = null; char output[] = new char[7]; 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) the actual characters. For this // demo, send 3 characters "ABC". try { output[0] = 0X00; output[1] = 0X00; output[2] = 0X00; output[3] = 0X03; // 3 bytes follow this byte output[4] = 'A'; // Hexadecimal 41 in ASCII output[5] = 'B'; // Hexadecimal 42 in ASCII output[6] = 'C'; // Hexadecimal 43 in ASCII out.write(output,0,7); 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(); 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))); } } }