0% found this document useful (0 votes)
5 views25 pages

14.0 JAVA Networking

The document provides an overview of networking in Java, explaining key concepts such as networking, protocols (TCP and UDP), and important terminologies like IP address, port number, and MAC address. It details how Java facilitates network programming through the java.net package, including the use of sockets for communication between devices. Additionally, it outlines the steps to create a basic networking application using the ServerSocket class to echo data sent by a client.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views25 pages

14.0 JAVA Networking

The document provides an overview of networking in Java, explaining key concepts such as networking, protocols (TCP and UDP), and important terminologies like IP address, port number, and MAC address. It details how Java facilitates network programming through the java.net package, including the use of sockets for communication between devices. Additionally, it outlines the steps to create a basic networking application using the ServerSocket class to echo data sent by a client.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Networking With JAVA

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.

• JAVA encapsulates classes and interfaces to allow


low-level communication details.
• JAVA Networking is a notion of connecting two or
more computing devices together to share the
resources.

• Java program communicates over the network at


the application layer.

• java.net package is useful for all the JAVA


networking classes and interfaces.
The java.net package provides support for two
protocols. They are as follows:

TCP − Transmission Control Protocol allows reliable


communication between two applications.
TCP is typically used over the Internet Protocol, which
is referred to as TCP/IP.

UDP − User Datagram Protocol is a connection-less


protocol that allows packets of data to be transmitted
between applications.
Networking Terminologies
The widely used Java networking terminologies used are as
follows:

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 MAC address is basically a hardware identification number which uniquely


identifies each device on a network.

• For example, an Ethernet card may have a MAC address of 00:0d:83:b1:c0:8e.

Connection-oriented and connection-less protocol

• In the connection-oriented protocol, acknowledgment is sent by the receiver. So it


is reliable but slow.
 The example of a connection-oriented protocol is TCP.

• But, in the connection-less protocol, acknowledgment is not sent by the receiver.


So it is not reliable but fast.
 The example of a connection-less protocol is UDP.
Socket

• A socket in Java is one endpoint of a two-way communication link between two


programs running on the network.

• 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:

• ServerSocket class is for servers


• The socket class is for the client
• When two devices are connected through “Socket”
programming, their data channels (streams) are connected in
the following way:
• A client writes to the “output” stream that is connected to
the “input” stream of the server which reads it and vice-
versa.
URL Class
• Java URL class mainly deals with URL(Uniform Resource
Locator) which is used to identify the resources on the
internet.
• For Example: https://www.timesofindia.com

• URL Class comprises of various methods to return the URL


information of a particular website.

getProtocol() : Returns protocol of URL


getHost() : Returns hostname(domain name) of the
specified URL
getPort() : Returns port number of the URL
specified
getFile() : Returns filename of the URL
Creating a Basic Networking
Application
Objective :

We will create a simple networking application that


creates a basic server using ServerSocket class
and accepts a single client connection.

It then reads data sent by the client from its input


stream and sends it back to the client through the
output stream
Steps :

• 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.

You might also like