Previous | Inter-App Comm / The Orderly Shutdown Pattern | Next |
A. The Server Code
// Create a server socket to listen for connections
java.net.ServerSocket server = new ServerSocket( PORT );
// accept() blocks til client request, then creates new
// socket on which to conduct client conversation.
java.net.Socket client = server.accept();
java.io.InputStream in = client.getInputStream();
byte[] inputStringBytes = new byte[ 256 ]; // Big enough?
int inputLen = in.read( inputStringBytes );
System.out.println( "\nServer received: " +
new String( inputStringBytes, 0, inputLen ) );
B. The Client Code
// Connect to server on same machine, documented port
java.net.Socket client = new Socket( "localhost", PORT );
java.io.InputStream in = client.getInputStream();
java.io.OutputStream out = client.getOutputStream();
out.write( new String( "Hello, Server." ).getBytes() );
out.flush();
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 jt@iwaytechnology.com |
A
limited right to copy this page for individual (non-commercial) educational use only is hereby granted. |