this section we are exploring the java.net
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
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.
|Listens to port 8080.||Connects to port 8080.|
|Accepts the connection.||Writes "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 OK\\n\\n".||"8080" means "here comes the file."|
Copies the contents of the file into the socket.
Reads the contents of the file and displays 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:
Including all above the package's interfaces of the java.net are as under :
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.
Recommend the tutorial