Académique Documents
Professionnel Documents
Culture Documents
14.1 Class InetAddress 14.2 An InetAddress example 14.3 Class Socket 14.4 Getting Web pages with a socket 14.5 Building a PostOutputStream class 14.6 Wrapping up
Package java.net
Authenticator ContentHandler DatagramPacket DatagramSocket
InetAddress
ServerSocket
Socket
SocketImpl URL URLConnection URLEncoder URLStreamHandler MalformedURLException ProtocolException SocketException UnknownHostException UnknownServiceException ContentHandlerFactory SocketImplFactory URLStreamHandlerFactory
public final class InetAddress { public String getHostName() public byte[] getAddress() public String getHostAddress() public int hashCode() public boolean equals(Object obj) public String toString()
public static synchronized InetAddress getByName(String host) throws UnknownHostException public static synchronized InetAddress getAllByName(String host)[] throws UnknownHostException public static InetAddress getLocalHost() throws UnknownHostException }
Static Methods
public static InetAddress getLocalHost() throws UnknownHostException The getLocalHost() returns an InetAddress object representing the address of the local host.
public static synchronized InetAddress[] getAllByName(String host) throws UnknownHostException The getAllByName() method returns an array of InetAddress objects representing all of the addresses for the specified host.
Static Methods
The getByName() method returns an InetAddress object containing the internet address information for the specified hostname. The host is a string object for the specified host.
Instance Methods
public String getHostName() The getHostName() method returns a String object containing the name of the host for this InetAddress. If the host is null, the returned string will contain any of the local machine's available network addresses.
Instance Methods
public byte[] getAddress() The getAddress() method returns an array of bytes containing the raw IP address of this InetAddress in network byte order.
Instance Methods
--- JDK1.1
public String getHostAddress() The getHostAddress() method returns the IP address string containing the raw IP address using the standard format (%d.%d.%d.%d).
public boolean isMulticastAddress() The isMulticastAddress() method checks if the InetAddress is an IP multicast address and returns a boolean indicating if the InetAddress is an IP multicast address. An IP multicast address is a Class D address i.e the first four bits of the address are 1110 (224~239).
UnknownHostException
java.lang.Object | +--java.lang.Throwable | +--java.lang.Exception | +--java.io.IOException | +--java.net.UnknownHostException
SecurityException
java.lang.Object | +--java.lang.Throwable | +--java.lang.Exception | +--java.lang.RuntimeException | +--java.lang.SecurityException
An InetAddress Example
Class Socket
An implementation of a TCP/IP network connection. A client create a Socket to a remote host to establish a streamed-based communication channel.
Class Socket
Protected members of java.net.Socket.
protected Socket() Creates an unconnected socket, with the systemdefault type of SocketImp protected Socket(SocketImpl impl) Creates an unconnected Socket with a userspecified SocketImpl.
Class Socket
Listing of Public members of java.net.Socket. public final class Socket { public Socket(String host, int port) throws UnknownHostException, IOException public Socket(String host, int port, boolean stream) throws IOException public Socket(InetAddress address, int port) throws IOException public Socket(InetAddress address, int port, boolean stream) throws IOException public InetAddress getInetAddress() public int getPort() public int getLocalPort() public InputStream getInputStream() throws IOException public OutputStream getOutputStream() throws IOException public synchronized void close() throws IOException public String toString() public static synchronized void setSocketImplFactory(SocketImplFactory fac) throws IOException }
Constructors
This Socket() constructor creates a stream socket to the specified port on the specified host. The host is a String object containing the host name to create the socket on. The port is an integer (1-65535) value representing the port to create the socket on.
Constructors
public Socket(String host, int port, boolean stream) throws IOException --- Deprecated This Socket() constructor creates a stream socket to the specified port on the specified host. The boolean stream value can be used to specify a stream socket or a datagram socket. The stream is a boolean value that is true if a stream socket is to be created, false if a datagram socket is to be created.
Constructors
This Socket() constructor creates a stream socket to the specified port at the specified InetAddress. The address is an InetAddress specifying the address at which to create the socket.
Constructors
public Socket(InetAddress address, int port, boolean stream) throws IOException --- Deprecated This Socket() constructor creates a stream socket to the specified port at the specified address. The boolean stream value can be used to specify a stream (true) socket or a datagram (false) socket. The address is an InetAddress specifying the address at which to create the socket.
Constructors JDK1.1
public Socket(InetAddress address, int port, InetAddress localAddr, int localPort) throws IOException Good for multihomed machine- a machine with multiple network interface.
This Socket() constructor creates a socket and connects it to the specified remote address on the specified remote port. The Socket will also bind() to the local address and port supplied.
Constructors JDK1.1
public Socket(String host, int port, InetAddress localAddr, int localPort) throws IOException
0 - use a random unsed port. Null - use default local address.
This Socket() constructor creates a socket and connects it to the specified remote host on the specified remote port. The Socket will also bind() to the local address and port supplied.
Methods
Related Exceptions
Class SocketException
java.lang.Object | +--java.lang.Throwable | +--java.lang.Exception | +--java.io.IOException | +--java.net.SocketException
Related Exceptions
Class SecurityException
java.lang.Object | +--java.lang.Throwable | +--java.lang.Exception | +--java.lang.RuntimeException | +--java.lang.SecurityException
Socket Implementation