// ShowSavingAClassNet - Client side that writes some objects to a stream // // Copyright (C) 1996 by Rinaldo Di Giorgio . All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions // are met: // 1. Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF // SUCH DAMAGE. import java.applet.Applet; import java.util.*; import java.io.*; import java.net.*; /** * Write some objects over the network to a remote server that reads the * stream and procceds to save the objects to disk. */ public class ShowSavingAClassNet extends Applet { DataOutputStream os = null; ObjectOutput s = null; int PortNumber = 2000; String ServerName = "traveler"; public void initMain(String s, int p) { ServerName = s; PortNumber = p; init(); } public void init() { Date d = new Date(); // Get the portnumber and servername and allow to be called from a // java main program also try { String param; param = getParameter("PortNumber"); if (param != null) PortNumber = Integer.parseInt(param); param = getParameter("ServerName"); if (param != null) ServerName = param; } catch ( NullPointerException e) { // Must be called from a main ignore } try { Socket skt = new Socket(ServerName,PortNumber); os = new DataOutputStream(skt.getOutputStream() ); } catch(Exception e) { System.out.println(e); System.exit(0); } try { s = new ObjectOutputStream(os); } catch ( Exception e ) { System.out.println(e); System.out.println("Unable to open stream as an object stream"); } try { s.writeObject("Today"); s.writeObject(d); os.close(); System.out.println("Object written"); } catch ( Exception e ) { System.out.println(e); System.out.println("Unable to write Objects"); } } //////////////////////////////////////////////////////////////////////////// // Example of an applet and a main sharing code and be parameter // driven. //////////////////////////////////////////////////////////////////////////// public static void main(String args[]) { ShowSavingAClassNet showSavingAClassNet = new ShowSavingAClassNet(); if (args.length < 2) { System.out.println("Usage: ServerName PortNumber"); System.exit(0); } else { String ServerName = new String(args[0]); // Notice local scope of ServerName and PortNumber int PortNumber = Integer.parseInt(args[1]); showSavingAClassNet.initMain(ServerName, PortNumber); } } }