home *** CD-ROM | disk | FTP | other *** search
/ Freesoft 1997 May / Freesoft_1997-05_cd.bin / recenz / PROGRAM / JAVADRAW / iavadraw301_inst.exe / data.z / QuoteClientApplet.java < prev    next >
Text File  |  1997-05-20  |  5KB  |  159 lines

  1. /*
  2.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.applet.Applet;
  18. import java.awt.*;
  19. import java.io.*;
  20. import java.net.*;
  21. import java.util.*;
  22.  
  23. public class QuoteClientApplet extends Applet {
  24.     boolean DEBUG = false;
  25.     InetAddress address;
  26.     TextField portField;
  27.     Label display;
  28.     DatagramSocket socket;
  29.  
  30.     public void init() {
  31.         //Initialize networking stuff.
  32.         String host = getCodeBase().getHost();
  33.  
  34.         try {
  35.             address = InetAddress.getByName(host);
  36.         } catch (UnknownHostException e) {
  37.             System.out.println("Couldn't get Internet address: Unknown host");
  38.             // What should we do?
  39.         }
  40.  
  41.         try {
  42.             socket = new DatagramSocket();
  43.         } catch (IOException e) {
  44.             System.out.println("Couldn't create new DatagramSocket");
  45.             return;
  46.         }
  47.  
  48.         //Set up the UI.
  49.         GridBagLayout gridBag = new GridBagLayout();
  50.         GridBagConstraints c = new GridBagConstraints();
  51.         setLayout(gridBag);
  52.  
  53.         Label l1 = new Label("Quote of the Moment:", Label.CENTER);
  54.         c.anchor = GridBagConstraints.SOUTH;
  55.         c.gridwidth = GridBagConstraints.REMAINDER;
  56.         gridBag.setConstraints(l1, c); 
  57.         add(l1);
  58.  
  59.         display = new Label("(no quote received yet)", Label.CENTER);
  60.         c.anchor = GridBagConstraints.NORTH;
  61.         c.weightx = 1.0;
  62.         c.fill = GridBagConstraints.HORIZONTAL;
  63.         gridBag.setConstraints(display, c); 
  64.         add(display);
  65.  
  66.         Label l2 = new Label("Enter the port (on host " + host
  67.                              + ") to send the request to:", 
  68.                              Label.RIGHT);
  69.         c.anchor = GridBagConstraints.SOUTH;
  70.         c.gridwidth = 1;
  71.         c.weightx = 0.0;
  72.         c.weighty = 1.0;
  73.         c.fill = GridBagConstraints.NONE;
  74.         gridBag.setConstraints(l2, c); 
  75.         add(l2);
  76.  
  77.         portField = new TextField(6);
  78.         gridBag.setConstraints(portField, c); 
  79.         add(portField);
  80.  
  81.         Button button = new Button("Send");
  82.         gridBag.setConstraints(button, c); 
  83.         add(button);
  84.  
  85.         validate();
  86.     }
  87.  
  88.     public Insets insets() {
  89.         return new Insets(4,4,5,5);
  90.     }
  91.  
  92.     public void paint(Graphics g) {
  93.         Dimension d = size();
  94.         Color bg = getBackground();
  95.  
  96.         g.setColor(bg);
  97.         g.draw3DRect(0, 0, d.width - 1, d.height - 1, true);
  98.         g.draw3DRect(3, 3, d.width - 7, d.height - 7, false);
  99.     }
  100.  
  101.     void doIt(int port) {
  102.         DatagramPacket packet;
  103.         byte[] sendBuf = new byte[256];
  104.         
  105.         packet = new DatagramPacket(sendBuf, 256, address, port);
  106.  
  107.         try { // send request
  108.             if (DEBUG) {
  109.                 System.out.println("Applet about to send packet to address "
  110.                                + address + " at port " + port);
  111.             }
  112.             socket.send(packet);
  113.             if (DEBUG) {
  114.                 System.out.println("Applet sent packet.");
  115.             }
  116.         } catch (IOException e) {
  117.             System.out.println("Applet socket.send failed:");
  118.             e.printStackTrace();
  119.             return;
  120.         }
  121.  
  122.         packet = new DatagramPacket(sendBuf, 256);
  123.  
  124.         try { // get response
  125.             if (DEBUG) {
  126.                 System.out.println("Applet about to call socket.receive().");
  127.             }
  128.             socket.receive(packet);
  129.             if (DEBUG) {
  130.                 System.out.println("Applet returned from socket.receive().");
  131.             }
  132.         } catch (IOException e) {
  133.             System.out.println("Applet socket.receive failed:");
  134.             e.printStackTrace();
  135.             return;
  136.         }
  137.  
  138.         String received = new String(packet.getData(), 0);
  139.         if (DEBUG) {
  140.             System.out.println("Quote of the Moment: " + received);
  141.         }
  142.         display.setText(received);
  143.     }
  144.  
  145.     public boolean action(Event event, Object arg) {
  146.         int port;
  147.         
  148.         try {
  149.             port = Integer.parseInt(portField.getText());
  150.         } catch (NumberFormatException e) {
  151.             //No integer entered.  Should warn the user.
  152.             return true;
  153.         }
  154.  
  155.         doIt(port);
  156.         return true;
  157.     }
  158. }
  159.