Client-Server Architecture: Java Networking
Client-Server Architecture: Java Networking
In Java, there is a java.net package provides the network support. All the classes for
making a network program are define in the java.net package. Through TCP we can
communicate over the network.
Such an environment providing the sharing and exchanging facilities among the
computer machines is called as a computer network.
Now you just need to maintain a network in which all the computers are
interconnected and share the same printer and all such devices i.e. we just link the
computer devices
together with hardware and software supporting data communication across the network.
Client-Server Architecture
Client-server architecture can be considered as a network environment that exchanges
information between a server machine and a client machine where server has some
resources that can be shared by different clients.
The main purpose of the server socket is to listen an incoming connection request and
ordinary socket is used to ask to server for the connection. Once a connection between
client and server established, both the connected socket can communicate with each
other.
It will take effect of registering the application with the system to receive all data
bound for that port. There is a limitation for the port that no port can be bound by two
applications at the same time.
Introduction of networking Ports
Now we are aware of the importance of the port number. In the same order there are
some ports which are predefine and called reserved ports. Some of them are given
below :-
Reserved port numbers.
Service Port no.
echo 7
daytime 13
ftp 21
telnet 23
smtp 25
finger 79
http 80
pop3 110
If we consider the range of the port numbers, there are 0 to 65,535 ports available. The
port numbers ranging from 0 - 1023 are reserved ports or we can say that are restricted
ports. All the 0 to 1023 ports are reserved for use by well-known services such as FTP,
telnet and http and other system services. These ports are called well-known ports.
Server Sockets
In common language we can say that the sockets are just like an end-point of two-way
communication link over the network between two programs. Socket classes are used
to establish a connection between client program and a server program. In java there is
a java
.net package, which provides two types of classes- first is ordinary socket, which
implement the client side connection and second is server socket, which implement the
server side connection.
In Java there are many socket class that is used for creating a Server applications
. ServerSockets are quite different from normal Sockets. The main work of ServerSocket
class is to wait for a request of connection by the client and connect them on published
ports and then possibly returns a result to the requester. The SocketImpl is a common
superclass of all classes that actually implement sockets. It is used to create both client
and server sockets.
There are some constructors that might throw an IOException under adverse conditions.
Some of the constructors are as under:
ServerSocket(int port, int Creates a server socket on the specified port with a
maxQueue) maximum queue length of maxQueue.
Now here we are provide a simple name structure of a URL which addresses the Java Web site
hosted by roseindia:
In the above given URL structure there are two main components:
• Protocol identifier
• Resource name
In which there is a colon and two forward slashes between protocol identifier and the resource
name. The protocol identifier is the name of the protocol which is used in the URL structure to get
the resource. Here we use the http which is one of the protocols used to access different types of
resources on the net. HTTP is mainly used to serve the hypertext documents.
The second part of the URL is resource name that is the complete address of the resource. The
resource name contains one or more of the components listed in the following table :-
Host Name The name of the machine on which the resource lives.
In a network environment the client and the server communicate with each-other by
reliable channel like TCP socket which have dedicated point-to-point channel between
client and server. All data sent over the channel is received and sent in the same order.
In other case the application, which communicate by datagrams sends and receives
completely independent packets of information and they also don’t need a dedicated
point-to-point channel.
Datagrams are simply a bundles of information data passed between machines. Java
implements datagrams on top of the UDP protocol by using three classes which are in
java.net package as well as define as under :
• DatagramSocket
• DatagramPacket
• MulticastSocket
In which the DatagramPacket is used to contain data for sent and receive by a
application and the DatagramSocket is used to send or receive the DatagramPackets
over the network envirnoment. While the Multicast socket is used to broadcast the
DatagramPackets to multiple recipients.
Networking in Java
In this section we are exploring the java.net package which provides the support for
networking in java with a generic style. All the java classes for developing a network
program are defined in the java.net package.
Here we illustrate an example in which a client sends a request (lets say the request
is.."POST/index.html HTTP/1.0\\n\\n" ) to the server for a file named index.html and
as the server establishes a connection successfully, it gets the index.html file and sends
it to the client.
Server Client
Listens to port 8080. Connects to port 8080.
Writes
Accepts the connection. "POST/index.html
HTTP/1.0\\n\\n".
Reads up until it gets the
second end-of line (\\n).
Sees that POST is a known
command and that HTTP/1.0
is a valid protocol version.
Reads a local file called
/index.html.
Writes "HTTP/1.0 8080 "8080" means "here
OK\\n\\n". comes the file."
Reads the contents of
Copies the contents of the
the file and displays
file into the socket.
it.
The above process is an actual transaction via which a client and a server talk with
each other. Every computer on a network has an address. It is called an Internet
address ( IP Address) it is a 12 digit number that uniquely identifies each computer on
the Network. There are 32 bits in an IP address having a sequence of four numbers
between 0 and 255 separated by dots (.).
Now lets quickly move on to the networking part of java and take a look at how it
relates to all of these networking concepts. In Java we can build I/O objects across the
network by extending the I/O interface. Java supports most of the networking
protocols e.g. TCP and UDP protocol families, TCP is used for reliable stream-based
I/O across the network and UDP supports the point-to-point datagram-oriented model.
All the java networking classes and interfaces are contained in the java.net package, it
is given below:
Authenticator JarURLConnection SocketPermission
ContentHandler MulticastSocket URL
DatagramPacket NetPermission URLClassLoader
DatagramSocket PasswordAuthentication URLConnection
DatagramSocketImpl ServerSocket URLDecoder
HttpURLConnection Socket URLEncoder
InetAddress SocketImpl URLStreamHandler
Including all above the package's interfaces of the java.net are as under :
ContentHandlerFactory SocketImplFactory
URLStreamHandlerFactory FileNameMap
SocketOptions
Apart from all of this the address is the fundamental part in sending mail, or
establishing a connection across the Internet. In java the InetAddress class is used to
encapsulate both the numerical IP address and the domain name for that address. The
InetAddress class has no visible constructors that is why, to create an InetAddress
object, we have to use one of the available factory methods like getLocalHost( ),
getByName( ), and getAllByName( ) can be used.