NETWORK PROGRAMMING IN JAVA

INTRODUCTION

In the 1980s, the US government’s Advanced Research Projects Agency (ARPA) provided funds to the University of California at Berkeley to implement TCP/IP protocols under the UNIX operating system. During this project, a group of Berkeley researchers developed an application program interface (API) for TCP/IP network communications called the socket interface. The socket interface is an API TCP/IP networks i.e. it defines a variety of software functions or routines for the development of applications for TCP/IP networks.

The socket interface designers originally built their interface into the UNIX operating system. However, the other operating systems, environments, such as Microsoft Windows, implement the socket interface as software libraries. However, regardless of the environment in which we program, the program code will look much the same.

NETWORK PROGRAMMING

The core of a network application consists of a pair of programs, a client program and server program. When these two programs are executed, a client and a server process are created, and these two processes communicate with each other by reading from and writing to sockets. A socket is a software abstraction for an input or output medium of communication. It is a communication channel that enables you to transfer data through a certain port. A port is a software abstraction that provides a means to differentiate between network services.

More specifically, a port is a 16-bit number identifying the different services offered by a network server. Each computer on the Internet has a bunch of ports that can be assigned different services. To use a particular service and therefore establish a line of communication via a particular protocol, we must connect to the correct port. Ports are numbered, and some of the numbers are specifically associated with a type of service. Ports with specific service assignments are known as standards ports. For example, the FTP service is located on port 21, so any computer wanting to perform an FTP file transfer would connect to port 21 of the host computer. Likewise, the HTTP service is located on port 80, so any time we access a Web site, we are really connecting to port 80 of the host using the HTTP protocol behind the scenes. All standard service assignments are given port values below 1024. This means that ports above 1024 are considered for custom communications, such as those required by a java client/server program implementing its own protocol.

TCP/IP lets the network programs use connection-oriented or connectionless network communication. The TCP/IP protocol suite supports these using two different protocols, TCP (Transport Control Protocol) and UDP (User Datagram Protocol).

NETWORK PROGRAMMING WITH TCP

TCP is a connection oriented, transport layer protocol that works on top of IP (Internet Protocol). In connection oriented network communication, data flows as a single, serial stream of bytes without record or other type of boundaries. Connection-oriented communications use virtual circuits – the link between the endpoints appears to be a point-point connection. The virtual circuit is permanent throughout the session between the local and the remote host. In other words, after the network software establishes the connection, the program can exchange data with the remote host in a steady byte stream. The circuit has to be established before data transfer begins. A connection is based on the host IP address and the port that the server is listening on.

Processes running on different machines communicate with each other by sending message into sockets. Each process is analogous to a house and the process’s socket is analogous to a door. As shown in the following diagram, the socket is the door between the application process and TCP. The application developer has control of everything on the application-layer side of the socket.

The client has to initiate the contact with the server. In order to react to the client’s initial contact, the server must be running as a process before the client attempts to initiate contact. Also the server must have some sort of a door i.e. a socket that welcomes the initial contact from a client running on an arbitrary machine. With the server process running, the client process can initiate a TCP connection to the server. This is done in the client program by creating a socket object. When the client creates its socket object, it specifies the address of the server process, namely, the IP address of the server and the port number of the process. Upon creation of the socket object, TCP in the client establishes a TCP connection with the server.

From the application’s perspective, the TCP connection is a direct virtual pipe between the client’s socket and the server’s connection socket. The client process can send arbitrary bytes into its socket. TCP guarantees that the server process will receive through the connection socket, each byte in the order sent. The client process can also receive byte from its socket and the server process can also send bytes into its connection socket. This is illustrated in the following diagram. Because sockets play a central role in client-server applications, client-server application development is also referred to as socket programming.

NETWORK PROGRAMMING WITH UDP

UDP is a connectionless transport layer protocol that also sits on top of IP. In connectionless network communication, data travels in separate, self-contained data packets called datagrams. Unlike TCP, UDP provides no data integrity mechanisms except for a single checksum, and unlike TCP, dose not establish a connection to the destination before sending any data. UDP simply packages its data with the destination address and port number and sends it out onto the network. If the destination host is live and listening it will receive the datagram, if not it will be discarded. Because there is no guaranteed delivery, as there is with TCP, there is a possibility that datagrams will be lost, corrupted or delivered in wrong order. Clearly, UDP isn’t a good match for applications like FTP that require reliable transmission of data over potentially unreliable networks. However there are many kinds of applications in which raw speed is more important than getting every bit right.

We can use sockets for connection-oriented or connectionless communications.

CLIENT SERVER APPLICATIONS

There are two sorts of client server applications. One sort is a client-server application that is an implementation of a protocol standard defined in an RFC. For such an implementation, the client and server programs must conform to the rules dictated by the RFC. For example, the client program could be an implementation of the FTP client, defined in RFC 959, and the server program could be an implementation of the FTP server, also defined in RFC 959. If one developer writes code for the client program and an independent developer writes code for the server program, and both developers carefully follow the rules of the RFC, then the two programs will be able to iteroperate.

Indeed, most of today’s network applications involve communication between client and server programs that have been created by independent developers. For example a FTP client on a PC can upload a file to a Unix FTP server. When a client or server program implements a protocol defined in an RFC it should use the port number associated with the protocol.

The other sort of client-server application is a proprietary client-server application. In this case the client and server programs do not necessarily conform to any existing RFC. A single developer or development team creates both the client and server programs, and the developer has complete control over what he puts in the code. But because the code does not implement a public-domain protocol, other independent developers will not be able to develop code that interoperates with the application.

When developing a proprietary application, the developer must be careful not to use one of the well-known port numbers defined in the RFCs. During the development phase of a proprietary client-server application, one of the first decisions the developer must make is whether the application is to run over TCP or over UDP.

SOME CLARIFICATIONS:

IP ADDRESS

Java works with in the Internet, which requires a way to uniquely identify a machine from all the others in the world. This is accomplished with the IP address that can exist in two forms:

  1. The DNS form-our domain name is Klnce.edu, so suppose we have a computer called system in our domain. Its domain name would be system1.Klnce.edu.
  2. In the another form, every computer in the network identified by a unique 32-bit number, which is typically written as a group of four numbers separated by dots. E.g. 196.168.1.201

In Java, special class called InetAddress represents the IP address. The InetAddress class doesn’t have a constructor, but provides three static methods to get an instance of InetAddress class.

InetAddress getLocalhost()

InetAddress getByName()

InetAddress[] getAllByName()

SERVERS AND CLIENTS

The Java network-programming model is based on Client/Server model. The machine that “stays in one place “ is called the server, and that one seeks for connection is called the Client. This distinction is only important while the client tries to make a connection to the server. Once they have connected is becomes a two-way communication process.

PORTS

An IP Address is not enough to identify a unique server, because many server programs may exists on one machine. So we need a unique identification for each server. This unique identification is referred to as port. When we are setting up client or a server we must choose a port to where both client and server agree to meet. This port is not a physical port, but a logical port specified by a 16-bit number. The port numbers 0 to 1024 are already used by common applications like FTP, TELNET etc.

SOCKETS

The socket is the software abstraction used to represent the “terminals” of a connection between two machines. For a given connection, there is socket on each machine. In Java, we create socket to make a connection to the other machine, then we get InputStream and OutputputStream from the socket in order to able to keep the connection as an I/O streams objects. At this point, you use methods getInputStream() and getOutputStream() to produce the corresponding InputStream and OutputStream objects from each socket.

The server socket class Serversocket in the network in the network package java.net can be used to create a “server” or “listening Socket”. This Serversocket listens for incoming connections and then returns an established socket through accept() method. The accept() method of Serversocket waits until some other machine connects to it and returns an actual socket.

DATAGRAMS

The Transmission Control Protocol (TCP), which is designed for ultimate reliability and which guarantees that the data will reach safely to the other end. The second protocol called UserDatagram Protocol(UDP), doesn’t guarantee that the packets will be delivered in order. It is called an unreliable protocol but, is very faster than the reliable TCP.

In Java, UDP client and server objects can be created using DatagramSocket class. But there is no any Serversocket that waits for a connection, because there is no connection held between two ends. The Datagram packet itself must know the Source and Destination address.

A Datagramsocket sends and receives packets and the DatagramPacket contains the information itself. When we are receiving a datagram, we only need to provide a buffer where the data will be placed. The constructor for a DatagramPacket to receice datagram is declared as,

DatagramPacket (byte[] buf, int buf.length)

Where buf is the array of byte.

When we send a datagram, the DatagramPacket must contain not only the data, but also the IP address and port it will be sent. So the constructot for the outgoing DatagramPacket is

DatagramPacket (byte[] buf, int length, InetAddress ia, int port)

Buf – Contains the data that wewant to send out.

Length – is the length of the buf.

ACCESSING WWW RESOURCES

Java provides a flexible URL and URL connection class to obtain information from resources on the network that has a URL address. We can create a URL class instance for a WWW address and then use the getContent() method for the URL class to download the content.java supports only the “text/plain”, “image/gif” and “image/jpeg” data types.

USING URL AND URL CONNECTION

We can create URLs by specifying a single string value for the entire URL address, or separate values for the protocols (IITTP, FTP and so on), host name, port number and file path. We can also create a URL by specifying a string value. Creating an URL class instance can throw the MalformedURLException.

SCOKET PROGRAMMING IN JAVA

Socket programming can be done in any language, which supports network communication, but java is preferred because it is platform independent, it has exception mechanisms for robust handling of common problems that occur during I/O and networking operations. One of java’s major strong suits as a programming language for client-server application development is its wide range of network support. Java has this advantage because it was developed with the Internet in mind. The result is that we have a lot of options in regard to network programming in Java. Java performs all of its network communication through sockets. Java provides basic socket classes to make programming with sockets much easier. Java sockets are broken down into two types: datagram sockets and stream sockets.

DATAGRAM SOCKETS

A datagram socket uses UDP to facilitate the sending of datagrams in an unreliable manner. Unreliable means that information sent via datagrams isn’t guaranteed to reach the destination. The trade-off here is that datagram sockets require relatively few resources because of this unreliable design, the clients and sent servers in a datagram scenario don’s require a dedicated network connection. Datagram are sent as individually bundled packets that may or may not reach the destination in any particular order or at any particular time. For this reason datagrams include a sequence number that specifies the order. The receiver has to wait to receive the entire sequence and put them back in the original order.

Java supports datagram socket programming through two classes DatagramSocket and DatagramPacket. The DatagramSocket class provides an implemention for a basic datagram socket. The DatagramPacket class provides the functionally required to send a packet of information through a datagram socket.

The methods implemented in the DatagramSocket class are

DatagramSocket() - This is the default constructor used to create a socket

DatagramSocket(int port) - This is also a constructor which is used to create a

Socket which is connected to a specific port.

Void send(DatagramPacket p) - To send a datagram packet p

Void receive (DatagramPacket p) - To receive a datagram packet p

Void close() - To close the connection

The methods implemented in the DatagramPacket class are

DatagramPacket (byte buf[], int length) - This is a constructor used to receive datagrams, but is a byte array that holds the data received and length is the maximum number of bytes to read.

DatagramPacket(byte buf[], int length, InetAddress addr, int port) - This is a constructor to send datagrams to the specified Address.

Byte getData() - To return the data received in the socket.

Int getLength() - To return the length of the datagram packet.

STREAM SOCKETS

A stream socket is a connected socket through which data is sent continuously. Because the communication link is dedicated, it is reliable. Java supports stream socket programming through two classes: Socket and ServerScoket. The Socket class provides the necessary overhead to facilitate a stream socket client and the ServerSocket class provides the core functionality for a server.

The methods implemented in this Socket class are

Socket (String host, int port) - This creates a stream socket and connects to the specified host host can be a host name or IP address and port must be in a range of 1- 65535.

Socket (InetAddress addr, int port) – This creates a stream socket and connects to the specified host and Port.

Inputstream getInputStream() - This returns an InputStream that allows the socket to receive data across the TCP connection.

Outputstream getoutputStream() - This returns an outputStream that allows the socket to send data across the TCP connection.

The methods implemented in the ServerSocket class are

ServerSocket (int port) - This is a constructor used to create a server socket.

ServerSocket (int port, int count) - This is a constructor used to create a server socket. The count parameter specifies a timeout period for the server to quit automatically listening for a client connection.