SlideShare a Scribd company logo
JAVA NETWORKING
CLIENT/SERVER
COMMUNICATION
At a basic level, network-based systems consist of
a server , client , and a media for communication
 A computer running a program that makes a
request for services is called client machine.
 A computer running a program that offers
requested services from one or more clients is
called server machine.
 The media for communication can be wired or
wireless network.

Generally, programs running on client machines
make requests to a program running on a server
machine. They involve networking services
provided by the transport layer, which is part of
the Internet software stack, often called TCP/IP
(Transport Control Protocol/Internet Protocol)
 The transport layer comprises two types of
protocols,


 TCP

(Transport Control Protocol)
 UDP (User Datagram Protocol).


The most widely used programming
interfaces for these protocols are
“sockets”.
TCP is a connection-oriented protocol that
provides a reliable flow of data between two
computers. Example applications that use such
services are HTTP, FTP, and Telnet.
 UDP is a protocol that sends independent packets
of data, called “datagrams”, from one computer to
another with no guarantees about arrival and
sequencing. Example applications that use such
services include Clock server and Ping.
 The TCP and UDP protocols use “ports” to map
incoming data to a particular process running on a
computer.
 Port is represented by a positive (16-bit) integer
value

Some ports have been reserved to support
common/well known services like ftp, http, https
etc.
 User-level process/services generally use port
number value >= 1024.
 Every computer on the Internet is identified by a
unique, 4-byte IP address .
 This is typically written in dotted quad format
like 128.250.25.158 where each byte is an
unsigned value between 0 and 255.
 This representation is not user-friendly because it
does not tell us anything about the content and
then it is difficult to remember. Hence, IP
addresses are mapped to names like
www.google.com, which are easier to remember.
 Domain name servers(DNS) translate these
names to IP addresses.

Computers often need to communicate and
provide more than one type of service or to talk to
multiple hosts/computers at a time. For example,
there may be multiple ftp sessions, web
connections, and chat programs all running at the
same time.
 To distinguish these services, a concept of “port” s,
a logical access point, represented by a 16-bit
integer number is used.
 That means, each service offered by a computer is
uniquely identified by a port number.
 Each Internet packet contains both the
destination host address and the port number on
that host to which the message/request has to be
delivered.
 The host computer dispatches the packets it
receives to programs by looking at the port
numbers specified within the packets.

SOCKET
Sockets provide an interface for programming
networks at the transport layer.
 A server runs on a specific computer and has a
socket that is bound to a specific port.
 The server listens to the socket for a client to
make a connection request
 If everything goes well, the server accepts the
connection.
 Upon acceptance, the server gets a new socket
bound to a different port. It needs a new socket
(consequently a different port number) so that it
can continue to listen to the original socket for
connection requests while serving the connected
client.

Socket is bound to a port number so that the TCP
layer can identify the application that data is
destined to be sent.
 Java provides a set of classes, defined in a
package called java.net, to enable the rapid
development of network applications.
 The two key classes from the java.net package
used in creation of server and client programs
are:


 ServerSocket


Socket
SOCKET PROGRAMMING USING TCP


A server program creates a specific type of socket
that is used to listen for client requests (server
socket), In the case of a connection request, the
program creates a new socket through which it
will exchange data with the client using input
and output streams.
STEPS TO CREATE SERVER
PROGRAM
1. Open the Server Socket:
ServerSocket server = new ServerSocket( PORT );
2. Wait for the Client Request:
Socket client = server.accept();
3. Create I/O streams for communicating to the client
DataInputStream is = new
DataInputStream(client.getInputStream());
DataOutputStream os = new
DataOutputStream(client.getOutputStream());
4. Perform communication with client
Receive from client: String line = is.readLine();
Send to client: os.writeBytes(“Hellon”);
5. Close socket:
client.close();
STEPS TO CREATE CLIENT
PROGRAM
1. Create a Socket Object:
Socket client = new Socket(server, port_id);
2. Create I/O streams for communicating with the
server.
is = new DataInputStream(client.getInputStream());
os = new
DataOutputStream(client.getOutputStream());
3. Perform I/O or communication with the server:
Receive data from the server:
String line = is.readLine();
Send data to the server: os.writeBytes(“Hellon”);
4. Close the socket when done:
client.close();
Sending E-Mail
To send e-mail, you make a socket connection to port 25, the
SMTP port. SMTP is the Simple Mail Transport Protocol that
describes the format for e-mail messages.
1. Open a socket to your host.
Socket s = new Socket("mail.yourserver.com", 25); // 25 is
PrintWriter out = new PrintWriter(s.getOutputStream());
2. Send the following information to the print stream:


HELO sending host
MAIL FROM: <sender email address>
RCPT TO: <recipient email address>
DATA
mail message
(any number of lines)
.
QUIT


Most SMTP servers do not check the veracity of the
information—you may be able to supply any sender you
like.
Internet addresses
Usually, you don't have to worry too much about Internet
addresses—the numerical host addresses that consist of
four bytes such as 132.163.4.102. However, you can use the
InetAddress class if you need to convert between host
names and Internet addresses.
 The static getByName method returns an InetAddress
object of a host.
 For example,
InetAddress address= InetAddress.getByName(“india.gov");
returns an InetAddress object that encapsulates the
sequence of four bytes 132.163.4.102.
 You can access the bytes with the getAddress method.
byte[] addressBytes = address.getAddress();

Some host names with a lot of traffic correspond to
multiple Internet addresses, to facilitate load balancing.
 For example, at the time of this writing, the host name
google.com corresponds to different Internet addresses.
One of them is picked at random when the host is
accessed. You can get all hosts with the getAllByName
method.
InetAddress[] addresses =InetAddress.getAllByName(host);


Finally, you sometimes need the address of the local host.
If you simply ask for the address of localhost, you always
get the address 127.0.0.1, which isn't very useful. Instead,
use the static getLocalHost method to get the address of
your local host.
InetAddress address= InetAddress.getLocalHost();

URL Connections


The URL and URLConnection classes encapsulate much of the
complexity of retrieving information from a remote site.





URL url = new URL(urlString);

The Java 2 platform supports both HTTP and FTP resources.
If you simply want to fetch the contents of the resource, then you can
use the openStream method of the URL class. This method yields an
InputStream object. Using this stream object, you can easily read the
contents of the resource.
InputStream uin = url.openStream();
BufferedReader in =
new BufferedReader(new InputStreamReader(uin));
String line;
while ((line = in.readLine()) != null)
{
process line;
}
Posting Form Data
we saw how to read data from a web server. Now
we learn how your programs can send data back
to a web server and to programs that the web
server invokes, using the CGI (Common Gateway
Interface).
 check boxes and radio buttons are sent back to
the server to be processed by a CGI script.
 The CGI script to use is specified in the ACTION
attribute of the FORM tag.
 The CGI script is a program that resides on the
server computer. The web server launches the
CGI script and feeds it the form data.

What Is a Servlet?










Servlets are small programs that execute on the server side of a web
connection.
Just as applets dynamically extend the functionality of a web
browser, servlets dynamically extend the functionality of a web
server.
In the early days of the Web, a server could dynamically construct a
page by creating a separate process to handle each client request.
It communicated with the web server via an interface Common
Gateway Interface (CGI). CGI allowed the separate process to read
data from the HTTP request and write data to the HTTP response.
CGI suffered serious performance problems.




It was expensive in terms of processor and memory resources to
create a separate process for each client request.
It was also expensive to open and close database connections for each
client request
The CGI programs were not platform-independent
Advantages of Servlets










Servlets offer several advantages in comparison with CGI.
Performance is significantly better.
Servlets execute within the address space of a web server.
It is not necessary to create a separate process to handle
each client request.
servlets are platform-independent because they are written
in Java.
the Java security manager on the server enforces a set of
restrictions to protect the resources on a server machine.
Finally, the full functionality of the Java class libraries is
available to a servlet. It can communicate with applets,
databases, or other software via the sockets that you have
seen already.
The Life Cycle of a Servlet









init( ), service( ), and destroy( ) methods
HTTP request for generated for a URL.
Web server maps this request to a particular servlet which
is dynamically retrieved and loaded.
The server invokes the init( ) method of the servlet.
The server invokes the service( ) method of the servlet;
may also formulate an HTTP response for the client.
The server may decide to unload the servlet from its
memory using destroy() method
To create servlets, you will need access to a servlet
development environment.
 The one we will use is Tomcat. Tomcat is an opensource product maintained by the Jakarta Project of the
Apache Software Foundation. It contains the class
libraries, documentation, and runtime support that you
will need to create and test servlets.

Servlet API


javax.servlet Package


Interfaces Description









Servlet- Declares life cycle methods for a servlet.
ServletConfig- Allows servlets to get initialization parameters.
ServletContext- Enables servlets to log events and access information
about their environment.
ServletRequest- Used to read data from a client request.
ServletResponse- Used to write data to a client response.

SingleThreadModel- Indicates that the servlet is thread safe.
Servlet API Contd…
 Classes

Description

GenericServlet- Implements the Servlet and ServletConfig
interfaces.
 ServletInputStream- Provides an input stream for reading
requests from a client.
 ServletOutputStream- Provides an output stream for
writing responses to a client.
 ServletException- Indicates a servlet error occurred.
 UnavailableException- Indicates a servlet is unavailable.

Servlet API Contd…


javax.servlet.http Package
 Interface

Description

HttpServletRequest Enables servlets to read data from an
HTTP request.
 HttpServletResponse Enables servlets to write data to an
HTTP response.
 HttpSession Allows session data to be read and written.

Servlet API Contd…
 Classes

Description

Cookie- Allows state information to be stored on a client
machine.
 HttpServlet- Provides methods to handle HTTP requests
and
 responses.
 HttpSessionEvent- Encapsulates a session-changed event.


More Related Content

What's hot

Networking in Java
Networking in JavaNetworking in Java
Networking in Java
Tushar B Kute
 
Networking & Socket Programming In Java
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In Java
Ankur Agrawal
 
Java Networking
Java NetworkingJava Networking
Java Networking
Sunil OS
 
Socket programming using java
Socket programming using javaSocket programming using java
Socket programming using java
UC San Diego
 
Socket programming
Socket programmingSocket programming
Socket programming
chandramouligunnemeda
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
Vipin Yadav
 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programming
ashok hirpara
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
kamal kotecha
 
Java networking programs socket based
Java networking programs socket basedJava networking programs socket based
Java networking programs socket based
Mukesh Tekwani
 
Socket programming-tutorial-sk
Socket programming-tutorial-skSocket programming-tutorial-sk
Socket programming-tutorial-sk
sureshkarthick37
 
Tcp/ip server sockets
Tcp/ip server socketsTcp/ip server sockets
Tcp/ip server sockets
rajshreemuthiah
 
Java Networking
Java NetworkingJava Networking
Java Networking
Ankit Desai
 
Socket Programming Tutorial
Socket Programming TutorialSocket Programming Tutorial
Socket Programming Tutorial
Jignesh Patel
 
Multiplayer Java Game
Multiplayer Java GameMultiplayer Java Game
Multiplayer Java Game
karim baidar
 
Java API: java.net.InetAddress
Java API: java.net.InetAddressJava API: java.net.InetAddress
Java API: java.net.InetAddress
Sayak Sarkar
 
Java Networking
Java NetworkingJava Networking
Java Networking
68SachinYadavSYCS
 

What's hot (20)

Networking in Java
Networking in JavaNetworking in Java
Networking in Java
 
Java sockets
Java socketsJava sockets
Java sockets
 
Networking & Socket Programming In Java
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In Java
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Socket programming using java
Socket programming using javaSocket programming using java
Socket programming using java
 
Sockets
SocketsSockets
Sockets
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programming
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
Java networking programs socket based
Java networking programs socket basedJava networking programs socket based
Java networking programs socket based
 
Socket programming-tutorial-sk
Socket programming-tutorial-skSocket programming-tutorial-sk
Socket programming-tutorial-sk
 
Tcp/ip server sockets
Tcp/ip server socketsTcp/ip server sockets
Tcp/ip server sockets
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Socket Programming Tutorial
Socket Programming TutorialSocket Programming Tutorial
Socket Programming Tutorial
 
Multiplayer Java Game
Multiplayer Java GameMultiplayer Java Game
Multiplayer Java Game
 
Java API: java.net.InetAddress
Java API: java.net.InetAddressJava API: java.net.InetAddress
Java API: java.net.InetAddress
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
Java Networking
Java NetworkingJava Networking
Java Networking
 

Similar to Networking Java Socket Programming

Socket programming
Socket programmingSocket programming
Socket programming
Padmavathione
 
Socket Programming by Rajkumar Buyya
Socket Programming by Rajkumar BuyyaSocket Programming by Rajkumar Buyya
Socket Programming by Rajkumar Buyya
iDhawalVaja
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
Java Network Programming.pptx
Java Network Programming.pptxJava Network Programming.pptx
Java Network Programming.pptx
RoshniSundrani
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
Tushar B Kute
 
Client server chat application
Client server chat applicationClient server chat application
Client server chat application
Samsil Arefin
 
Chat server nitish nagar
Chat server nitish nagarChat server nitish nagar
Chat server nitish nagar
Nitish Nagar
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project ReportKavita Sharma
 
Lan chat system
Lan chat systemLan chat system
Lan chat system
Wipro
 
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docxProject Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
kacie8xcheco
 
Socket
SocketSocket
Lecture25
Lecture25Lecture25
Networking in java, Advanced programming
Networking in java, Advanced programmingNetworking in java, Advanced programming
Networking in java, Advanced programming
Gera Paulos
 
How a network connection is created A network connection is initi.pdf
How a network connection is created A network connection is initi.pdfHow a network connection is created A network connection is initi.pdf
How a network connection is created A network connection is initi.pdf
arccreation001
 
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
hanneloremccaffery
 
692015 programming assignment 1 building a multi­threaded w
692015 programming assignment 1 building a multi­threaded w692015 programming assignment 1 building a multi­threaded w
692015 programming assignment 1 building a multi­threaded w
smile790243
 
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
RashidFaridChishti
 

Similar to Networking Java Socket Programming (20)

Socket programming
Socket programmingSocket programming
Socket programming
 
Socket Programming by Rajkumar Buyya
Socket Programming by Rajkumar BuyyaSocket Programming by Rajkumar Buyya
Socket Programming by Rajkumar Buyya
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Java Network Programming.pptx
Java Network Programming.pptxJava Network Programming.pptx
Java Network Programming.pptx
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
 
Client server chat application
Client server chat applicationClient server chat application
Client server chat application
 
Chat server nitish nagar
Chat server nitish nagarChat server nitish nagar
Chat server nitish nagar
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project Report
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Lan chat system
Lan chat systemLan chat system
Lan chat system
 
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docxProject Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
 
Socket
SocketSocket
Socket
 
Lecture25
Lecture25Lecture25
Lecture25
 
Networking in java, Advanced programming
Networking in java, Advanced programmingNetworking in java, Advanced programming
Networking in java, Advanced programming
 
How a network connection is created A network connection is initi.pdf
How a network connection is created A network connection is initi.pdfHow a network connection is created A network connection is initi.pdf
How a network connection is created A network connection is initi.pdf
 
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
[Type text]ECET465Project 2Project Assignment 2 Building a Mul.docx
 
Networking
NetworkingNetworking
Networking
 
692015 programming assignment 1 building a multi­threaded w
692015 programming assignment 1 building a multi­threaded w692015 programming assignment 1 building a multi­threaded w
692015 programming assignment 1 building a multi­threaded w
 
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
 

Recently uploaded

FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 

Networking Java Socket Programming

  • 2. CLIENT/SERVER COMMUNICATION At a basic level, network-based systems consist of a server , client , and a media for communication  A computer running a program that makes a request for services is called client machine.  A computer running a program that offers requested services from one or more clients is called server machine.  The media for communication can be wired or wireless network. 
  • 3. Generally, programs running on client machines make requests to a program running on a server machine. They involve networking services provided by the transport layer, which is part of the Internet software stack, often called TCP/IP (Transport Control Protocol/Internet Protocol)  The transport layer comprises two types of protocols,   TCP (Transport Control Protocol)  UDP (User Datagram Protocol).  The most widely used programming interfaces for these protocols are “sockets”.
  • 4. TCP is a connection-oriented protocol that provides a reliable flow of data between two computers. Example applications that use such services are HTTP, FTP, and Telnet.  UDP is a protocol that sends independent packets of data, called “datagrams”, from one computer to another with no guarantees about arrival and sequencing. Example applications that use such services include Clock server and Ping.  The TCP and UDP protocols use “ports” to map incoming data to a particular process running on a computer.  Port is represented by a positive (16-bit) integer value 
  • 5. Some ports have been reserved to support common/well known services like ftp, http, https etc.  User-level process/services generally use port number value >= 1024.  Every computer on the Internet is identified by a unique, 4-byte IP address .  This is typically written in dotted quad format like 128.250.25.158 where each byte is an unsigned value between 0 and 255.  This representation is not user-friendly because it does not tell us anything about the content and then it is difficult to remember. Hence, IP addresses are mapped to names like www.google.com, which are easier to remember.  Domain name servers(DNS) translate these names to IP addresses. 
  • 6. Computers often need to communicate and provide more than one type of service or to talk to multiple hosts/computers at a time. For example, there may be multiple ftp sessions, web connections, and chat programs all running at the same time.  To distinguish these services, a concept of “port” s, a logical access point, represented by a 16-bit integer number is used.  That means, each service offered by a computer is uniquely identified by a port number.  Each Internet packet contains both the destination host address and the port number on that host to which the message/request has to be delivered.  The host computer dispatches the packets it receives to programs by looking at the port numbers specified within the packets. 
  • 7. SOCKET Sockets provide an interface for programming networks at the transport layer.  A server runs on a specific computer and has a socket that is bound to a specific port.  The server listens to the socket for a client to make a connection request  If everything goes well, the server accepts the connection.  Upon acceptance, the server gets a new socket bound to a different port. It needs a new socket (consequently a different port number) so that it can continue to listen to the original socket for connection requests while serving the connected client. 
  • 8.
  • 9. Socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent.  Java provides a set of classes, defined in a package called java.net, to enable the rapid development of network applications.  The two key classes from the java.net package used in creation of server and client programs are:   ServerSocket  Socket
  • 10. SOCKET PROGRAMMING USING TCP  A server program creates a specific type of socket that is used to listen for client requests (server socket), In the case of a connection request, the program creates a new socket through which it will exchange data with the client using input and output streams.
  • 11. STEPS TO CREATE SERVER PROGRAM 1. Open the Server Socket: ServerSocket server = new ServerSocket( PORT ); 2. Wait for the Client Request: Socket client = server.accept(); 3. Create I/O streams for communicating to the client DataInputStream is = new DataInputStream(client.getInputStream()); DataOutputStream os = new DataOutputStream(client.getOutputStream()); 4. Perform communication with client Receive from client: String line = is.readLine(); Send to client: os.writeBytes(“Hellon”); 5. Close socket: client.close();
  • 12. STEPS TO CREATE CLIENT PROGRAM 1. Create a Socket Object: Socket client = new Socket(server, port_id); 2. Create I/O streams for communicating with the server. is = new DataInputStream(client.getInputStream()); os = new DataOutputStream(client.getOutputStream()); 3. Perform I/O or communication with the server: Receive data from the server: String line = is.readLine(); Send data to the server: os.writeBytes(“Hellon”); 4. Close the socket when done: client.close();
  • 13. Sending E-Mail To send e-mail, you make a socket connection to port 25, the SMTP port. SMTP is the Simple Mail Transport Protocol that describes the format for e-mail messages. 1. Open a socket to your host. Socket s = new Socket("mail.yourserver.com", 25); // 25 is PrintWriter out = new PrintWriter(s.getOutputStream()); 2. Send the following information to the print stream:  HELO sending host MAIL FROM: <sender email address> RCPT TO: <recipient email address> DATA mail message (any number of lines) . QUIT
  • 14.  Most SMTP servers do not check the veracity of the information—you may be able to supply any sender you like.
  • 15. Internet addresses Usually, you don't have to worry too much about Internet addresses—the numerical host addresses that consist of four bytes such as 132.163.4.102. However, you can use the InetAddress class if you need to convert between host names and Internet addresses.  The static getByName method returns an InetAddress object of a host.  For example, InetAddress address= InetAddress.getByName(“india.gov"); returns an InetAddress object that encapsulates the sequence of four bytes 132.163.4.102.  You can access the bytes with the getAddress method. byte[] addressBytes = address.getAddress(); 
  • 16. Some host names with a lot of traffic correspond to multiple Internet addresses, to facilitate load balancing.  For example, at the time of this writing, the host name google.com corresponds to different Internet addresses. One of them is picked at random when the host is accessed. You can get all hosts with the getAllByName method. InetAddress[] addresses =InetAddress.getAllByName(host);  Finally, you sometimes need the address of the local host. If you simply ask for the address of localhost, you always get the address 127.0.0.1, which isn't very useful. Instead, use the static getLocalHost method to get the address of your local host. InetAddress address= InetAddress.getLocalHost(); 
  • 17. URL Connections  The URL and URLConnection classes encapsulate much of the complexity of retrieving information from a remote site.    URL url = new URL(urlString); The Java 2 platform supports both HTTP and FTP resources. If you simply want to fetch the contents of the resource, then you can use the openStream method of the URL class. This method yields an InputStream object. Using this stream object, you can easily read the contents of the resource. InputStream uin = url.openStream(); BufferedReader in = new BufferedReader(new InputStreamReader(uin)); String line; while ((line = in.readLine()) != null) { process line; }
  • 18. Posting Form Data we saw how to read data from a web server. Now we learn how your programs can send data back to a web server and to programs that the web server invokes, using the CGI (Common Gateway Interface).  check boxes and radio buttons are sent back to the server to be processed by a CGI script.  The CGI script to use is specified in the ACTION attribute of the FORM tag.  The CGI script is a program that resides on the server computer. The web server launches the CGI script and feeds it the form data. 
  • 19. What Is a Servlet?      Servlets are small programs that execute on the server side of a web connection. Just as applets dynamically extend the functionality of a web browser, servlets dynamically extend the functionality of a web server. In the early days of the Web, a server could dynamically construct a page by creating a separate process to handle each client request. It communicated with the web server via an interface Common Gateway Interface (CGI). CGI allowed the separate process to read data from the HTTP request and write data to the HTTP response. CGI suffered serious performance problems.    It was expensive in terms of processor and memory resources to create a separate process for each client request. It was also expensive to open and close database connections for each client request The CGI programs were not platform-independent
  • 20. Advantages of Servlets       Servlets offer several advantages in comparison with CGI. Performance is significantly better. Servlets execute within the address space of a web server. It is not necessary to create a separate process to handle each client request. servlets are platform-independent because they are written in Java. the Java security manager on the server enforces a set of restrictions to protect the resources on a server machine. Finally, the full functionality of the Java class libraries is available to a servlet. It can communicate with applets, databases, or other software via the sockets that you have seen already.
  • 21. The Life Cycle of a Servlet       init( ), service( ), and destroy( ) methods HTTP request for generated for a URL. Web server maps this request to a particular servlet which is dynamically retrieved and loaded. The server invokes the init( ) method of the servlet. The server invokes the service( ) method of the servlet; may also formulate an HTTP response for the client. The server may decide to unload the servlet from its memory using destroy() method
  • 22. To create servlets, you will need access to a servlet development environment.  The one we will use is Tomcat. Tomcat is an opensource product maintained by the Jakarta Project of the Apache Software Foundation. It contains the class libraries, documentation, and runtime support that you will need to create and test servlets. 
  • 23. Servlet API  javax.servlet Package  Interfaces Description        Servlet- Declares life cycle methods for a servlet. ServletConfig- Allows servlets to get initialization parameters. ServletContext- Enables servlets to log events and access information about their environment. ServletRequest- Used to read data from a client request. ServletResponse- Used to write data to a client response. SingleThreadModel- Indicates that the servlet is thread safe.
  • 24. Servlet API Contd…  Classes Description GenericServlet- Implements the Servlet and ServletConfig interfaces.  ServletInputStream- Provides an input stream for reading requests from a client.  ServletOutputStream- Provides an output stream for writing responses to a client.  ServletException- Indicates a servlet error occurred.  UnavailableException- Indicates a servlet is unavailable. 
  • 25. Servlet API Contd…  javax.servlet.http Package  Interface Description HttpServletRequest Enables servlets to read data from an HTTP request.  HttpServletResponse Enables servlets to write data to an HTTP response.  HttpSession Allows session data to be read and written. 
  • 26. Servlet API Contd…  Classes Description Cookie- Allows state information to be stored on a client machine.  HttpServlet- Provides methods to handle HTTP requests and  responses.  HttpSessionEvent- Encapsulates a session-changed event. 