Subject: Advanced Java
Subject Code: 3160707
Subject Credit: 6
Semester: VIth Computer Engineering
ADVANCED JAVA - 3160707
Advanced Java
JAVA
Platform
Java SE Java EE Java ME
Java Standard Edition Java Enterprise Edition Java Mobile Edition
Unit-1 JAVA NETWORKING 2
Subject Overview
• Teaching and Examination Scheme:
ADVANCED JAVA - 3160707
Subject Overview
Sr. No. Unit % Weightage
1 Java Networking 5
2 JDBC Programming 10
3 Servlet API and Overview 25
4 Java Server Pages 25
5 Java Server Faces 10
6 Hibernate 15
7 Java Web Frameworks: Spring MVC 10
Reference Book:
1. Complete Reference J2EE by James Keogh mcgraw publication
2. Black Book “ Java server programming” J2EE, 1st ed., Dream Tech Publishers, 2008.
Kathy walrath ”
3. Professional Java Server Programming by Subrahmanyam Allamaraju, Cedric Buest
Wiley Publication
ADVANCED JAVA - 3160707
Subject Overview :Unit Mapping
Sr. Unit Reference Book Chapter
No.
1 Java Networking The Complete Reference, Java (Seventh 20
Edition), Herbert Schild - Osbrone.
2 JDBC Programming Complete Reference J2EE by James Keogh 6,7
mcgraw publication
3 Servlet API and Overview 7,8
Professional Java Server Programming by
4 Java Server Pages Subrahmanyam Allamaraju, Cedric Buest Wiley 10,11
Publication
5 Java Server Faces 11
6 Hibernate Black Book “ Java server programming” J2EE, 15
1st ed., Dream Tech Publishers, 2008. 3. Kathy
7 Java Web Frameworks: walrath ” 21
Spring MVC
Unit-1 JAVA NETWORKING 5
UNIT-1
JAVA NETWORKING
Reference Book
The Complete Reference, Java (Seventh Edition), Herbert Schild - Osbrone.
CHAPTER : 20
ADVANCED JAVA - 3160707
Unit-1: Java Networking
Topic
1. Network Basics and Socket overview
2. InetAddress
3. TCP/IP server sockets
4. TCP/IP client sockets
5. Datagrams
6. URL
7. URLConnection
Unit-1 JAVA NETWORKING 7
Unit-1: Java Networking
Topic
1. Network Basics and Socket overview
2. InetAddress
3. TCP/IP server sockets
4. TCP/IP client sockets
5. Datagrams
6. URL
7. URLConnection
Unit-1 JAVA NETWORKING 8
Network Basics
https://www.computernetworkingnotes.com/ccna-study-guide/similarities-and-differences-between-osi-and-tcp-ip-model.html
Unit-1 JAVA NETWORKING 9
Network Basics(cont.)
https://www.researchgate.net/figure/The-logical-mapping-between-OSI-basic-reference-model-and-the-TCP-IP-stack_fig2_327483011
Unit-1 JAVA NETWORKING 10
Network Basics(cont.)
▪ TCP
▪ UDP
▪ PORT
▪ URL
Unit-1 JAVA NETWORKING 11
Network Basics: java.net pacakage
▪ 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.
Java.net Package
InetAddress
import java.net.*
URL
URLConnection
ServerSocket
TCP
Socket
DatagramPacket
UDP DatagramSocket
Unit-1 JAVA NETWORKING 12
Socket overview
Socket
▪ “A socket is one endpoint of a two-way communication link
between two programs running on the network.”
▪ An Socket is combination of an IP address and a port number.
Unit-1 JAVA NETWORKING 13
Client – Server Communication
▪ Two machines must connect
▪ Server waits for connection
▪ Client initiates connection
▪ Server responds to the client request
Server Client
Response
socket socket
Request
Waits
Unit-1 JAVA NETWORKING 14
Socket overview
• The server is just like any ordinary program running in a computer.
• Each computer is equipped with some ports.
Reference: isinotes/javatut/net
Unit-1 JAVA NETWORKING 15
Socket overview
• The server connects to one of the ports.
• This process is called binding to a port.
• The connection is called a server socket.
The Java server code that does this is:
ServerSocket ss = new ServerSocket(1234);//1234 is port number
Reference: isinotes/javatut/net
Unit-1 JAVA NETWORKING 16
Socket overview
• Server is waiting for client machine to connect.
Reference: isinotes/javatut/net
Unit-1 JAVA NETWORKING 17
Socket overview
Reference: isinotes/javatut/net
Unit-1 JAVA NETWORKING 18
Socket overview
• In the next step the client connects to this port of the server's computer.
• The connection is called a (client) socket.
Socket sock = new Socket("www.ldce.ac.in",80);
/* The client knows the number 80 */
Reference: isinotes/javatut/net
Unit-1 JAVA NETWORKING 19
Socket overview
• Now, connection is established between client and
server.
Reference: isinotes/javatut/net
Unit-1 JAVA NETWORKING 20
Socket overview
Reference: isinotes/javatut/net
Unit-1 JAVA NETWORKING 21
Socket overview
Everytime a client is found, its Socket is extracted, and the loop again waits for the
next client.
Reference: isinotes/javatut/net
Unit-1 JAVA NETWORKING 22
Socket overview
Client 2
Server
Client 1
Reference: isinotes/javatut/net
Unit-1 JAVA NETWORKING 23
Unit-1: Java Networking
Topic
1. Network Basics and Socket overview
2. InetAddress
3. TCP/IP server sockets
4. TCP/IP client sockets
5. Datagrams
6. URL
7. URLConnection
Unit-1 JAVA NETWORKING 24
InetAddress
java.net package
• This class represents an Internet Protocol (IP) address.
• The java.net.InetAddress class provides methods to get an IP of host name.
Example
InetAddress ip
=InetAddress.getByName("www.ldce.ac.in");
System.out.println(“ip:“+ ip);
Output:
ip: www.ldce.ac.in/166.62.10.189
Unit-1 JAVA NETWORKING 25
InetAddress : Method
Method Description
public static InetAddress Determines the IP address of a given host's
getByName(String host) name.
throws UnknownHostException
Example
InetAddress ip
=InetAddress.getByName("www.ldce.ac.in");
System.out.println(“ip:“+ ip);
Output:
ip: www.ldce.ac.in/166.62.10.189
Unit-1 JAVA NETWORKING 26
InetAddress : Method
Method Description
public static InetAddress Returns the address of the local host.
getLocalHost()
throws UnknownHostException
Example
InetAddress ip=InetAddress.getLocalHost();
System.out.println(“LocalHost:“+ip);
Output:
LocalHost:Pragnesh-PC/192.168.20.18
Unit-1 JAVA NETWORKING 27
InetAddress : Method
Method Description
public String getHostName() it returns the host name of the IP address.
Example
InetAddress ip=InetAddress.getByName(“www.ldce.ac.in");
System.out.println(“Hostname:”+ip.getHostName());
Output:
Hostname:www.ldce.ac.in
Unit-1 JAVA NETWORKING 28
InetAddress : Method
Method Description
public String getHostAddress() it returns the IP address in string format.
Example
InetAddress ip=InetAddress.getByName("www.ldce.ac.in");
System.out.println(“HostAddress:”+ip.getHostAddress());
Output:
HostAddress:166.62.10.189
Unit-1 JAVA NETWORKING 29
InetAddress: Program
import java.io.*;
import java.net.*;
public class Program1 {
public static void main(String[] args){
try{
InetAddress ip=InetAddress.getByName("www.ldce.ac.in");
System.out.println("IP : "+ip);
System.out.println("Host Name: "+ip.getHostName());
System.out.println("IP Address: "+ip.getHostAddress());
System.out.println("Hostname:"+ip.getHostName());
System.out.println("HostAddress: "+ip.getHostAddress());
InetAddress ip1=InetAddress.getLocalHost();
System.out.println("LocalHost: "+ip1);
}catch(Exception e){System.out.println(e);}
}
}
Unit-1 JAVA NETWORKING 30
InetAddress: Program
Output:
IP : www.ldce.ac.in/166.62.10.189
Host Name: www.ldce.ac.in
IP Address: 166.62.10.189
Hostname:www.ldce.ac.in
HostAddress: 166.62.10.189
LocalHost: Pragnesh-PC/192.168.20.18
Unit-1 JAVA NETWORKING 31
Unit-1: Java Networking
Topic
1. Network Basics and Socket overview
2. InetAddress
3. TCP/IP server sockets
4. TCP/IP client sockets
5. Datagrams
6. URL
7. URLConnection
Unit-1 JAVA NETWORKING 32
TCP/IP Client-Server sockets
Java.net
InetAddress
URL
URLConnection This class implements Server sockets
ServerSocket
TCP
Socket This class implements Client sockets.
DatagramPacket
DatagramSocket
MulticastSocket
Unit-1 JAVA NETWORKING 33
Ref: https://static.javatpoint.com/core/images/socket-programming.png
Unit-1 JAVA NETWORKING 34
TCP/IP ServerSocket Class
▪ The ServerSocket class (java.net) can be used to create a server
socket.
▪ This object is used to establish communication with the clients.
Constructor
ServerSocket(int port) Creates a server socket, bound to the specified
port.
Method
public Socket accept() returns the socket and establish a connection
between server and client.
Unit-1 JAVA NETWORKING 35
TCP/IP Server Sockets
Syntax
ServerSocket ss;
ss=new ServerSocket(Port_no);
Example
ServerSocket ss=new ServerSocket(1111);
Unit-1 JAVA NETWORKING 36
TCP/IP Server program
MyServer.java
1. import java.io.*; // required data input/output stream
2. import java.net.*; //required for Socket Class
3. public class MyServer {
4. public static void main(String[] args){
5. try{
6. ServerSocket ss=new ServerSocket(1111);
7. Socket s=ss.accept();//establishes connection
8. DataInputStream dis=
9. new DataInputStream (s.getInputStream());
10. String str=(String)dis.readUTF();
11. System.out.println("message= "+str);
12. ss.close();
13. }
14. catch(Exception e){System.out.println(e);}
15. }
16.}
Output
message= Hello Server
Server
socket
Unit-1 JAVA NETWORKING 37
TCP/IP Client Sockets: Socket Class
The client in socket programming must know two information:
1. IP Address of Server
2. Port number.
Constructor
Socket() Creates an unconnected socket
ServerSocket()Crates an unbound server socket.
Socket(InetAddress address, int port) Creates a stream socket and connects it to the
specified port number at the specified IP
ServerSocket(int port)Creates a server socket, bound to the specified port.
address.
ServerSocket(int port, int backlog)Creates a server socket and binds it to the
specified local port number, with the specified backlog.
Method
public InputStream getInputStream() returns the InputStream attached with this
ServerSocket(int port, int backlog, InetAddress
socketbindAddr)Create a server with the
specified port, listen backlog, and local IP address to bind to.
public OutputStream getOutputStream() returns the OutputStream attached with this
socket.
Unit-1 JAVA NETWORKING 38
TCP/IP Client Sockets
Syntax
Socket myClient; //Creates object of Socket Class
myClient = new Socket("Machine name", PortNumber);
DNS or IP Address Port Number is the port
(a number) on which the
server you are trying to
connect.
Example
Socket s;
s=new Socket("localhost",1111);
Unit-1 JAVA NETWORKING 39
TCP/IP Client Sockets: Program
1. import java.net.*; //required for Socket Class
2. import java.io.*; // required data input/output stream
3. public class MyClient {
4. public static void main(String[] args)
5. { try{
6. Socket s = new Socket("localhost",1111);
7. DataOutputStream dout= new Object of Socket class
DataOutputStream(s.getOutputStream());
8. dout.writeUTF("Hello Server");// Writes a
string to the underlying output stream
9. }catch(Exception e)
10. {System.out.println(e);}
11. }
12.}
Unit-1 JAVA NETWORKING 40
TCP/IP Client-Server program
MyServer.java MyClient.java
import java.io.*; import java.net.*;
import java.net.*; import java.io.*;
public class MyServer { public class MyClient {
public static void main(String[] args){
public static void main(String[] args){
try{
try {
ServerSocket ss=new ServerSocket(1111);
Socket s=new Socket("localhost",1111);
Socket s=ss.accept();
DataOutputStream dout=new
DataInputStream dis=new DataInputStream DataOutputStream(s.getOutputStream());
(s.getInputStream());
String str=(String)dis.readUTF(); dout.writeUTF("Hello Server");
//Writes string to underlying o/p
System.out.println("message= "+str); stream
ss.close();
}//try
catch(Exception e)
}catch(Exception e)
{System.out.println(e);} {System.out.println(e);}
}//psvm } //psvm
}//class }//class
Output
message= Hello Server
Unit-1 JAVA NETWORKING 41
Assignment
▪ Write a java program for TCP Client/Server Chat Application.
Unit-1 JAVA NETWORKING 42
Unit-1: Java Networking
Topic
1. Network Basics and Socket overview
2. InetAddress
3. TCP/IP server sockets
4. TCP/IP client sockets
5. Datagrams
6. URL
7. URLConnection
Unit-1 JAVA NETWORKING 43
Datagrams
Java.net
InetAddress
URL
URLConnection
ServerSocket This class represents a
datagram packet.
Socket
DatagramPacket
Represents a socket for
UDP DatagramSocket sending and receiving
datagram packets.
Unit-1 JAVA NETWORKING 44
Datagrams: DatagramSocket class
▪ DatagramSocket class represents a connection-less socket for
sending and receiving datagram packets.
▪ A datagram is basically an information but there is no guarantee
of its content, arrival or arrival time.
▪ Constructor
DatagramSocket() it creates a datagram socket and binds it with the
available Port Number on the localhost machine.
DatagramSocket(int port) it creates a datagram socket and binds it with the given
Port Number.
DatagramSocket(int port, it creates a datagram socket and binds it with the
InetAddress address) specified port number and host address.
Unit-1 JAVA NETWORKING 45
Datagrams: DatagramPacket class
▪ Java DatagramPacket is a message that can be sent or received.
▪ If you send multiple packet, it may arrive in any order.
▪ Additionally, packet delivery is not guaranteed.
number of bytes to
buffer for holding the incoming datagram. read.
Constructor
DatagramPacket(byte[] barr, int length) It creates a datagram packet. This
constructor is used to receive the packets.
DatagramPacket(byte[] barr, int length, It creates a datagram packet. This
InetAddress address, int port) constructor is used to send the packets.
Unit-1 JAVA NETWORKING 46
Example of Sending DatagramPacket by DatagramSocket
import java.net.*; //required for Datagram Class
public class DSender{
public static void main(String[] args)
throws Exception
{
DatagramSocket ds = new DatagramSocket();
String str = "Message sent by Datagram socket";
InetAddress ip = InetAddress.getByName("127.0.0.1");
DatagramPacket dp = new DatagramPacket
(str.getBytes(), str.length(), ip, 3000);
ds.send(dp);
ds.close();
}
}
Unit-1 JAVA NETWORKING 47
Example of Receiving DatagramPacket by DatagramSocket
import java.net.*;
public class DReceiver{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket(3000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 1024);
ds.receive(dp);
String str = new String(dp.getData(), 0,dp.getLength());
System.out.println(str);
ds.close();
}
}
Output
Message sent by Datagram socket
Unit-1 JAVA NETWORKING 48
Example of Sending and Receiving DatagramPacket
DSender.java DReceiver.java
import java.net.*; import java.net.*;
public class DSender{ public class DReceiver{
public static void main(String[] args) public static void main(String[] args
throws Exception{ ) throws Exception {
DatagramSocket ds= DatagramSocket ds =
new DatagramSocket(); new DatagramSocket(3000);
String str = "Message sent by Datagram byte[] buf = new byte[1024];
socket"; DatagramPacket dp =
InetAddress ip = new DatagramPacket(buf, 1024);
InetAddress.getByName(“localhost");
ds.receive(dp);
DatagramPacket dp = new DatagramPacket String str = new String
(str.getBytes(),str.length(),ip,3000); (dp.getData(), 0,dp.getLength());
ds.send(dp); System.out.println(str);
ds.close(); ds.close();
}
} }
}
Output
Message sent by Datagram socket
Unit-1 JAVA NETWORKING 49
Unit-1: Java Networking
Topic
1. Network Basics and Socket overview
2. InetAddress
3. TCP/IP client sockets
4. TCP/IP server sockets
5. Datagrams
6. URL
7. URLConnection
Unit-1 JAVA NETWORKING 50
URL: Uniform Resource Locator
▪ The Java URL class represents an URL.
▪ This class is pointer to “resource” on the World Wide Web.
Port Number
E.g.
http is the
http://10.255.1.1:8090/httpclient.html
protocol.
Server name or IP Address File Name or directory name
Unit-1 JAVA NETWORKING 51
URL
Constructor
URL(String url) Creates a URL object from the String representation.
Example
URL url=new URL("http://www.ldce.ac.in");
Method
public URLConnection openConnection() This method of URL class returns the
throws IOException object of URLConnection class
Example
URLConnection urlcon=url.openConnection();
Unit-1 JAVA NETWORKING 52
Unit-1: Java Networking
Topic
1. Network Basics and Socket overview
2. InetAddress
3. TCP/IP client sockets
4. TCP/IP server sockets
5. Datagrams
6. URL
7. URLConnection
Unit-1 JAVA NETWORKING 53
URLConnection
▪ Abstact Class
▪ URLConnection is the superclass of all classes that represent a
communications link between the application and a URL.
▪ Instances of this class can be used both to read from and to write
to the resource referenced by the URL.
Constructor
URLConnection(URL url) Constructs a URL connection to the
specified URL
Unit-1 JAVA NETWORKING 54
URLConnection
Method
public InputStream getInputStream() Returns an input stream that reads from this
throws IOException open connection.
public OutputStream getOutputStream()
openConnection()
Returns an output stream that writes to this
connect()
throws IOException connection.
Manipulate parameters that affect the Interact with the resource; query header fields
connection to the remote resource. and contents.
Unit-1 JAVA NETWORKING 55
URLConnection : Program
import java.io.*; //required for input stream
import java.net.*; //required for URL & URLConnection
public class URLConnectionDemo {
public static void main(String[] args){
try{
URL url=new URL("http://www.ldce.ac.in");
URLConnection urlcon=url.openConnection();
InputStream stream=urlcon.getInputStream();
int i; Object of URLConnection Class
while((i=stream.read())!=-1){
System.out.print((char)i);
}
}catch(Exception e){System.out.println(e);}
}
}
Unit-1 JAVA NETWORKING 56
URL, URI and URN
▪ URL
▪ URI
▪ URN
Unit-1 JAVA NETWORKING 57
Summary
▪ Basic of socket
▪ Socket programming
▪ Client-Server Communication
▪ TCP Client-Server Program
▪ UDP Sender-Receiver Program
▪ URL Connection
Unit-1 JAVA NETWORKING 58
Reference
Book Reference
• The Complete Reference, Java (Seventh Edition), Herbert Schild - Osbrone.
Web Reference
• https://docs.oracle.com/javase/7/docs/api/
• https://www.tutorialspoint.com
Unit-1 JAVA NETWORKING 59
Assignment: Unit-1
1. Explain working of following protocol
IP, IPV4, IPV6, TCP, UDP, SMTP, HTTP, HTTPS, DNS, FTP
2. TCP vs UDP with their applications.
3. HTTP is stateless protocol. Justify
4. Define the following: Socket, URL, Port
5. Write a java program to demonstrate methods of InetAddress.
6. Write a java Connection less (UDP) Program to do the following:
Sender will send : Welcome to Gujarat Technological UNIVERSITY
Receiver will send back to sender: ytisrevinu LACIGOLONHCEt TARAJUg TO
EMOCLEw
7. Write a java Connection less (UDP) Program for chat application.
8. Write a client-server program using TCP and UDP where the client sends 10
numbers and server responds with the numbers in sorted order.
9. Write a TCP Client-Server program to get the Date & Time details from Server on
the Client request.
10. Write a client server program using TCP where client sends two numbers and
server responds with sum of them.
Unit-1 JAVA NETWORKING 60