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

  1. /*
  2.  * @(#)DatagramSocket.java    1.13 96/04/02 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. import java.io.FileDescriptor;
  23. import java.io.IOException;
  24. import java.io.InterruptedIOException;
  25.  
  26. /**
  27.  * The datagram socket class implements unreliable datagrams.
  28.  * @author Pavani Diwanji
  29. */
  30. public
  31. class DatagramSocket {
  32.     private int localPort;
  33.     private FileDescriptor fd;
  34.  
  35.     /**
  36.      * Load net library into runtime.
  37.      */
  38.     static {
  39.     System.loadLibrary("net");
  40.     }
  41.  
  42.     /**
  43.      * Creates a datagram socket
  44.      */
  45.     public DatagramSocket() throws SocketException {
  46.         fd = new FileDescriptor();
  47.     // creates a udp socket
  48.     datagramSocketCreate();
  49.     // binds the udp socket to any local available port
  50.     localPort = datagramSocketBind(0);
  51.     }
  52.  
  53.     /**
  54.      * Creates a datagram socket
  55.      * @param local port to use
  56.      */
  57.     public DatagramSocket(int port) throws SocketException {
  58.     SecurityManager security = System.getSecurityManager();
  59.     if (security != null) {
  60.         security.checkListen(port);
  61.     }
  62.         fd = new FileDescriptor();
  63.     // creates a udp socket
  64.     datagramSocketCreate();
  65.     // binds the udp socket to desired port
  66.     localPort = datagramSocketBind(port);
  67.     }
  68.  
  69.     /**
  70.      * Sends Datagram Packet to the destination address
  71.      * @param DatagramPacket to be sent. The packet contains the buffer 
  72.      * of bytes, length and destination InetAddress and port.
  73.      * @exception IOException i/o error occurred
  74.      */
  75.     public void send(DatagramPacket p) throws IOException  {
  76.  
  77.     // check the address is ok wiht the security manager on every send.
  78.     SecurityManager security = System.getSecurityManager();
  79.     if (security != null) {
  80.         security.checkConnect(p.getAddress().getHostAddress(), p.getPort());
  81.     }
  82.  
  83.     // call the native method to send
  84.     datagramSocketSend(p);
  85.     }
  86.  
  87.     /**
  88.      * Receives datagram packet.
  89.      * @param DatagramPacket to be received.
  90.      * On return, the DatagramPacket contains the buffer in which the 
  91.      * data is received, packet length, sender's address and sender's port 
  92.      * number. Blocks until some input is available.
  93.      * @exception IOException i/o error occurred
  94.      */
  95.     
  96.     public synchronized void receive(DatagramPacket p) throws IOException {
  97.  
  98.       // check the address is ok with the security manager before every recv.
  99.       // If there is no security mgr, just accept.
  100.       
  101.       SecurityManager security = System.getSecurityManager();
  102.       if (security != null) {
  103.     // peek at the packet to see who it is from.
  104.     InetAddress peekAddress = new InetAddress();
  105.     int peekPort = datagramSocketPeek(peekAddress);
  106.     security.checkConnect(peekAddress.getHostAddress(), peekPort);
  107.       }
  108.     
  109.       // If the security check succeeds, then receive the packet.
  110.       datagramSocketReceive(p);
  111.       
  112.       return;      
  113.     }
  114.  
  115.     /**
  116.      *    Returns the local port that this socket is bound to.
  117.      */
  118.     public int getLocalPort() {
  119.     return localPort;
  120.     }
  121.  
  122.     /**
  123.      * Close the datagram socket.
  124.      */
  125.     public synchronized void close() {
  126.     datagramSocketClose();
  127.     }
  128.  
  129.     protected synchronized void finalize() {
  130.     datagramSocketClose();
  131.     }
  132.  
  133.     private native void datagramSocketCreate();
  134.     private native int  datagramSocketBind(int port);
  135.     private native void datagramSocketSend(DatagramPacket p);
  136.     private native int datagramSocketPeek(InetAddress i);
  137.     private native void datagramSocketReceive(DatagramPacket p);
  138.     private native void datagramSocketClose();
  139. }
  140.