socket_java.PDF

Socket and HTTP Programming using Java
W.J. BUCHANAN, BSc (Hons.), CEng, PhD.
Napier University, Edinburgh, UK
1 Introduction
Many networking protocols have been developed, but the most important was developed by the US
Defense Advanced Research Projects Agency (DARPA). This protocol was named TCP/IP and has
since allowed computers around the world to intercommunicate, no matter their operating system,
their type or their network connection. DARPA’s initial aim was simply to connect a number of
universities and other research establishments to its own network. The resultant interconnected
network (internet) is now known as the Internet. It has since outgrown this its original application and
many commercial organisations and home users now connect to the Internet. The Internet uses
TCP/IP as a standard to transfer data and each node on the Internet is assigned a unique network
address, called an IP address.
The IP part of the TCP/IP protocol provides for the routing of the data around the Internet and also
a standardised addressing structure, where each node on the Internet is assigned a unique IP address.
This address is a 32-bit address and takes the form of WWW.XXX.YYY.ZZZ (where WWW, XXX,
YYY and ZZZ can range from 0 to 255). IP addresses identify the location of the sender and the also
the receiver. They can either be permanently assigned to a node or can be dynamically assigned when
the node accesses the Internet.
IP addresses are difficult to remember thus Domain Name Services (DNS) are used to allow users
to use symbolic names rather than IP addresses. DNS computers on the Internet determine the IP
address of the named destination resource or application program. The Internet naming structure uses
labels separated by periods (full stops); an example is eece.napier.ac.uk. It uses a hierarchical
structure where organisations are grouped into primary domain names, such as com (for commercial
organisations), edu (for educational organisations), gov (for government organisations), mil (for
military organisations), net (Internet network support centres) and org (other organisations). The primary domain name may also define the country in which the host is located, such as uk (United
Kingdom), fr (France), and so on. All hosts on the Internet must be registered to one of these primary
domain names. The domain name labels after the primary field describe the subnetworks (subnets)
within the network. For example, in the address eece.napier.ac.uk, the ac label relates to an
academic institution within the uk, napier to the name of the institution and eece the subnet with that
organisation. TCP/IP has been unbelievably successful and has outgrown its original application. The
addressing structure only allows for up to 4 billion addresses (many of these will be unused).
IP, itself, allows for the routing and addressing of the transmitted data, whereas, TCP supports the
communication between application programs, using a stream of data. The main function of TCP is to
provide a robust and reliable transport protocol. With TCP, a connection is initially established and is
then maintained for the length of the transmission. It contains simple acknowledgement messages and
a set of sequential numbers. It also supports multiple simultaneous connections using destination and
source port numbers, and manages them for both transmission and reception. A port number relates to
the type of data being transmitted, such as port 23 allows for remote connection (telnet), and the
socket number relates to the transmitted stream. With sockets, a node can have multiple connections,
each with its own unique socket number.
Along with the growth of the Internet came the World Wide Web (WWW). It was initially
conceived in 1989 by CERN, the European particle physics research laboratory in Geneva,
Switzerland. Its main objective was to allow various different types of information, such as text,
graphics and video, to be integrated together in an easy-to-use manner. It also supports the
interlinking of information. One of the main characteristics of the WWW is that stored information
tends to be distributed over a geographically wide area. The WWW, or Web, is basically an
infrastructure of information. This information is stored on the WWW on Web servers and it uses the
Internet to transmit data around the world. These servers run special programs that allow information
to be transmitted to remote computers which are running a Web browser.
The WWW uses TCP/IP for the transmission of the data, and HTTP (Hyper Text Transmission
Protocol) to allow the WWW browser to communicate with the WWW server. HTTP is a stateless
protocol where each transaction is independent of any previous transactions. The advantage of being
stateless is that it allows the rapid access of WWW pages over several widely distributed servers. It
uses the TCP protocol to establish the connection between the client and the server for each
transaction then terminates the connection once the transaction completes.
HTTP also support many different formats of data. Initially, a client issues a request to a server
which may include a prioritised list of formats that it can handle. This allows new formats to be easily
added and also prevents the transmission of unnecessary information.
A client’s WWW browser (the user agent) initially establishes a direct connection with destination
server which contains the required WWW page. To make this connection the client initiates a TCP
connection between the client and the server. After this has been established, the client then issues an
HTTP request, such as, the specific command (the method), the URL (Universal Resource Locator,
which is the full name of the requested data), and possibly extra information such as request
parameters or client information. When the server receives the request, it attempts to perform the
requested action. It then returns an HTTP response, which includes status information, a success/error
code, and extra information. After the client receives this, the TCP connection is closed.
WWW servers use HTML for WWW pages. Unfortunately it is a rather limited language which
has limited user interaction and lacks many of the functionality of programming languages, such as
having expressions, loops or decisions. The increased power of computers allowed the development of
the Java programming language. It was first released in 1995 and was quickly adopted as it fitted well
with Internet-based programming. Java 1.0 introduced the concept of an applet, which is a machineindependent program which runs with a WWW browser. It was quickly followed by Java 1.1 which
gave faster interpretation of Java applets and included many new features. It is a general-purpose,
concurrent, class-based, object-oriented language and has been designed to be relatively simple to
built complex applications.
Java has the great advantage over conventional software languages in that it produces code which
is computer hardware independent. This is because the compiled code (called bytecode) is interpreted
by the WWW browser. Unfortunately, this leads to slower execution, but, as much of the time in a
graphical user interface program is spent updating the graphics display, then the overhead is, as far as
the user is concerned, not a great one.
One advantage that Java has over conventional software languages is its direct support for the
Internet and networking. This paper discusses how Java uses sockets and the HTTP protocol.
2 JAVA SOCKETS
Figure 1 shows the operation of a connection of a client to a server. The server is defined as the
computer which waits for a connection, the client is the computer which initially makes contact with
the server.
On the server the computer initially creates a socket with ServerSocket() method, and then
listens for a connection. The client creates a socket with the Socket() method. When the server
receives this connection it accepts it with the accept() method. Streams can then be setup on these
sockets with the getInputStream() and getOutputStream() methods, and the readUTF() and
writeUTF() methods can be used to read and write data to/from the stream. When the data transfer is
complete the close() method is used to close any open sockets.
Data is received using:
try
{
DataOutputStream send=new DataOutputStream(client.getOutputStream());
send.writeUTF(str);
}
catch (IOException e)
{
//something
}
and transmitted with:
try
{
DataInputStream receive=new DataInputStream(client.getInputStream());
str=receive.readUTF();
}
catch (IOException e)
{
//something
}
Figure 1 Socket connection
class java.net.Socket
The TCP protocol links two computers using sockets and ports. The constructors for
java.net.Socket are:
public Socket(InetAddress address,
int port)
public Socket(String host, int port)
Creates a stream socket and connects it to the specified
address (address) on the specified port (port).
Creates a stream socket and connects it to the specified
port (port) on the specified host (host).
The methods are:
public
public
public
public
public
public
public
synchronized void close()
InetAddress getInetAddress()
InputStream getInputStream()
int getLocalPort()
OutputStream getOutputStream()
int getPort()
String toString()
Creating a socket
Closes the socket.
Returns the address to which the socket is connected.
Returns the InputStream for this socket.
Returns the local port to which the socket is connected.
Returns an OutputStream for this socket.
Returns the remote port to which the socket is connected.
Converts the Socket to a String.
Java applet 1 constructs a socket for
using port 19 (Socket remote = new
After this the data stream is created and assigned to
DataIn. The readUTF() method is then used to get the text from the stream.
www.eece.napier.ac.uk
Socket("www.eece.napier.ac.uk",19)).
&
Java applet 1
import
import
import
import
java.io.*;
java.net.*;
java.awt.*;
java.applet.*;
public class client1 extends Applet
{
TextArea tarea = new TextArea(5,50);
public void init()
{
String
Instr;
InputStream Instream;
TextArea
tarea;
add(tarea);
try
{
Socket remote = new Socket("www.eece.napier.ac.uk",19);
Instream = remote.getInputStream();
DataInputStream DataIn = new DataInputStream(Instream);
do
{
Instr = DataIn.readUTF();
if (Instr!=null) tarea.setText(Instr);
} while (Instr!=null);
}
catch (UnknownHostException err)
{
tarea.setText("UNKNOWN HOST: "+err);
}
catch (IOException err)
{
tarea.setText("Error" + err); }
}
}
Java program 2 contacts a server on a given port and returns the local and remote port. It uses
command line arguments, and the program is run in the form:
java client2
host port
where java is the Java interpreter, client2 is the name of the class file, host is the name of the host to
contact and port is the port to use. The args.length parameter is used to determine the number of
command line options, anything other than two will display the following message:
Usage : client2 host port
&
Java program 2
import java.net.*;
import java.io.*;
public class client2
{
public static void main (String args[])
{
if (args.length !=2)
System.out.println(" Usage : client2 host port");
else
{
String inp;
try
{
Socket sock = new Socket(args[0], Integer.valueOf(args[1]).intValue());
DataInputStream is = new DataInputStream(sock.getInputStream());
System.out.println("address : " + sock.getInetAddress());
System.out.println("port : " + sock.getPort());
System.out.println("Local address : " +
sock.getLocalAddress());
System.out.println("Localport : " + sock.getLocalPort());
while((inp = is.readUTF()) != null)
{ System.out.println(inp);}
}
catch (UnknownHostException e)
{
System.out.println(" Known Host : " + e.getMessage());
}
catch (IOException e)
{
System.out.println("error I/O : " + e.getMessage());
}
finally
{
System.out.println("End of program");
}
}
}
}
Sample run 1 shows a sample run which connects to port 13 on www.eece.napier.ac.uk. It can be
seen that the connection to this port causes the server to return back the current date and time. Sample
run 2 connects into the same server, in this case on port 19. It can be seen that a connection to this
port returns a sequence of characters. Typical port values are: echo (7), null (9), daytime (13),
Character test (19), ftp (21), telnet (23), SMTP (25), TIME (37), DNS (53), HTTP (80) and POP3
(110).
: Sample run 1
>> java client2 www.eece.napier.ac.uk 13
Host and IP address : www.eece.napier.ac.uk/146.176.151.139
port : 13
Local address :pc419.eece.napier.ac.uk
Localport : 1393
Fri May 8 13:19:59 1998
End of program
: Sample run 2
>> java client2 www.eece.napier.ac.uk 19
Host and IP address : www.eece.napier.ac.uk/146.176.151.139
port : 19
Local IP address :pc419.eece.napier.ac.uk
Localport : 1403
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefg
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefgh
"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghi
#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghij
$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijk
%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijkl
&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm
'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmn
()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmno
)*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnop
*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopq
+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqr
Client/server program
A server is a computer which runs a special program which passively waits for another computer (a
client) to connect to it. This server normally performs some sort of special operation, such as FTP,
Telnet or WWW service.
Java program 3 acts as a server program and waits for a connection on port 1111. When a
connection is received on this port it sends its current date and time back to the client. This program
can be run with Java program 2 (which is running on a remote computer) with a connection to the
server’s IP address (or domain name) and using port 1111. When the client connects to the server, the
server responds back to the client with its current date and time. Figure 2 shows a sample run from
the server. It can be seen that it has received connection from the client with the IP address of
62.136.29.76.
&
Java program 3
import java.net.*;
import java.io.*;
import java.util.*;
class server
{
public static void main( String arg[])
{
try
{
ServerSocket sock = new ServerSocket(1111);
Socket sock1 = sock.accept();
System.out.println(sock1.toString());
System.out.println("address : " +
sock1.getInetAddress());
System.out.println("port
: " +
sock1.getPort());
DataOutputStream out = new DataOutputStream(sock1.getOutputStream());
out.writeUTF("Welcome "+ sock1.getInetAddress().getHostName()+
". We are "+ new Date()+ "\n");
sock1.close();
sock.close();
}
catch(IOException err)
{
System.out.println(err.getMessage());
}
finally
{
System.out.println("End of the program");
}
}
}
Figure 2 Sample run of client/server programs
3 Java networking methods
Java directly supports TCP/IP communications and has the following classes:
java.net.ContentHandler
java.net.DatagramPacket
java.net.DatagramSocket
java.net.InetAddress
java.net.ServerSocket
java.net.Socket
java.net.SocketImpl
java.net.URL
java.net.URLConnection
java.net.URLEncoder
java.net.URLStreamHandler
Class which reads data from a URLConnection and also supports
MIME (Multipurpose Internet Mail Extension).
Class representing a datagram packet which contains packet data,
packet length, internet addresses and the port number.
Class representing a datagram socket class.
Class representing Internet addresses.
Class representing Socket server class.
Class representing Socket client classes.
Socket implementation class.
Class URL representing a Uniform Reference Locator (URL) which
is a reference to an object on the WWW.
Class representing an active connection to an object represented by a
URL.
Converts strings of text into URLEncoded format.
Class for opening URL streams.
When an error occurs in the connection or in the transmission and reception of data it causes an
exception. The classes which handle these are:
java.io.IOException
java.net.MalformedURLException
java.net.ProtocolException
java.net.SocketException
java.net.UnknownHostException
java.net.UnknownServiceException
To handle general errors.
Malformed URL.
Protocol error.
Socket error.
Unknown host error.
Unknown service error.
class java.net.InetAddress
This class represents Internet addresses. The methods are:
public static synchronized InetAddress[]
getAllByName(String host)
public static synchronized InetAddress
getByName(String host)
public String getHostAddress()
public byte[] getAddress()
public String getHostName()
public static InetAddress getLocalHost()
public String toString()
This returns an array with all the corresponding
InetAddresses for a given host name (host).
This returns the network address of an indicated
host. A hostname of null return the default address
for the local machine.
This returns the IP address string
(WW.XX.YY.ZZ) in a string format.
This returns the raw IP address in network byte
order. The array position 0 (addr[0]) contains the
highest order byte.
Gets the hostname for this address. If the host is
equal to null, then this address refers to any of the
local machine’s available network addresses.
Returns the local host.
Converts the InetAddress to a String.
Java applet 4 uses the getAllByName method to determine all the IP addresses associated with an
Internet host. In this case the host is named www.microsoft.com. It can be seen from the test run that
there are 18 IP addresses associated with this domain name. It can be seen that the applet causes an
exception error as the loop tries to display 30 such IP addresses. When the program reaches the 19th
InetAddress, the exception error is displayed (ArrayIndexOutOfBoundsException).
& Java applet 4
import java.net.*;
import java.awt.*;
import java.applet.*;
public class http1 extends Applet
{
InetAddress[] address;
int i
public void start()
{
System.out.println("Started");
try
{
address=InetAddress.getAllByName("www.microsoft.com");
for (i=0;i<30;i++)
{
System.out.println("Address " + address[i]);
}
}
catch (Exception e)
{
System.out.println("Error :" + e);
}
}
}
: Sample run 3
Started
Address www.microsoft.com/207.68.137.59
Address www.microsoft.com/207.68.143.192
Address www.microsoft.com/207.68.143.193
Address www.microsoft.com/207.68.143.194
Address www.microsoft.com/207.68.143.195
Address www.microsoft.com/207.68.156.49
Address www.microsoft.com/207.68.137.56
Address www.microsoft.com/207.68.156.51
Address www.microsoft.com/207.68.156.52
Address www.microsoft.com/207.68.137.62
Address www.microsoft.com/207.68.156.53
Address www.microsoft.com/207.68.156.54
Address www.microsoft.com/207.68.137.65
Address www.microsoft.com/207.68.156.73
Address www.microsoft.com/207.68.156.61
Address www.microsoft.com/207.68.156.16
Address www.microsoft.com/207.68.156.58
Address www.microsoft.com/207.68.137.53
Error :java.lang.ArrayIndexOutOfBoundsException: 18
Java applet 5 overcomes the problem of the displaying of the exception. In this case the exception is
caught by inserting the address display within a try {} statement then having a catch statement which
does nothing. Test run 4 shows a sample run.
& Java applet 5
import java.net.*;
import java.awt.*;
import java.applet.*;
public class http2 extends Applet
{
InetAddress[] address;
int i;
public void start()
{
System.out.println("Started");
try
{
address=InetAddress.getAllByName("www.microsoft.com");
try
{
for (i=0;i<30;i++)
{
System.out.println("Address " + address[i]);
}
}
catch(Exception e)
{ /* Do nothing about the exception, as it is not really an error */}
}
catch (Exception e)
{
System.out.println("Error :" + e);
}
}
}
: Sample run 4
Started
Address
Address
Address
Address
Address
Address
Address
Address
Address
Address
Address
Address
Address
Address
Address
Address
Address
Address
www.microsoft.com/207.68.137.59
www.microsoft.com/207.68.143.192
www.microsoft.com/207.68.143.193
www.microsoft.com/207.68.143.194
www.microsoft.com/207.68.143.195
www.microsoft.com/207.68.156.49
www.microsoft.com/207.68.137.56
www.microsoft.com/207.68.156.51
www.microsoft.com/207.68.156.52
www.microsoft.com/207.68.137.62
www.microsoft.com/207.68.156.53
www.microsoft.com/207.68.156.54
www.microsoft.com/207.68.137.65
www.microsoft.com/207.68.156.73
www.microsoft.com/207.68.156.61
www.microsoft.com/207.68.156.16
www.microsoft.com/207.68.156.58
www.microsoft.com/207.68.137.53
Java applet 6 shows an example of displaying the local host name (getLocalHost), the host name
(getHostName) and the hosts IP address (getHostAddress). Test run 5 shows a sample run.
& Java applet 6
import java.net.*;
import java.awt.*;
import java.applet.*;
public class http3 extends Applet
{
InetAddress host;
String str;
int i;
public void start()
{
System.out.println("Started");
try
{
host=InetAddress.getLocalHost();
System.out.println("Local host " + host);
str=host.getHostName();
System.out.println("Host name: " + str);
str=host.getHostAddress();
System.out.println("Host address: " + str);
}
catch (Exception e)
{
System.out.println("Error :" + e);
}
}
}
: Sample run 5
Started
Local host toshiba/195.232.26.125
Host name: toshiba
Host address: 195.232.26.125
The previous Java applets have all displayed their output to the output terminal (with
System.out.println). Java applet 7 uses the drawString method to display the output text to the
Applet window. Figure 3 shows a sample run.
& Java applet 7
import java.net.*;
import java.awt.*;
import java.applet.*;
public class http4 extends Applet
{
InetAddress[] address;
int i;
public void paint(Graphics g)
{
g.drawString("Addresses for WWW.MICROSOFT.COM",5,10);
try
{
address=InetAddress.getAllByName("www.microsoft.com");
for (i=0;i<30;i++)
{
g.drawString(" "+
address[i].toString(),5,20+10*i);
}
}
catch (Exception e)
{
System.out.println("Error :" + e);
}
}
}
Figure 3 Sample run
class java.net.URL
The URL (Uniform Reference Locator) class is used to reference to an object on the World Wide Web.
The main constructors are:
public URL(String protocol, String host,
int port, String file)
public URL(String protocol, String host,
String file)
public URL(String spec)
public URL(URL context, String spec)
Creates an absolute URL from the specified
protocol (protocol), host (host), port (port)
and file (file).
Creates an absolute URL from the specified
protocol (protocol), host (host) and file
(file).
Creates a URL from an unparsed absolute URL
(spec)
Creates a URL from an unparsed absolute URL
(spec) in the specified context.
The methods are:
public int getPort()
public String getProtocol()
public String getHost()
public String getFile()
public boolean equals(Object obj)
public String toString()
Returns a port number. A return value of -1
indicates that the port is not set.
Returns the protocol name.
Returns the host name.
Returns the file name.
Compares two URLs, where obj is the URL to
compare against.
Converts to a string format.
public String toExternalForm()
public URLConnection openConnection()
public final InputStream openStream()
public final Object getContent()
Reverses the URL parsing.
Creates a URLConnection object that contains a
connection to the remote object referred to by the
URL.
Opens an input stream.
Gets the contents from this opened connection.
class java.net.URLConnection
Represents an active connection to an object represented by a URL. The main methods are:
public abstract void connect()
public URL getURL()
public int getContentLength()
public String getContentType()
public String getContentEncoding()
public long getExpiration()
public long getDate()
public long getLastModified()
public String getHeaderField(String name)
public Object getContent()
public InputStream getInputStream()
public OutputStream getOutputStream()
public String toString()
URLConnection objects are initially created and
then they are connected.
Returns the URL for this connection.
Returns the content length, a –1 if not known.
Returns the content type, a null if not known.
Returns the content encoding, a null if not
known.
Returns the expiration date of the object, a 0 if
not known.
Returns the sending date of the object, a 0 if not
known.
Returns the last modified date of the object, a 0 if
not known.
Returns a header field by name (name), a null if
not known.
Returns the object referred to by this URL.
Used to read from objects.
Used to write to objects.
Returns the String URL representation.
class java.net.URLStreamHandler
Abstract class for URL stream openers. Subclasses of this class know how to create streams for
particular protocol types.
protected abstract URLConnection
openConnection(URL u)
protected void parseURL(URL u, String
spec, int start, int limit)
protected String toExternalForm(URL u)
protected void setURL(URL u, String
protocol, String host, int port,
String file, String ref)
Opens an input stream to the object referenced by
the URL (u).
Parses the string (spec) into URL (u), where
start and limit refer to the range of
characters in spec that should be parsed.
Reverses the parsing of the URL.
Calls the (protected) set method out of the URL
given.
java.applet.AppletContext
The AppletContext can be used by an applet to obtain information from the applet’s environment,
which is usually the browser or the applet viewer. Related methods are:
public abstract void showDocument(URL url)
public abstract void showDocument(URL url,
String target)
Shows a new document.
Show a new document in a target window or
frame.
Connecting to a WWW site
Java applet 8 shows an example of an applet that connects to a WWW site. It allows the user to enter
a URL and it also shows a status window (status). Figure 4 show a sample runs. The left-hand side
of In Figure 4 shows that the user has added an incorrect URL (www.sun.com). The status windows
shows that this is an error. The right-hand side of the figure shows a correct URL
(http://www.sun.com).
& Java applet 8
import java.net.*;
import java.awt.*;
import java.applet.*;
public class j1 extends Applet
{
URL
urlWWW;
Button
btn;
Label
label = new Label("Enter a URL:");
TextField inURL = new TextField(30);
TextArea status = new TextArea(3,30);
public void init()
{
add(label);
add(inURL);
btn = (new Button("Connect"));
add(btn);
add(status);
}
public void getURL()//Check for valid URL
{
try
{
String str;
str=inURL.getText();
status.setText("Site: " + str);
urlWWW = new URL(str);
}
catch (MalformedURLException e)
{
status.setText("URL Error: " + e);
}
}
public boolean action(Event evt, Object obj)
{
if (evt.target.equals(btn))
{
status.setText("Connecting...\n");
getURL();
getAppletContext().showDocument(urlWWW);
return true;
}
return false;
}
}
Figure 4 WWW connection