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
Network programming in java - PPT
PPTX
java interface and packages
PPTX
Event Handling in java
PPTX
Networking in Java
PPTX
JAVA AWT
PPT
Introduction to java beans
PPTX
MULTI THREADING IN JAVA
PPTX
Assemblies
Network programming in java - PPT
java interface and packages
Event Handling in java
Networking in Java
JAVA AWT
Introduction to java beans
MULTI THREADING IN JAVA
Assemblies

What's hot (20)

PPTX
Cloud computing using Eucalyptus
PPS
Procedures functions structures in VB.Net
PPTX
Static dynamic and active web pages
PPTX
Introduction to java
PPTX
Java - Sockets
PPTX
Java socket programming
PPTX
Core java complete ppt(note)
PPTX
Servlets
PPT
Java features
PPT
Subnetting
PPTX
Features of java
PPTX
Java package
PDF
JavaScript - Chapter 12 - Document Object Model
PDF
Java I/o streams
PPT
Architecture of .net framework
PPTX
Training on Core java | PPT Presentation | Shravan Sanidhya
PPTX
Introduction to java
PDF
PPTX
Operating system overview concepts ppt
PPS
Java rmi
Cloud computing using Eucalyptus
Procedures functions structures in VB.Net
Static dynamic and active web pages
Introduction to java
Java - Sockets
Java socket programming
Core java complete ppt(note)
Servlets
Java features
Subnetting
Features of java
Java package
JavaScript - Chapter 12 - Document Object Model
Java I/o streams
Architecture of .net framework
Training on Core java | PPT Presentation | Shravan Sanidhya
Introduction to java
Operating system overview concepts ppt
Java rmi
Ad

Similar to Networking in java, Advanced programming (20)

PPTX
Java Network Programming.pptx
PPT
Networking Java Socket Programming
PDF
Socket programming
PPT
Java Networking
PPT
Advanced Java Topics
PDF
Socket Programming by Rajkumar Buyya
PPT
Network programming in Java
PPTX
5_6278455688045789623.pptx
PPTX
Client server chat application
PPTX
Networking.pptx
PDF
28 networking
PPT
Socket Programming - nitish nagar
PPTX
socket-programming.pptx
PPT
Sockets
PDF
How a network connection is created A network connection is initi.pdf
PPTX
Chuong5_Networking_updated.Networking_updatedpptx
PPTX
PDF
sockets SMTP Bmsce ppt information science and engineering
PPT
Java networking
PPT
Multi user chat system using java
Java Network Programming.pptx
Networking Java Socket Programming
Socket programming
Java Networking
Advanced Java Topics
Socket Programming by Rajkumar Buyya
Network programming in Java
5_6278455688045789623.pptx
Client server chat application
Networking.pptx
28 networking
Socket Programming - nitish nagar
socket-programming.pptx
Sockets
How a network connection is created A network connection is initi.pdf
Chuong5_Networking_updated.Networking_updatedpptx
sockets SMTP Bmsce ppt information science and engineering
Java networking
Multi user chat system using java
Ad

More from Gera Paulos (20)

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

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Advanced IT Governance
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Electronic commerce courselecture one. Pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
KodekX | Application Modernization Development
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPT
Teaching material agriculture food technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
NewMind AI Weekly Chronicles - August'25 Week I
Advanced IT Governance
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
Chapter 2 Digital Image Fundamentals.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Electronic commerce courselecture one. Pdf
NewMind AI Monthly Chronicles - July 2025
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Advanced Soft Computing BINUS July 2025.pdf
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
KodekX | Application Modernization Development
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Teaching material agriculture food technology
Reach Out and Touch Someone: Haptics and Empathic Computing
Dropbox Q2 2025 Financial Results & Investor Presentation

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]