home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / DatagramPacket.java < prev    next >
Text File  |  1996-05-03  |  2KB  |  83 lines

  1. /*
  2.  * @(#)DatagramPacket.java    1.8 96/02/14 Pavani Diwanji
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.net;
  21.  
  22. /**
  23.  * A class that represents a datagram packet containing packet data, packet
  24.  * length, internet addresses and port.
  25.  * @author     Pavani Diwanji
  26.  */
  27. public final 
  28. class DatagramPacket {
  29.     private byte[] buf;
  30.     private int length;
  31.     private InetAddress address;
  32.     private int port;
  33.  
  34.     /**
  35.      * This constructor is used to create a DatagramPacket object used 
  36.      * for receiving datagrams.
  37.      * @param ibuf is where packet data is to be received.
  38.      * @param ilength is the number of bytes to be received.
  39.      */
  40.     public DatagramPacket(byte ibuf[], int ilength) {
  41.     if (ilength > ibuf.length) {
  42.         throw new IllegalArgumentException("illegal length");
  43.     }
  44.     buf = ibuf;
  45.     length = ilength;
  46.     address = null;
  47.     port = -1;
  48.     }
  49.     
  50.     /**
  51.      * This constructor is used construct the DatagramPacket to be sent.
  52.      * @param ibuf contains the packet data.
  53.      * @param ilength contains the packet length
  54.      * @param iaddr and iport contains destination ip addr and port number.
  55.      */
  56.     public DatagramPacket(byte ibuf[], int ilength,
  57.               InetAddress iaddr, int iport) {
  58.     if (ilength > ibuf.length) {
  59.         throw new IllegalArgumentException("illegal length");
  60.     }
  61.     buf = ibuf;
  62.     length = ilength;
  63.     address = iaddr;
  64.     port = iport;
  65.     }
  66.     
  67.     public InetAddress getAddress() {
  68.     return address;
  69.     }
  70.     public int getPort() {
  71.     return port;
  72.     }
  73.     public byte[] getData() {
  74.     return buf;
  75.     }
  76.     public int getLength() {
  77.     return length;
  78.     }
  79. }
  80.  
  81.  
  82.  
  83.