Networking in java
CONTENTS
Lesson Content
o 2.1 Introduction
o 2.2 Sockets
o 2.3 Manipulating a File on a Web Server
o 2.4 Establishing a Simple Server Using Stream Sockets
o 2.5 Establishing a Simple Client Using Stream Sockets
o 2.6 Client/Server Interaction with Stream Socket Connection
o 2.7 Connectionless Client/Server Interaction with Datagram
o 2.8 Security and the Network
2.1 Introduction
o The World Wide Web makes the Internet easy to use and gives it the
flair and sizzle of multimedia.
o Java provides a number of built-in networking capabilities that make it
easy to develop Internet-based and Web-based applications.
o Not only can Java specify parallelism through multithreading, but it can
enable programs to search the world for information and to collaborate
with programs running on other computers internationally, nationally,
or just within an organization. Java can enable applets and applications
to communicate with one another (subject to security constraints).
2.1 Introduction
o Networking is a massive and complex topic. Java provides a rich
complement of networking capabilities and will likely be used as an
implementation vehicle in computer networking. Java’s networking
capabilities are grouped into several packages.
o The fundamental networking capabilities are defined by classes and
interfaces of package java.net, through which Java offers socket-based
communications that enable applications to view networking as streams
of data
o The classes and interfaces of package java.net also offer packet-based
communications that enable individual packets of information to be
2.1 Introduction
o A common implementation of the request-response model is between
World Wide Web browsers and World Wide Web servers
o Java provides stream sockets and datagram sockets. With stream
sockets, a process establishes a connection to another process.
o Stream sockets are said to provide a connection-oriented service. The
protocol used for transmission is the popular TCP (Transmission Control
Protocol.
o With datagram sockets, individual packets of information are
transmitted
o UDP is most appropriate for network applications that do not require the
2.1 Introduction
2.2 Sockets
o Normally, a server runs on a specific computer and has a socket that is
bound to a specific port number. The server just waits, listening to the
socket for a client to make a connection request.
o Socket in Java is used for communication between the applications that
are running on different Java Runtime Environment (JRE). It is an
interface to the network protocol stack typically at the transport layer. It
can be either connection-oriented or connectionless.
2.2 Sockets
o Socket programming is a way of connecting two nodes on a network to
communicate with each other. One socket (node) listens on a particular
port at an IP, while the other socket reaches out to the other in order to
form a connection.
o The client knows the hostname of the machine on which the server is
running and the port number on which the server is listening. To make a
connection request, the client tries to engage with the server on the
server’s machine and port.
o The client also needs to identify itself to the server so it binds to a local
port number that it will use during this connection. This is usually
2.2 Sockets
o If everything goes well, the server accepts the connection. Upon
acceptance, the server gets a new socket bound to the same local port
and also has its remote endpoint set to the address and port of the
client
2.2. Sockets
o Definition: A socket is one end-point of a two-way communication link
between two programs (process) running on the network. A socket is
bound to a port number so that the TCP/IP layer can identify the
application to that data is destined to be sent. An endpoint is a
combination of an IP address and a port number
o Example: Socket(“10.198.7.20”, 1234)
o { protocol, local-address, local-port, remote-address, remote-port }
o The package in the Java platform provides a class, Socket that
implements one side of a two-way connection between your Java
2.2. Sockets
o 1. Socket Class: Implements the client side of a two-way
connection between your Java program and another program on the
network by using java.net.Socket..
2.3 Establishing a Simple Server
Using Stream Sockets
Establishing a simple server in Java requires five
Step 1:- create a ServerSocket object. A call to the ServerSocket constructor
steps.
This registers an available port number and specifies a
maximum number of clients that can wait to connect to the
server (i.e., the queueLength
2.3 Establishing a Simple Server
Using Stream
Establishing Sockets
a simple server in Java requires five
Step 1:- create a ServerSocket object. A call to the ServerSocket constructor
steps.
The port number is used by clients to the located server
application on the server computer.
This often is called the handshake point.
If the queue is full, the server refuses client connections.
The preceding statement establishes the port where the server
waits for connections from clients (a process known as binding
the server to the port).
Each client will ask to connect to the server on this port.
Programs manage each client connection with a Socket object.
After binding the server to a port with a ServerSocket.
2.3 Establishing a Simple Server
Using Stream
Establishing Sockets
a simple server in Java requires five
Step 2:- the server listens indefinitely (or blocks) for an
steps.
attempt by a client to connect. To listen for a client, the
program calls ServerSocket method to accept, as in
This statement returns a Socket object when a connection
with a client is established.
The accept() method is called by the server to validate an
incoming request to the socket.
It is called the communication phase.
2.3 Establishing a Simple Server
Using Stream
Establishing Sockets
a simple server in Java requires five
Step 3:- Get the OutputStream and InputStream objects
steps.
that enable the server to communicate with the client by
sending and receiving
DataInputStream bytes.
dis= new DataInputStream( client.getInputStream());
DataOutputStream dos= new DataOutputStream( client.getOutputStream());
The server sends information to the client via an OutputStream
object.
The server receives information from the client via an
InputStream object.
To obtain the streams, the server invokes method
getOutputStream on the Socket to get a reference to the
OutputStream associated with the Socket and invokes method
2.3 Establishing a Simple Server
Using Stream
Establishing Sockets
a simple server in Java requires five
Step 4:- The processing phase, in which the server and
steps.
the client communicate via the InputStream and
OutputStream objects
Step 5:- When the transmission is complete, the server
closes the connection by invoking the close method on the
Socket and on the corresponding streams.
2.4 Establishing a Simple Client
Using Stream
Establishing Sockets
a simple Client in Java requires four
Step 1:- create a ServerSocket object. A call to the
steps.
ServerSocket constructor
Step 2:- Create
dis= newinput output
DataInputStream( stream for communicating
client.getInputStream());
with the server.
dos= new DataOutputStream( client.getOutputStream());
Step 3:- Perform input output communication with the
server Send data to the Server: dos.writeUTF(str2);
Read data from the server: str1=dis. readUTF();
Step 4:- Close the socket
Socket.close();
2.3 Establishing a Simple Server
Using Stream Sockets
Thank You For Your Attention!!.