Common Java Networking Terminology
IP Address
An IP address is a unique number assigned to a network node. IP is an abbreviation for "Internet Protocol." It is made up of octets ranging from 0 to 255.
Example. 192.168.1.1
Protocol
A network protocol is a set of rules, standards, and data structures that govern how devices communicate data over networks.
There are many types of protocols, for example, TCP, UDP, FTP, TELNET, POP, etc.
Port Number
In computer networking, a port number is a piece of addressing information used to identify message senders and receivers. Several port numbers are used to define which protocol incoming communication should be directed to. When an Internet or other network message arrives at a server, the port number represents the exact process to which it is to be passed. Each protocol has its own port, which serves as a communication endpoint.
Mac Address
The term MAC address refers to the Media Access Control address. A MAC (Media Access Control) address is a unique identifier for a network interface card (NIC) (Network Interface Controller). A network node may have many NICs, each with a specific MAC address. A MAC address is a one-of-a-kind number used to track a device in a network.
For example, 00:0d:83::b1:c0:8e.
Socket
A socket is one endpoint of a two-way communication connection between two network-running programs. The socket mechanism implements inter-process communication (IPC) by establishing named contact points between which communication takes place. A socket is associated with a port number so that the TCP layer can identify the application to which data is to be transmitted.
Connection-Oriented and Connection-Less Protocol
Before initiating communication in a connection-oriented protocol, the user must establish a connection. When the connection is established, the user can send the message or information, then disconnect. As a result, it is dependable yet slow.
The data in a connectionless protocol is delivered in a single path from source to destination without checking to see if the destination is still present or ready to accept the message. As a result, it is quick but unreliable. The connectionless protocol does not require authentication.
Java .net Package
Java.net package can be divided into two types based on the complexity of the java.net API.
Low-level API
It is concerned with the abstractions of addresses, sockets, and interfaces.
High-level API
It is concerned with the abstraction of URIs, URLs, and Connections.
The java.net Supports two well-known protocols such as:
TCP (Transmission Control Protocol )
TCP is used in the TCP IP model to transport data between applications and devices on a network. It is intended to break down a message, such as an email, into data packets for the message to reach its destination successfully and as rapidly as possible.
UDP (User Datagram Protocol )
UDP is a protocol that is datagram-centric. It is used for network transmissions such as broadcast and multicast. UDP is an abbreviation for User Datagram Protocol (A datagram is a transfer unit associated with a packet-switched network.) The UDP protocol is comparable to TCP in many ways, but it eliminates all error-checking, back-and-forth communication, and deliverability.
Java Networking Classes
Java.net API provides many classes that allow easy access to network resources.
Some of the commonly used Networking Classes are:
- Authenticator
- CacheRequest
- CookieHandler
- CookieManager
- DatagramPacket
- InetAddress
- Server Socket
- Socket
- DatagramSocket
- Proxy
- URL
- URLConnection
Java Networking Interface
Java.net API also provides many interfaces that allow easy access to network resources.
Some of the commonly used Networking Interface are:
CookiePolicy
The CookiePolicy interface in Java determines whether cookies should be accepted or rejected by the HTTP protocol handler. It provides a flexible way to manage cookies based on custom rules or predefined policies like ACCEPT_ALL or ACCEPT_NONE.
ContentHandlerFactory
ContentHandlerFactory is an interface that creates ContentHandler objects, which are responsible for handling the content of a specific MIME type. This interface allows for the dynamic association of content handlers with data streams.
CookieStore
The CookieStore interface represents a storage mechanism for cookies. It allows for adding, retrieving, and removing cookies from a collection. It's commonly used to manage cookies between HTTP requests and responses.
FileNameMap
The FileNameMap interface is used to map file extensions to MIME types. It provides a method getContentTypeFor that returns the MIME type for a given file name, aiding in content type determination.
DatagramSocketImplFactory
DatagramSocketImplFactory is an interface for creating DatagramSocketImpl instances, which handle the low-level implementation details of DatagramSocket and MulticastSocket. It allows for customizing the creation of these socket implementations.
SocketOptions
The SocketOptions interface defines constants and methods to configure socket options, such as enabling or disabling the TCP_NODELAY option. It allows for fine-tuning the behavior of sockets for network communication.
SocketOption<T>
SocketOption<T> represents a type-safe socket option that can be used with a SocketChannel, DatagramChannel, or ServerSocketChannel. It allows for configuring options like buffer sizes and timeouts on network sockets.
URLStreamHandlerFactory
The URLStreamHandlerFactory interface is used to create URLStreamHandler objects, which are responsible for handling the communication with different types of URLs. It allows for the dynamic association of protocols with URL handlers.
SocketImplFactory
SocketImplFactory is an interface for creating SocketImpl instances, which define the underlying implementation of sockets. This interface allows for customizing the behavior of sockets in network communication.
ProtocolFamily
The ProtocolFamily interface represents a family of communication protocols, such as INET for IPv4 or INET6 for IPv6. It is used when creating sockets to specify the desired protocol family.
Also read, Hashcode Method in Java
Advantages of Java Networking
There are several advantages of Java networking:
-
Platform Independence: Java’s networking capabilities are platform-independent, allowing developers to create applications that work across different operating systems without modification.
-
Ease of Use: The Java java.net package provides a rich set of classes and interfaces, simplifying the process of network programming with easy-to-understand APIs.
-
Robust Security: Java offers built-in security features, such as SSL (Secure Sockets Layer) and authentication mechanisms, ensuring secure communication over networks.
-
Multithreading Support: Java's multithreading capabilities allow network applications to handle multiple connections simultaneously, improving performance and responsiveness.
-
Standard Protocol Support: Java supports a wide range of standard protocols like TCP/IP, UDP, HTTP, and FTP, making it versatile for different networking needs.
-
Extensive Library: The comprehensive java.net library includes tools for working with sockets, URLs, and datagrams, providing a strong foundation for building complex networked applications.
-
Object Serialization: Java supports object serialization, enabling objects to be sent over the network and reconstructed at the destination, facilitating distributed computing.
-
Portability: Java's networking code can be easily ported across different environments, reducing the need for rewriting code for different platforms.
-
Scalability: Java’s networking APIs support the development of scalable applications that can handle increasing loads, making it suitable for both small and large-scale systems.
-
Community Support: With a vast community of developers and extensive documentation, Java networking enjoys strong support, making it easier to find solutions and best practices.
Frequently Asked Questions
Which class is used to create sockets in java?
The "java.net.Socket" class defines a socket, whereas the "java.net.ServerSocket" class implements a tool for the server application to host clients and establish connections with them.
What is TCP/IP?
TCP is an abbreviation for Transmission Control Protocol, whereas IP is an abbreviation for Internet Protocol. TCP is used in the TCP IP paradigm to transport data between applications and devices on a network. It is intended to break down communication, such as an email, into data packets for the message to reach its destination successfully and as rapidly as possible.
How to include java.net API?
Use this at the header.
​​import java.net.*;
Conclusion
Today we learned about basic network terminologies and how to use them in Java.
We learned about java.net API and its various classes and interfaces.
If you wish to learn more about Java from experts, you can check out the Basics of Java with Data Structures and Algorithms course by coding ninjas.
Happy Coding!