Write Double Using Sockets : Socket « Network Protocol « Java






Write Double Using Sockets

    
// 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);
    }
  }
}

   
    
    
    
  








Related examples in the same category

1.Create a socket without a timeout
2.Create a socket with a timeout
3.Demonstrate Sockets.
4.Socket connection and concurrent package
5.XML based message
6.ObjectInputStream and ObjectOutputStream from Socket
7.ServerSocket and Socket for Serializable object
8.String based communication between Socket
9.Get email with Socket
10.Create PrintWriter from BufferedWriter, OutputStreamWriter and Socket
11.Read from server
12.Use Socket to read and write stream
13.Connects to a server at a specified host and port. It reads text from the console and sends it to the server
14.A simple network client that establishes a network connection to a specified port on a specified host, send an optional message across the connection
15.Reading Text from a Socket
16.Writing Text to a Socket
17.Sending a POST Request Using a Socket
18.Get the Date from server
19.Transfer a file via Socket
20.Ping a server
21.Read and write through socket
22.Read float number from a Socket
23.Read Object from Socket
24.deleting messages from a POP3 mailbox based on message size and Subject line
25.A timeout feature on socket connections
26.Write Objects From Socket
27.Download WWW Page
28.Redirects incoming TCP connections to other hosts/ports
29.Socket Fetcher
30.Socket Address Encoder
31.Zip socket
32.This program shows how to interrupt a socket channel.
33.This program shows how to use sockets to send plain text mail messages.
34.Makes a socket connection to the atomic clock in Boulder, Colorado, and prints the time that the server sends.