Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * Conditions Of Use
  *
  * This software was developed by employees of the National Institute of
  * Standards and Technology (NIST), an agency of the Federal Government.
  * Pursuant to title 15 Untied States Code Section 105, works of NIST
  * employees are not subject to copyright protection in the United States
  * and are considered to be in the public domain.  As a result, a formal
  * license is not needed to use the software.
 *
 * This software is provided by NIST as a service and is expressly
 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
 * AND DATA ACCURACY.  NIST does not warrant or make any representations
 * regarding the use of the software or the results thereof, including but
 * not limited to the correctness, accuracy, reliability or usefulness of
 * the software.
 *
 * Permission to use this software is contingent upon your acceptance
 * of the terms of this agreement
 *
 * .
 *
 */
 package gov.nist.core.net;
 
 
 /* Added by Daniel J. Martinez Manzano <dani@dif.um.es> */
default implementation which passes straight through to java platform

Author(s):
m.andrews
Version:
1.2
Since:
1.1
 
 public class DefaultNetworkLayer implements NetworkLayer {
 
 
single default network layer; for flexibility, it may be better not to make it a singleton, but singleton seems to make sense currently.
 
     public static final DefaultNetworkLayer SINGLETON = new DefaultNetworkLayer();
 
     private DefaultNetworkLayer() {
          = (SSLServerSocketFactory) SSLServerSocketFactory
                 .getDefault();
          = (SSLSocketFactory) SSLSocketFactory.getDefault();
     }
 
     public ServerSocket createServerSocket(int portint backlog,
             InetAddress bindAddressthrows IOException {
         return new ServerSocket(portbacklogbindAddress);
     }
 
     public Socket createSocket(InetAddress addressint port)
             throws IOException {
         return new Socket(addressport);
     }
 
     public DatagramSocket createDatagramSocket() throws SocketException {
         return new DatagramSocket();
     }
 
     public DatagramSocket createDatagramSocket(int portInetAddress laddr)
             throws SocketException {
 
         if ( laddr.isMulticastAddress() ) {
             try {
                 MulticastSocket ds = new MulticastSocketport );
                 ds.joinGroupladdr );
                 return ds;
             } catch (IOException e) {
                 throw new SocketExceptione.getLocalizedMessage() );
             }
         } else return new DatagramSocket(portladdr);
     }
 
     /* Added by Daniel J. Martinez Manzano <dani@dif.um.es> */
     public SSLServerSocket createSSLServerSocket(int portint backlog,
             InetAddress bindAddressthrows IOException {
                portbacklogbindAddress);
    }
    /* Added by Daniel J. Martinez Manzano <dani@dif.um.es> */
    public SSLSocket createSSLSocket(InetAddress addressint port)
            throws IOException {
        return (SSLSocket.createSocket(addressport);
    }
    /* Added by Daniel J. Martinez Manzano <dani@dif.um.es> */
    public SSLSocket createSSLSocket(InetAddress addressint port,
            InetAddress myAddressthrows IOException {
        return (SSLSocket.createSocket(addressport,
                myAddress, 0);
    }
    public Socket createSocket(InetAddress addressint port,
            InetAddress myAddressthrows IOException {
        if (myAddress != null)
            return new Socket(addressportmyAddress, 0);
        else
            return new Socket(addressport);
    }

    
Creates a new Socket, binds it to myAddress:myPort and connects it to address:port.

Parameters:
address the InetAddress that we'd like to connect to.
port the port that we'd like to connect to
myAddress the address that we are supposed to bind on or null for the "any" address.
myPort the port that we are supposed to bind on or 0 for a random one.
Returns:
a new Socket, bound on myAddress:myPort and connected to address:port.
Throws:
java.io.IOException if binding or connecting the socket fail for a reason (exception relayed from the correspoonding Socket methods)
    public Socket createSocket(InetAddress addressint port,
                    InetAddress myAddressint myPort)
        throws IOException
    {
        if (myAddress != null)
            return new Socket(addressportmyAddressmyPort);
        else if (port != 0)
        {
            //myAddress is null (i.e. any)  but we have a port number
            Socket sock = new Socket();
            sock.bind(new InetSocketAddress(port));
            sock.connect(new InetSocketAddress(addressport));
            return sock;
        }
        else
            return new Socket(addressport);
    }
New to GrepCode? Check out our FAQ X