14.0 JAVA Networking
14.0 JAVA Networking
What is Networking ?
• When computing devices such
as laptops, desktops, servers,
smartphones, and tablets and
an eternally-expanding
arrangement of IoT gadgets such
as cameras, door locks,
doorbells, refrigerators,
audio/visual systems,
thermostats, and various
sensors are sharing information
and data with each other is
known as Networking.
• Network programming refers to writing programs
that execute across multiple devices (computers), in
which the devices are connected to each other via a
network.
1) IP Address
2) Protocol
3) Port Number
4) MAC Address
5) Connection-oriented and connection-
less protocol
6) Socket
IP Address
• The IP address is a unique number assigned to a node of a network e.g.
192.168.0.1.
• It is composed of octets that range from 0 to 255.
Octets
Protocol
A protocol is a set of rules followed for communication. For example:
• TCP
• FTP
• Telnet
• SMTP
• POP etc.
Port Number
• The port number uniquely identifies different applications.
• It acts as a communication endpoint between applications.
• To communicate between two applications, the port number is used along with an
IP Address.
MAC Address
• A socket is bound to a port number so that the TCP layer can identify the
application that data is destined to be sent to.
Socket and Socket Server Class
• A socket is used to establish a connection through the use of
the port, which is a numbered socket on a particular
machine.
• Socket basically provides a communication mechanism
between two computers using Transmission Control Protocol.
• There are two types of sockets as follows:
• We will write JAVA code to create server that will echo data
sent by the client
• We will use “telnet” program to send data to the JAVA
server that will read the data back to us.
• (NOTE: Install “telnet” from Control Panel Windows
Features, if it’s not installed already”)
#3-#7 These import statements bring in the required classes for handling
input/output streams, socket communication, and exception handling.
#16-#17 This line creates a “serverSocket” object that listens on port 8080. The server
will wait for incoming client connections on this port.
#20 The accept() method blocks execution until a client connects to the server.
When a client connects to the server, “serverSocket.accept()” returns a new “socket”
object that represents the newly established connection to the client.
This object will be used to communicate with the specific client that connected to
the server.
We will associate “Input” and “Output” streams with this socket to send / receive
data
#23 - #27 This code obtains the input and output streams associated with the client's
socket.
The input stream is used to read data sent by the client, and the output stream is used to
send data back to the client.
#32 - #35 This loop continuously reads data from the client's input stream byte by byte
until it reaches the end of the stream (when read() returns -1).
• It then writes the received data back to the client through the output stream.
• Effectively, this code echoes data back to the client.
#38 - #39 Finally, the code closes both the client socket and the server socket to
release the network resources and terminate the server.