Vous êtes sur la page 1sur 27

Client-side networking

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

Class InetAddress (no constructors)


Listing of Public members of java.net.InetAddress.

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

public static synchronized InetAddress getByName(String host) throws UnknownHostException

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

Thrown to indicate that the IP address of a host could not be determined.

SecurityException
java.lang.Object | +--java.lang.Throwable | +--java.lang.Exception | +--java.lang.RuntimeException | +--java.lang.SecurityException

Thrown by the security manager to indicate a security violation.

An InetAddress Example

A nslookup like example. Class FileReader


java.lang.Object | | | +--java.io.FileDescriptor | +--java.io.Reader | | | +--java.io.BufferedReader | +--java.io.InputStreamReader | +--java.io.FileReader

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

public Socket(String host, int port) throws UnknownHostException, IOException

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

public Socket(InetAddress address, int port) throws IOException

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

See the JDK1.3 Documentation

Related Exceptions

Class SocketException
java.lang.Object | +--java.lang.Throwable | +--java.lang.Exception | +--java.io.IOException | +--java.net.SocketException

Direct Known Subclasses: BindException, ConnectException, NoRouteToHostException

Related Exceptions

Class SecurityException
java.lang.Object | +--java.lang.Throwable | +--java.lang.Exception | +--java.lang.RuntimeException | +--java.lang.SecurityException

Direct Known Subclasses: AccessControlException, RMISecurityException

Socket Implementation

Must inherit SocketImpl abstract class.


Create a subclass that implements the necessary connection setup and control methods. Create a SocketImplFactory that returns instances of this new SocketImpl class. Register this SocketImplFactory with the socket class through its setSocketImplFactory() method. Provide, if necessary, a corresponding implementation at the server.

Getting Web pages with a socket


An Example to demonstrate how to download a web page. This program uses URL class

Building a PostOutputStream class


Demostrating an OutputStream class that perform an HTTP post operation. Connect to the web server and send: POST filename HTTP/1.0 Content-type: MIME type application/xwww-form-urlencoded Content-length: length

Building a PostOutputStream class


The body contains: key1=value1&key2=value2.. The Example Program

Vous aimerez peut-être aussi