Previous Inter-App Comm / The Orderly Shutdown Pattern Next

III. Java Inter-App Communication Example

A. The Server Code

   // Create a server socket to listen for connections server = new ServerSocket( PORT );

   // accept() blocks til client request, then creates new
   // socket on which to conduct client conversation. client = server.accept(); in  = client.getInputStream();
   byte[] inputStringBytes = new byte[ 256 ]; // Big enough?
   int inputLen = inputStringBytes );

   System.out.println( "\nServer received: " + 
      new String( inputStringBytes, 0, inputLen ) );

B. The Client Code

   // Connect to server on same machine, documented port client   = new Socket( "localhost", PORT ); in   = client.getInputStream(); out = client.getOutputStream();
   out.write( new String( "Hello, Server." ).getBytes() );

C. Server Output

Server received: Hello, Server

D. Example

Run “portcomm”

Previous Inter-App Comm / The Orderly Shutdown Pattern Next

Copyright 2018
iWay Technology Company
Boulder, Colorado USA
A limited right to copy this page for
individual (non-commercial)
educational use only
is hereby granted.