SlideShare a Scribd company logo
3
Most read
6
Most read
11
Most read
Advanced Programming
Networking in Java
Gera
2020
1
Networking in Java
•The Java programming language is supposed to
become the premier tool for connecting
computers over the Internet and corporate
intranets and, in this realm, Java mostly lives up to
the hype.
•If you are accustomed to programming network
connections in C or C++, you will be pleasantly
surprised at how easy it is to program them in the
Java programming language.
2
Networking in Java…
•The term network programming refers to writing
programs that execute across multiple devices
(computers), in which the devices are all
connected to each other using a network.
•The java.net package of the J2SE APIs contains a
collection of classes and interfaces that provide
the low-level communication details, allowing you
to write programs that focus on solving the
problem at hand.
3
Networking in Java…
• The java.net package provides support for the two
common network protocols:
• TCP: TCP stands for Transmission Control Protocol,
which allows for reliable communication between
two applications. TCP is typically used over the
Internet Protocol, which is referred to as TCP/IP.
• UDP: UDP stands for User Datagram Protocol, a
connection-less protocol that allows for packets of
data to be transmitted between applications.
4
Implementing Servers in Java
• To implement a basic network client that receives data from the
Net, let’s implement a simple server that can send information
out to the Net.
• Once you start the server program, it waits for some client to
attach to its port.
• We chose port number 8189, which is not used by any of the
standard services.
• The ServerSocket class is used to establish a socket.
• In our case, the command establishes a server that monitors port
8189
• ServerSocket s = new ServerSocker (8189);
5
Socket Programming
•Sockets provide the communication mechanism
between two computers using TCP.
•A client program creates a socket on its end of the
communication and attempts to connect that
socket to a server.
•When the connection is made, the server creates a
socket object on its end of the communication.
• The client and server can now communicate by
writing to and reading from the socket. 6
Socket Programming…
• The java.net.Socket class represents a socket, and the
java.net.ServerSocket class provides a mechanism for the
server program to listen for clients and establish
connections with them.
• The following steps occur when establishing a TCP
connection between two computers using sockets:
• The server instantiates a ServerSocket object, denoting which
port number communication is to occur on.
• The server invokes the accept() method of the ServerSocket
class. This method waits until a client connects to the server
on the given port.
7
Socket Programming…
• After the server is waiting, a client instantiates a Socket
object, specifying the server name and port number to
connect to.
• The constructor of the Socket class attempts to connect the
client to the specified server and port number. If
communication is established, the client now has a Socket
object capable of communicating with the server.
• On the server side, the accept() method returns a reference to
a new socket on the server that is connected to the client's
socket.
8
Socket Programming…
• After the connections are established, communication
can occur using I/O streams. Each socket has both an
OutputStream and an InputStream. The client's
OutputStream is connected to the server's InputStream,
and the client's InputStream is connected to the server's
OutputStream.
• The following GreetingServer program is an example of a
server application that uses the Socket class to listen for
clients on a port number specified by a command-line
argument:
9
Manipulating URLs…
10
Compile client and Server and then start server as follows:
Manipulating URLs
• The Internet offers many protocols.
• The Hypertext Transfer Protocol (HTTP), which forms the basis of
the World Wide Web, uses URIs (Uniform Resource Identifiers)
to identify data on the Internet.
• URIs that specify the locations of documents are called URLs
(Uniform Resource Locators).
• Common URLs refer to files or directories and can reference
objects that perform complex tasks, such as database lookups
and Internet searches.
• If you know the HTTP URL of a publicly available HTML document
anywhere on the web, you can access it through HTTP.
11
Manipulating URLs…
•Java makes it easy to manipulate URLs.
•When you use a URL that refers to the exact
location of a resource (e.g., a web page) as an
argument to the showDocument method of
interface AppletContext, the browser in which
the applet is executing will display that resource.
12
Manipulating URLs…
URL Processing
•URL stands for Uniform Resource Locator and
represents a resource on the World Wide Web,
such as a Web page or FTP directory.
•The following URLDemo program demonstrates
the various parts of a URL.
•A URL is entered on the command line, and the
URLDemo program outputs each part of the given
URL. 13
Manipulating URLs…
14
A sample run of the program would produce the
following result:
Manipulating URLs…
URL Connections Class Methods:
•The openConnection() method returns a
java.net.URLConnection, an abstract class whose
subclasses represent the various types of URL
connections.
• The following URLConnectionDemo program connects to
a URL entered from the command line.
• If the URL represents an HTTP resource, the connection is
cast to HttpURLConnection, and the data in the resource
is read one line at a time. 15
Manipulating URLs…
16
A sample run of the program would produce the
following result:
References
➢S. Horstmann and Gary Cornell, Core Java 2 – Volume II-
Advanced Features, Sun Microsystems Press
➢Harvey M. Deitel and Paul J. Deitel, Java How to
Program, Deitel & Associates
➢Tutorials Point “Java Tutorial,” tutorialspoint.com
17
Gerabirhan Paulos
ToCourseInfo@gmail.com

More Related Content

PPTX
Threads in JAVA
Haldia Institute of Technology
 
PPTX
UNIT-3 Servlet
ssbd6985
 
PPS
Wrapper class
kamal kotecha
 
PPT
Server Controls of ASP.Net
Hitesh Santani
 
PPTX
Namespaces in C#
yogita kachve
 
PPS
Java Exception handling
kamal kotecha
 
PPTX
Event handling
swapnac12
 
PPT
Collection Framework in java
CPD INDIA
 
UNIT-3 Servlet
ssbd6985
 
Wrapper class
kamal kotecha
 
Server Controls of ASP.Net
Hitesh Santani
 
Namespaces in C#
yogita kachve
 
Java Exception handling
kamal kotecha
 
Event handling
swapnac12
 
Collection Framework in java
CPD INDIA
 

What's hot (20)

PDF
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
PPT
Intro to web services
Neil Ghosh
 
PPTX
Java Server Pages(jsp)
Manisha Keim
 
PPT
Shell and its types in LINUX
SHUBHA CHATURVEDI
 
PPSX
Exception Handling
Reddhi Basu
 
PPTX
OOP Introduction with java programming language
Md.Al-imran Roton
 
PPT
Introduction to .NET Framework
Raghuveer Guthikonda
 
PDF
Advanced perl finer points ,pack&unpack,eval,files
Shankar D
 
DOCX
Basic java important interview questions and answers to secure a job
Garuda Trainings
 
PDF
Basics of JavaScript
Bala Narayanan
 
PPTX
Exception handling
Abhishek Pachisia
 
PPT
Working with color and font
myrajendra
 
PPTX
Classes objects in java
Madishetty Prathibha
 
PPTX
Multithreading in java
Raghu nath
 
PDF
Java threads
Prabhakaran V M
 
PDF
Java threading
Chinh Ngo Nguyen
 
PPTX
Hibernate ppt
Aneega
 
PPT
Js ppt
Rakhi Thota
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
Intro to web services
Neil Ghosh
 
Java Server Pages(jsp)
Manisha Keim
 
Shell and its types in LINUX
SHUBHA CHATURVEDI
 
Exception Handling
Reddhi Basu
 
OOP Introduction with java programming language
Md.Al-imran Roton
 
Introduction to .NET Framework
Raghuveer Guthikonda
 
Advanced perl finer points ,pack&unpack,eval,files
Shankar D
 
Basic java important interview questions and answers to secure a job
Garuda Trainings
 
Basics of JavaScript
Bala Narayanan
 
Exception handling
Abhishek Pachisia
 
Working with color and font
myrajendra
 
Classes objects in java
Madishetty Prathibha
 
Multithreading in java
Raghu nath
 
Java threads
Prabhakaran V M
 
Java threading
Chinh Ngo Nguyen
 
Hibernate ppt
Aneega
 
Js ppt
Rakhi Thota
 
Ad

Similar to Networking in java, Advanced programming (20)

PPT
Java networking
Arati Gadgil
 
PDF
Unit 6 - Netwohhhhhddddrking in Java.pdf
kassyemariyam21
 
DOC
T2
Mo Ch
 
PPT
Networking.ppt(client/server, socket) uses in program
govindjha339843
 
PPT
Md13 networking
Rakesh Madugula
 
PPT
Networking Java Socket Programming
Mousmi Pawar
 
PDF
Socket Programming by Rajkumar Buyya
iDhawalVaja
 
PPTX
Chapter 4
Ebisa Bekele
 
PPT
Advanced Java Topics
Salahaddin University-Erbil
 
PPTX
Learnadvancedjavaprogramming 131217055604-phpapp02
Hardeep Kaur
 
PPTX
Java networking
ssuser3a47cb
 
PPT
Java Network Programming 1197644949195966 4
chendi.bhargavi
 
PPT
Java Network Programming
backdoor
 
DOCX
Unit8 java
mrecedu
 
PPTX
Networking.pptx
Esubesisay
 
PPTX
5_6278455688045789623.pptx
EliasPetros
 
PPT
Network Programming in Java
Tushar B Kute
 
PPT
Socket Programming - nitish nagar
Nitish Nagar
 
PPT
Network programming in Java
Tushar B Kute
 
PDF
Socket programming
Padmavathione
 
Java networking
Arati Gadgil
 
Unit 6 - Netwohhhhhddddrking in Java.pdf
kassyemariyam21
 
T2
Mo Ch
 
Networking.ppt(client/server, socket) uses in program
govindjha339843
 
Md13 networking
Rakesh Madugula
 
Networking Java Socket Programming
Mousmi Pawar
 
Socket Programming by Rajkumar Buyya
iDhawalVaja
 
Chapter 4
Ebisa Bekele
 
Advanced Java Topics
Salahaddin University-Erbil
 
Learnadvancedjavaprogramming 131217055604-phpapp02
Hardeep Kaur
 
Java networking
ssuser3a47cb
 
Java Network Programming 1197644949195966 4
chendi.bhargavi
 
Java Network Programming
backdoor
 
Unit8 java
mrecedu
 
Networking.pptx
Esubesisay
 
5_6278455688045789623.pptx
EliasPetros
 
Network Programming in Java
Tushar B Kute
 
Socket Programming - nitish nagar
Nitish Nagar
 
Network programming in Java
Tushar B Kute
 
Socket programming
Padmavathione
 
Ad

More from Gera Paulos (20)

PDF
Remote Method Invocation, Advanced programming
Gera Paulos
 
PDF
Multi Threading Concept (Advanced programming)
Gera Paulos
 
PDF
Java Database Connectivity (Advanced programming)
Gera Paulos
 
PDF
Servlets as introduction (Advanced programming)
Gera Paulos
 
PDF
Advanced programming ch2
Gera Paulos
 
PDF
Advanced programming ch1
Gera Paulos
 
PDF
Image restoration and enhancement #2
Gera Paulos
 
PDF
Introduction to digital image processing #1
Gera Paulos
 
PDF
Implement maintenance procedures
Gera Paulos
 
PDF
Maintain equipment and consumables
Gera Paulos
 
PDF
Care for network and computer hardware
Gera Paulos
 
PDF
Update and document operational procedures
Gera Paulos
 
PDF
Apply quality control
Gera Paulos
 
PDF
Monitoring implementation of work plan
Gera Paulos
 
PDF
Provide first level remote help desk support
Gera Paulos
 
PDF
Configuring and administrate server
Gera Paulos
 
PDF
Identifying and resolving network problems
Gera Paulos
 
PDF
Conduct / facilitate user training
Gera Paulos
 
PDF
Monitor and administer system and network
Gera Paulos
 
PDF
Creating technical documents
Gera Paulos
 
Remote Method Invocation, Advanced programming
Gera Paulos
 
Multi Threading Concept (Advanced programming)
Gera Paulos
 
Java Database Connectivity (Advanced programming)
Gera Paulos
 
Servlets as introduction (Advanced programming)
Gera Paulos
 
Advanced programming ch2
Gera Paulos
 
Advanced programming ch1
Gera Paulos
 
Image restoration and enhancement #2
Gera Paulos
 
Introduction to digital image processing #1
Gera Paulos
 
Implement maintenance procedures
Gera Paulos
 
Maintain equipment and consumables
Gera Paulos
 
Care for network and computer hardware
Gera Paulos
 
Update and document operational procedures
Gera Paulos
 
Apply quality control
Gera Paulos
 
Monitoring implementation of work plan
Gera Paulos
 
Provide first level remote help desk support
Gera Paulos
 
Configuring and administrate server
Gera Paulos
 
Identifying and resolving network problems
Gera Paulos
 
Conduct / facilitate user training
Gera Paulos
 
Monitor and administer system and network
Gera Paulos
 
Creating technical documents
Gera Paulos
 

Recently uploaded (20)

PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Software Development Company | KodekX
KodekX
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Software Development Company | KodekX
KodekX
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 

Networking in java, Advanced programming

  • 2. Networking in Java •The Java programming language is supposed to become the premier tool for connecting computers over the Internet and corporate intranets and, in this realm, Java mostly lives up to the hype. •If you are accustomed to programming network connections in C or C++, you will be pleasantly surprised at how easy it is to program them in the Java programming language. 2
  • 3. Networking in Java… •The term network programming refers to writing programs that execute across multiple devices (computers), in which the devices are all connected to each other using a network. •The java.net package of the J2SE APIs contains a collection of classes and interfaces that provide the low-level communication details, allowing you to write programs that focus on solving the problem at hand. 3
  • 4. Networking in Java… • The java.net package provides support for the two common network protocols: • TCP: TCP stands for Transmission Control Protocol, which allows for reliable communication between two applications. TCP is typically used over the Internet Protocol, which is referred to as TCP/IP. • UDP: UDP stands for User Datagram Protocol, a connection-less protocol that allows for packets of data to be transmitted between applications. 4
  • 5. Implementing Servers in Java • To implement a basic network client that receives data from the Net, let’s implement a simple server that can send information out to the Net. • Once you start the server program, it waits for some client to attach to its port. • We chose port number 8189, which is not used by any of the standard services. • The ServerSocket class is used to establish a socket. • In our case, the command establishes a server that monitors port 8189 • ServerSocket s = new ServerSocker (8189); 5
  • 6. Socket Programming •Sockets provide the communication mechanism between two computers using TCP. •A client program creates a socket on its end of the communication and attempts to connect that socket to a server. •When the connection is made, the server creates a socket object on its end of the communication. • The client and server can now communicate by writing to and reading from the socket. 6
  • 7. Socket Programming… • The java.net.Socket class represents a socket, and the java.net.ServerSocket class provides a mechanism for the server program to listen for clients and establish connections with them. • The following steps occur when establishing a TCP connection between two computers using sockets: • The server instantiates a ServerSocket object, denoting which port number communication is to occur on. • The server invokes the accept() method of the ServerSocket class. This method waits until a client connects to the server on the given port. 7
  • 8. Socket Programming… • After the server is waiting, a client instantiates a Socket object, specifying the server name and port number to connect to. • The constructor of the Socket class attempts to connect the client to the specified server and port number. If communication is established, the client now has a Socket object capable of communicating with the server. • On the server side, the accept() method returns a reference to a new socket on the server that is connected to the client's socket. 8
  • 9. Socket Programming… • After the connections are established, communication can occur using I/O streams. Each socket has both an OutputStream and an InputStream. The client's OutputStream is connected to the server's InputStream, and the client's InputStream is connected to the server's OutputStream. • The following GreetingServer program is an example of a server application that uses the Socket class to listen for clients on a port number specified by a command-line argument: 9
  • 10. Manipulating URLs… 10 Compile client and Server and then start server as follows:
  • 11. Manipulating URLs • The Internet offers many protocols. • The Hypertext Transfer Protocol (HTTP), which forms the basis of the World Wide Web, uses URIs (Uniform Resource Identifiers) to identify data on the Internet. • URIs that specify the locations of documents are called URLs (Uniform Resource Locators). • Common URLs refer to files or directories and can reference objects that perform complex tasks, such as database lookups and Internet searches. • If you know the HTTP URL of a publicly available HTML document anywhere on the web, you can access it through HTTP. 11
  • 12. Manipulating URLs… •Java makes it easy to manipulate URLs. •When you use a URL that refers to the exact location of a resource (e.g., a web page) as an argument to the showDocument method of interface AppletContext, the browser in which the applet is executing will display that resource. 12
  • 13. Manipulating URLs… URL Processing •URL stands for Uniform Resource Locator and represents a resource on the World Wide Web, such as a Web page or FTP directory. •The following URLDemo program demonstrates the various parts of a URL. •A URL is entered on the command line, and the URLDemo program outputs each part of the given URL. 13
  • 14. Manipulating URLs… 14 A sample run of the program would produce the following result:
  • 15. Manipulating URLs… URL Connections Class Methods: •The openConnection() method returns a java.net.URLConnection, an abstract class whose subclasses represent the various types of URL connections. • The following URLConnectionDemo program connects to a URL entered from the command line. • If the URL represents an HTTP resource, the connection is cast to HttpURLConnection, and the data in the resource is read one line at a time. 15
  • 16. Manipulating URLs… 16 A sample run of the program would produce the following result:
  • 17. References ➢S. Horstmann and Gary Cornell, Core Java 2 – Volume II- Advanced Features, Sun Microsystems Press ➢Harvey M. Deitel and Paul J. Deitel, Java How to Program, Deitel & Associates ➢Tutorials Point “Java Tutorial,” tutorialspoint.com 17 Gerabirhan Paulos [email protected]