Java Networking | Basics

How Java supports Network programming?

Java Networking is a vast concept. As we know, there are mainly three types of networks. Metropolitan Area Network, Local Area Network Wide Area Network. The Internet has pertained as the network of the networks. It also refer as Wide Area Network. In the context of Java, we will learn how networking is support by Java and how it exactly works. Java is an internet language that supports web sites, web pages, data and so on.

thus, It is necessary to understand a few concepts of the internet before we proceed with the advanced topics. A set of rules that governs the communications between computers on a network is call a protocol. These rules include guidelines that are use to regulate the features of a network. Network features include a method of access, allowed and possible physical topologies, cable types and data transfer speed etc.

HTTP & Web servers

The Hypertext Transfer Protocol(HTTP) is a networking protocol for hypermedia, collaborative and distributed information systems. HTTP refers to the foundation of data communication for the WWW that is the World Wide Web.

on the other hand, Web Servers are nothing but the computers that give web pages. All Web servers have a domain name and IP address. For example, if you enter the URL http://www.canva.com/download.html in your browser, this will send a request to the webserver. The server then fetches the pages named download.html and sends it to your browser. 

A proxy server is a server that is a mediator between the real web server and a client application like the web browser. Proxy server captures all requests to the real server to check if it can be fulfilled the requests on its own. If not, it broadcasts the request to the actual server.

Socket Overview: 

A network socket can be compare up with the electrical sockets because of similarities between them. In electrical sockets, different plugs around the network have a standard way of delivering their payload. We can plugin anything that understands the protocol to the socket and can start communication.

We can attach any device to the socket and use it. The bill is generate up for you and send to your address. Here you are connect with the electrical network present outside. The bills are generat as per usage. The same kind of idea applies to network sockets except for TCP/IP packets and IP address.In-network sockets we talk about TCP/IP packets and IP addresses instead of electrons and street addresses.

  • Internet Protocol(IP) is a low-level routing protocol that divides data into small packets and sends them to an address across a network. It does not guarantee to deliver said packets to the destination.
  • Another protocol is the Transmission Control Protocol(TCP) is a higher-level protocol. It robustly strings together these packets, sorts and retransmits them. This protocol ensures the necessary reliability for transmitting data.
  • The third protocol is User Datagram Protocol(UDP) is the next to TCP. It supports fast, connectionless, unreliable transport of data packets.

Client/Server | Java Networking

  • The client-server model is a networking model where one or more machines act as a server or main machine and the remaining machines are refer clients who get service from the server. This term is well refer to us in the context of networking. It is quite simple to understand. 
  • A server is anything which has got some resources that can be shared. The compute servers provide computing power. There are different types of servers.
  • A client is any other entity that wants to get access to a particular server. Client and server interaction is similar to the interaction between a lamp and an electrical socket. The power grid of the house can be refer to as a server, and the lamp can be called a power client.
  • One of the important features of networking is that the server is a permanently available resource, while the client is free to unplug any time or after it is has been served.
  • The Barkerey sockets put forth an idea that socket allows a single computer to serve many different clients at a time and that to it serves different types of information.
  • This feature is manage by the introduction of the concept refer as port.
  • A server process is in “listen” mode to a port till the client connects to it. A server is permit to accept multiple clients connected to the same port number with each unique session.

Reserved Sockets | Java Networking

  • The sockets which are reserved by specific protocols for communication are reserved sockets. Once the connection is ready a higher-level protocol is in use. This is dependent on the port which you are using. TCP/IP uses or reserves the lower 1024 ports for specific protocols.
  • An example related to the port number are as follows:
  1. FTP – Port no. 21
  2. TELNET- Port no. 23
  3. E-mail – Port no. 25
  4. Finger- Port no. 79
  5. HTTP- Port no. 80
  6. Netnews- Port no. 119 etc.

Protocol decides how a client should interact with the port.

  • Let us understand this example. The HTTP is the protocol that web browsers and servers use to transfer hypertext pages and images.
  • When a request is sent by the client for a particular file from an HTTP server, it is called hit.

Internet Addressing | Java Networking

  • Internet addressing is a very interesting concept to know. Every machine or computer on the internet has an address. An internet address is a unique number that identifies each computer on the network or internet.
  • An IP address is compose of 32 bits. We always refer to them as a sequence of four numbers between 0 and 255 which separates by(.) Dots.
  • This helps in remembering them easily as they are randomly assign up. Their arrangement does this hierarchically. The first few bits define the class of the network. The classes of the network are letter like A, B, C, D, or E, which are represents in the address.
  • It is seen that most of the internet users are on the class C network. The reason is, there are millions of networking class C. The first byte of the class C network is between 192 and 224.
  • The last byte identifies an individual computer among the 256 allowed on a single class C network. This scheme allows billions of devices to exist in class C networks.
  • The four names of the IP address exactly describes a network hierarchy from left to right.. Domain Name describes the location of a machine in the namespace from right to left.

Java and the Net:

Java is mainly known as an internet language because it supports features required by the Internet-related features very strongly.Java supports TCP/IP both by extending the readily established stream input/ output interface.

Below are the interfaces of the java.net package:

  1. ContentHandlerFactory 
  2. SocketImplFactory
  3. URLStreamHandlerFactory
  4. FileNameMap
  5. SocketOptions.

Factory Methods:

  • The InetAddress class does not have visible constructors. Object creation for InetAddress class, it’s factory methods can be used.
  • When Factory methods are used, the static methods from the class return an instance of that class.
  • It results in overloading a constructor with a different parameter list while having distinct method names that give a clear result.
  • The InetAddress gives three methods:
getLocalHost( ), getByName( ) and getAllByName( )which are used to create the instance of InetAddress class.
  • The getLocalHost( ):  This method is useful in getting objects of InetAddress class. This object represents the localhost.
  • The getByName( )  This method gives an InetAddress of the hostname passed to it.
  • If both of these methods are not able to work, then they throw an exception called UnknownHostExcepetion.
  • The getAllByName( ) is a method that returns an array of InetAddresses.
  • In case if it can’t resolve the name to at least one address, it throws UnknownHostException.
  • The following example displays the addresses and names of the local machine and two internet websites.
  • Program: to demonstrate InetAddress class.

URL (Uniform Resource Locator) | Java Networking

  • URL is Uniform Resource Locator. A URL is a formatted text string used by email clients, web browsers and different types of software to recognize a network resource on the Internet.
  • URL strong encompasses three parts:
  1. Network protocol
  2. Hostname or address
  3. File or resource location
  • It is about WWW, the World Wide Web.

URL Connection:

URLConnection is a class used for accessing the attributes of a remote resource. Once we establish the connection to a remote server, we can use URLConnection to inspect the properties of the remote object before actually by the HTTP protocol specification and it only makes sense for URL objects that are using the HTTP protocol. thus, URLConnection methods are listed  below:

Here we will check the most useful elements of URLConnection.

Consider the following example to understand URL connection in a better way:

Datagrams: 

TCP/IP provides data in the form of packets. The stream through which it sends the data is reliable, serialized, and predictable. thus, This is not easy to achieve. TCP has got several complicated algorithms to deal with congestion control over the crowded networks.

Datagrams are nothing but bundles of information passed between machines. Datagrams send data properly but it does not ensure the receiving of data.

Two classes of datagrams are used by java to implement on top of the UDP protocol. Those two classes are DatagramPacket whose object is the data container and another class is DatagramSocket which is the mechanism used to send or receive the DatagramPackets.

Datagram Packets:

  • DatagramPackets could be created with any one of its four constructors. They are listed below: 
  • The above first constructor specifies a buffer that will receive data and the size of a packet. Its purpose is to receive data over a DatagramPacket.
  • The second form of constructor facilitates to specify an offset into the buffer at which data will be stored.

Datagram Server and Client:

The example below shows a very simple networked communication between client and server. In the program, messages are typed into the window at the server and written across the network to the client-side where they are printed or displayed.

Program: to demonstrate the datagrams server and client.

  • The program above has the limitation that the constructor of DatagramSocket will run between two ports on the local machine.
  • To use the program, run java Write Server in one window. This is going to be a client. Then run java WriterS server.

Conclusion | Java Networking:

Networking in Java makes use of a socket for interprocess communication between hosts where sockets act as the endpoint of the interprocess communication. Here sockets can also be termed as network sockets or Internet sockets since communication between computers is based on Internet protocol.

written by: Vickky Bopche

reviewed by: Soutik Maity

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

Your email address will not be published. Required fields are marked *