Vous êtes sur la page 1sur 7

Java networking

The Java platform is extremely preferable to write an application program require to


communicate with the resources on network. Java, mainly focuses on the networking
relating the description of the networking capabilities of the Java platform and second
one is describes a brief summary of networking in a very simple manner that how to
use URLs, sockets, and datagrams.

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.

In a Client/server architecture individual computers (known as clients) are connected


to a central computer which is known as “server”.
Let’s take an example of a file server to understand the core process of a client/server
network, the file server acts as a storage space on the network for the files,
spreadsheets, databases, etc. Instead of storing these records on every individual
computer, the file server allows the clients to store their files on one central computer
and make them sharable. The client-server architecture is beneficial in reducing the
multiple iterations of a single file and allowing the organization to have one centralized
point for every computer to access the same file.

The interaction between a lamp and an electrical socket can be considered as a


interaction between client and server is just like. In the example the electrical socket is
just like a server and the lamp works like a client.
Socket and ports
In common language we can say that the socket is one of the most primal technologies
of computer networking. Sockets are just like an end-point of two-way
communication, which allow applications
to communicate using network hardware and operating systems
. However in case of java never get confused with a socket. 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.
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.

In other hand we can consider the work of port in connection-based communication is


like a medium through which, an application establish a connection with another
application by binding a socket by a port number. Addressing the information and the
port no., accompanied the data transfer over the network. The Ports are used by TCP
and UDP to deliver the data to the right application, are identified by a 16-bit number.

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

In computer networking of connection-based communication port is like a medium


through which, an application establish a connection with another application by
binding a socket by a port number. Addressing the information and the port no.,
accompanied the data transfer over the network. The Ports are used by TCP and UDP
to deliver the data to the right application, are identified by a 16-bit number present in
the header of a data packet. Ports are typically used to map data to a particular process
running on a client. If we consider a letter (data packet) sent to a particular apartment
(IP) with house no. (port no), at this time the port no. is the most important part for the
delivery of the letter. In order for the delivery to work, the sender needs to include an
house number along with the address to ensure the letter gets to the right destination.

If we consider the client-server architecture, a server application binds a socket to a


specific port number in connection-based communication. It registered the server with
the system where all the data destined for that port.

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:

Creates server socket on the specified port with a queue


ServerSocket(int port)
length of 50.

ServerSocket(int port, int Creates a server socket on the specified port with a
maxQueue) maximum queue length of maxQueue.

Creates a server socket on the specified port with a


ServerSocket(int port, int
maximum queue length of maxQueue. On a multihomed
maxQueue, InetAddress
host, localAddress specifies the IP address to which this
localAddress)
socket binds.

HTTP is mainly used to serve the hypertext documents.


URL in term of Java Network Programming
A URL (Uniform Resource Locator) is the address of a resource on the Internet. In
java network programming we can use URLs to connect and retrieve information over
the Internet. In this section we will provide the complete information about the way of
using URL in java network programming through a full code-example. The example
give you the full exposure to network programming e.g. how to open a connection to a
URL, how to retrieve information of a given URL.

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.

Filename The pathname to the file on the machine.

Port
The port number to which to connect (typically optional).
Number

A reference to a named anchor within a resource that usually identifies


Reference
a specific location within a file (typically optional).

In the above given table of the URL structure, for many protocols the host name and the
filename are required but in some times the port number and reference are optional.

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 (.).

However it is a very cumbersome process to remember the computers on the network


through their IP address values so DNS is there to help you out. It has devised a string
format to identify an IP address that avoids the user to track so many numbers (IPs)
over a network, it is also known as Domain Naming Service (DNS).

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.

Vous aimerez peut-être aussi