java 9
java 9
net
InetAddress Class
• No public constructor
• Three static methods:
– InetAddress getByName(String)
• Static method used to retrieve the address for the host name
passed as the parameter.
– InetAddress [ ] getAllByName(String)
• Static method used to retrieve all the addresses for the host
name passed as a parameter.
– InetAddress getLocalHost( )
• Static method used to retrieve the address for the current, or
local, host.
InetAddress Class
try
{ InetAddress fullname = netAddress.getByName(“bigyellowcat.cs.binghamton.edu");
InetAddress alias = InetAddress.getByName(“bigyellowcat");
InetAddress octets = InetAddress.getByName(“128.226.121.44");
if (fullname.equals(alias) && fullname.equals(octets))
// All is right with the world! }
catch (UnknownHostException e)
{ // Exception handling here. }
TCP Sockets
• Constructors
– DatagramSocket()
• Constructs a datagram socket and binds it to any available port on the local host.
– DatagramSocket(DatagramSocketImpl impl)
• Creates an unbound datagram socket with the specified DatagramSocketImpl.
– DatagramSocket(int port)
• Constructs a datagram socket and binds it to the specified port on the local host.
– DatagramSocket(int port, InetAddress iaddr)
• Creates a datagram socket, bound to the specified local address.
– DatagramSocket(SocketAddress bindaddr)
• Creates a datagram socket, bound to the specified local socket address.
DatagramSocket Class – operational Methods
Setter Methods:
void setBroadcast( boolean on)
static void setDatagramSocketImplFactory (DatagramSocketImplFactory fac)
void serReceiveBufferSize(int size)
void setReuseAddress(boolean on)
void setSevdBufferSize(int size)
void setSoTimeout(int timeout)
void setTrafficClass(int tc)
DatagramSocket Class – test methods
Test Methods:
boolean isBound( )
boolean isClosed( )
boolean isConnected( )
Datagram Sockets Examples
• RFC 2396
• essentially a “pointer” to a resource on the World
Wide Web
– different services use slightly different formats
• file://ftp.yoyodyne.com/pub/files/foobar.txt
• http://www.yahoo.com/index.html
• ftp://[email protected]
• news:rec.gardening
• gopher://gopher.banzai.edu:1234/
URL Class - Constructors
• URL(String spec)
– Creates a URL object from the String representation.
• URL(String protocol, String host, int port, String file)
– Creates a URL object from the specified protocol, host, port number, and file.
• URL(String protocol, String host, int port, String file, URLStreamHandler handler)
– Creates a URL object from the specified protocol, host, port number, file, and handler.
• URL(String protocol, String host, String file)
– Creates a URL from the specified protocol name, host name, and file name.
Setters
set(String protocol, String host, int port, String file, String ref)
set(String protocol, String host, int port, String authority, String userInfo, String path, String query,
String ref)
setURLStreamHandlerFactory(URLStreamHandlerFactory fac)
URL Class – Utility methods
int hashCode( )
URLConnection openConnection( )
InputStream openStream( )
boolean sameFile(URL other)
String toExternalForm( )
String toString( )
boolean equals(Object obj)
URL Class - example