Java tutorial
// WriteDoubleUsingSockets - Client side that writes some objects to a stream // // Copyright (C) 1996 by Rinaldo Di Giorgio <rinaldo@digiorgio.com>. 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.awt.Label; import java.io.DataOutputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.Date; /** * Write some objects over the network to a remote server that reads the stream * and procceds to save the objects to disk. */ public class WriteDoubleUsingSockets extends Applet { DataOutputStream os = null; ObjectOutput s = null; int PortNumber = 2000; String ServerName = "traveler"; /** * Set the server and port name */ 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) { Label l = new Label(e.toString()); add(l); System.out.println(e); return; } try { s = new ObjectOutputStream(os); } catch (Exception e) { System.out.println(e); System.out.println("Unable to open stream as an object stream"); } try { long startTime = System.currentTimeMillis(); for (int i = 0; i < 25; i++) { s.writeObject(new Double(i)); } os.close(); long endTime = System.currentTimeMillis() - startTime; Label l = new Label( "Writing 25 doubles server took: " + new Long(endTime).toString() + " milliseconds."); add(l); 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[]) { WriteDoubleUsingSockets showSavingAClassNet = new WriteDoubleUsingSockets(); 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); } } }