Java Networking
Java Networking
Java is practically a synonym for Internet programming. There are a number of reasons for this, not
the least of which is its ability to generate secure, cross-platform, portable code.
This chapter explores the java.net package. It is important to emphasize that networking is a very
large and at times complicated topic.
At the core of Java’s networking support is the concept of a socket. A socket identifies an endpoint
in a network.
Sockets are at the foundation of modern networking because a socket allows a single computer to
serve many different clients at once, as well as to serve many different types of information.
This is accomplished through the use of a port, which is a numbered socket on a particular machine.
A server process is said to “listen” to a port until a client connects to it. A server is allowed to accept
multiple clients connected to the same port number, although each session is unique.
To manage multiple client connections, a server process must be multithreaded.
Socket communication takes place via a protocol. Internet Protocol (IP).
Internet Protocol (IP) – Low level
Transmission Control Protocol (TCP) – High level
Port number 21 is for FTP; 23 is for Telnet; 25 is for e-mail; 43 is for whois; 79 is for finger; 80 is for
HTTP; 119 is for netnews
A key component of the Internet is the address. Every computer on the Internet has one. An
Internet address is a number that uniquely identifies each computer on the Net.
Networking
InetAddress
The InetAddress class is used to encapsulate both the numerical IP address and the domain
name for that address
InetAddress can handle both IPv4 and IPv6 addresses.
Three commonly used InetAddress factory methods are shown here:
static InetAddress getLocalHost( ) throws UnknownHostException
static InetAddress getByName(String hostName) throws UnknownHostException
static InetAddress[ ] getAllByName(String hostName) throws UnknownHostException
The getLocalHost( ) method simply returns the InetAddress object that represents the local
host. The getByName( ) method returns an InetAddress for a host name passed to it. If these
methods are unable to resolve the host name, they throw an UnknownHostException.
getAllByName( ) factory method returns an array of InetAddresses that represent all of the
addresses that a particular name resolves to. It will also throw an UnknownHostException if
it can’t resolve the name to at least one address
InetAddress also includes the factory method getByAddress( ), which takes an IP address and
returns an InetAddress object
Example 1: import java.net.*;
class InetAddressTest
{
public static void main(String args[]) throws UnknownHostException
{
InetAddress Address = InetAddress.getLocalHost();
System.out.println(Address);
Address = InetAddress.getByName("google.com");
System.out.println(Address);
InetAddress SW[] = InetAddress.getAllByName("java2s.com");
for (int i=0; i<SW.length; i++)
System.out.println(SW[i]);
}
}
Networking
Socket methods
You can gain access to the input and output streams associated with a Socket by use
of the getInputStream( ) and getOuptutStream( ) methods.
Networking
other methods
connect( ), which allows you to specify a new connection;
isConnected( ), which returns true if the socket is connected to a server;
isBound( ), which returns true if the socket is bound to an address;
isClosed( ), which returns true if the socket is closed.
Example2:
import java.net.*;
import java.io.*;
class Whois
{
public static void main(String args[]) throws Exception
{
int c;
// Create a socket connected to internic.net, port 43.
Socket s = new Socket("internic.net", 43);
// Obtain input and output streams.
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
// Construct a request string.
To access the actual bits or content information of a URL, create a URLConnection object
from it, using its openConnection( ) method, like this:
urlc = url.openConnection()
openConnection( ) has the following general form:
URLConnection openConnection( ) throws IOException
URLConnection
URLConnection is a general-purpose class for accessing the attributes of a remote resource.
HttpURLConnection
Java provides a subclass of URLConnection that provides support for HTTP connections.This
class is called HttpURLConnection.
Networking