01: /*
02: * @(#)SerializationTestClient.java 1.9 06/10/10
03: *
04: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
05: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License version
09: * 2 only, as published by the Free Software Foundation.
10: *
11: * This program is distributed in the hope that it will be useful, but
12: * WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * General Public License version 2 for more details (a copy is
15: * included at /legal/license.txt).
16: *
17: * You should have received a copy of the GNU General Public License
18: * version 2 along with this work; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA
21: *
22: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
23: * Clara, CA 95054 or visit www.sun.com if you need additional
24: * information or have any questions.
25: *
26: */
27: package foundation;
28:
29: import java.net.Socket;
30: import java.io.OutputStream;
31: import java.io.ObjectOutputStream;
32: import foundation.SerializationTestObject;
33:
34: /** Connects to a SerializationTestServer and sends two objects (a
35: String array and a custom object) to it to be printed. */
36:
37: public class SerializationTestClient {
38: public static void usage() {
39: System.out
40: .println("java SerializationTestClient [server name]");
41: }
42:
43: public static void main(String[] args) {
44: try {
45: if (args.length != 1) {
46: usage();
47: return;
48: }
49: Socket sock = new Socket(args[0], 13913);
50: OutputStream outStr = sock.getOutputStream();
51: ObjectOutputStream objOutStr = new ObjectOutputStream(
52: outStr);
53: String[] strs = new String[3];
54: strs[0] = "Hello,";
55: strs[1] = "serialized";
56: strs[2] = "world!";
57: System.out.println("Writing string array...");
58: objOutStr.writeObject(strs);
59: SerializationTestObject obj = new SerializationTestObject();
60: obj.booleanField = true;
61: obj.byteField = 4;
62: obj.charField = 'L';
63: obj.shortField = 17;
64: obj.intField = 39;
65: obj.floatField = 5.0f;
66: obj.longField = 8000000000001L; // 2^33 + 1
67: obj.doubleField = 7.0f;
68: System.out.println("Writing SerializationTestObject...");
69: objOutStr.writeObject(obj);
70: System.out.println("Flushing ObjectOutputStream...");
71: objOutStr.flush();
72: System.out.println("Closing socket...");
73: sock.close();
74: System.out
75: .println("SerializationTestClient exiting successfully.");
76: } catch (Exception e) {
77: e.printStackTrace();
78: System.out
79: .println("ERROR: SerializationTestClient exiting unsuccessfully.");
80: }
81: }
82: }
|