2160707 Advanced Java Unit1 Java Networking Prof Swati

  • Slides: 47
Download presentation
2160707 Advanced Java Unit-1 Java Networking Prof. Swati R. Sharma swati. sharma@darshan. ac. in

2160707 Advanced Java Unit-1 Java Networking Prof. Swati R. Sharma swati. sharma@darshan. ac. in

Subject Overview : Unit Mapping Sr. No. Unit Reference Book 1 Java Networking The

Subject Overview : Unit Mapping Sr. No. Unit Reference Book 1 Java Networking The Complete Reference, Java (Seventh Edition), Herbert Schild - Osbrone. 20 2 JDBC Programming Complete Reference J 2 EE by James Keogh mcgraw publication 6, 7 3 Servlet API and Overview 4 Java Server Pages 5 Java Server Faces 6 Hibernate 7 Java Web Frameworks: Spring MVC Unit-1 JAVA NETWORKING Professional Java Server Programming by Subrahmanyam Allamaraju, Cedric Buest Wiley Publication Black Book “ Java server programming” J 2 EE, 1 st ed. , Dream Tech Publishers, 2008. 3. Kathy walrath ” Chapter 7, 8 10, 11 11 15 21 Darshan Institute of Engineering & Technology 2

Unit-1: Java Networking Topic 1. Network Basics and Socket overview 2. Inet. Address 3.

Unit-1: Java Networking Topic 1. Network Basics and Socket overview 2. Inet. Address 3. TCP/IP server sockets 4. TCP/IP client sockets 5. Datagrams 6. URL 7. URLConnection Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 3

Network Basics: java. net pacakage § The term network programming refers to writing programs

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 import java. net. * TCP UDP Unit-1 JAVA NETWORKING This class represents an Class represents a Uniform Internet Protocol (IP) address. URL Resource Locator, a pointer to a URLConnection "resource" on the World Wide Represent the communications link Web. Used to create a server socket. between the application and a URL Server. Socket This object is used to establish Socket communication with the clients. This class implements client sockets. Datagram. Packet This class represents a datagram packet. Datagram. Socket Represents a socket for sending and receiving datagram packets. Inet. Address Darshan Institute of Engineering & Technology 4

Socket overview Socket § “A socket is one endpoint of a two-way communication link

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 Darshan Institute of Engineering & Technology 5

Client – Server Communication § Two machines must connect § Server waits for connection

Client – Server Communication § Two machines must connect § Server waits for connection § Client initiates connection § Server responds to the client request Server Client Response socket Request Waits Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 6

Socket overview • The server is just like any ordinary program running in a

Socket overview • The server is just like any ordinary program running in a computer. • Each computer is equipped with some ports. Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 7

Socket overview • The server connects to one of the ports. • This process

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: Server. Socket ss = new Server. Socket(1234); //1234 is port number Reference: isinotes/javatut/net Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 8

Socket overview • Server is waiting for client machine to connect. Reference: isinotes/javatut/net Unit-1

Socket overview • Server is waiting for client machine to connect. Reference: isinotes/javatut/net Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 9

Socket overview Reference: isinotes/javatut/net Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 10

Socket overview Reference: isinotes/javatut/net Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 10

Socket overview • In the next step the client connects to this port of

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. darshan. ac. in", 80); /* The client knows the number 80 */ Reference: isinotes/javatut/net Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 11

Socket overview • Now, connection is established between client and server. Reference: isinotes/javatut/net Unit-1

Socket overview • Now, connection is established between client and server. Reference: isinotes/javatut/net Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 12

Socket overview Reference: isinotes/javatut/net Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 13

Socket overview Reference: isinotes/javatut/net Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 13

Socket overview Everytime a client is found, its Socket is extracted, and the loop

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 Darshan Institute of Engineering & Technology 14

Socket overview Client 2 Server Client 1 Reference: isinotes/javatut/net Unit-1 JAVA NETWORKING Darshan Institute

Socket overview Client 2 Server Client 1 Reference: isinotes/javatut/net Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 15

Unit-1: Java Networking Topic 1. Network Basics and Socket overview 2. Inet. Address 3.

Unit-1: Java Networking Topic 1. Network Basics and Socket overview 2. Inet. Address 3. TCP/IP server sockets 4. TCP/IP client sockets 5. Datagrams 6. URL 7. URLConnection Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 16

Inet. Address java. net package • This class represents an Internet Protocol (IP) address.

Inet. Address java. net package • This class represents an Internet Protocol (IP) address. • The java. net. Inet. Address class provides methods to get an IP of host name. Example Inet. Address ip =Inet. Address. get. By. Name("www. darshan. ac. in"); System. out. println(“ip: “+ ip); Output: ip: www. darshan. ac. in/89. 238. 188. 50 Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 17

Inet. Address : Method Description public static Inet. Address get. By. Name(String host) throws

Inet. Address : Method Description public static Inet. Address get. By. Name(String host) throws Unknown. Host. Exception Determines the IP address of a given host's name. Example Inet. Address ip =Inet. Address. get. By. Name("www. darshan. ac. in"); System. out. println(“ip: “+ ip); Output: ip: www. darshan. ac. in/89. 238. 188. 50 Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 18

Inet. Address : Method Description public static Inet. Address get. Local. Host() throws Unknown.

Inet. Address : Method Description public static Inet. Address get. Local. Host() throws Unknown. Host. Exception Returns the address of the local host. Example Inet. Address ip=Inet. Address. get. Local. Host(); System. out. println(“Local. Host: “+ip); Output: Local. Host: swati-PC/10. 254. 3. 34 Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 19

Inet. Address : Method Description public String get. Host. Name() it returns the host

Inet. Address : Method Description public String get. Host. Name() it returns the host name of the IP address. Example Inet. Address ip=Inet. Address. get. By. Name("10. 254. 3. 34"); System. out. println(“Hostname: ”+ip. get. Host. Name()); Output: Hostname: swati-PC Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 20

Inet. Address : Method Description public String get. Host. Address() it returns the IP

Inet. Address : Method Description public String get. Host. Address() it returns the IP address in string format. Example Inet. Address ip=Inet. Address. get. By. Name("www. darshan. ac. in"); System. out. println(“Host. Address: ”+ip. get. Host. Address()); Output: Host. Address: 89. 238. 188. 50 Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 21

Inet. Address: Program import java. net. *; //required for Inet. Address Class public class

Inet. Address: Program import java. net. *; //required for Inet. Address Class public class Inet. Demo{ public static void main(String[] args){ try{ Inet. Address ip =Inet. Address. get. By. Name("www. darshan. ac. in"); System. out. println("Host Name: "+ip. get. Host. Name()); System. out. println("IP Address: "+ip. get. Host. Address()); } catch(Exception e){System. out. println(e); } } } Output: Host Name: www. darshan. ac. in IP Address: 89. 238. 188. 50 Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 22

Inet. Address: Method Summary static Inet. Address get. By. Name(String host) Determines the IP

Inet. Address: Method Summary static Inet. Address get. By. Name(String host) Determines the IP address of a given host's name. static Inet. Address get. Local. Host() Returns the address of the local host. public String get. Host. Name() it returns the host name of the IP address. public String get. Host. Address() it returns the IP address in string format. String to. String() Converts this IP address to a String. boolean equals(Object obj) Compares this object against the specified object. static Inet. Address[] get. All. By. Name(String host) Returns an array of its IP addresses, based on the configured name service on the system. static Inet. Address get. Loopback. Address() Returns the loopback address. Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 23

Unit-1: Java Networking Topic 1. Network Basics and Socket overview 2. Inet. Address 3.

Unit-1: Java Networking Topic 1. Network Basics and Socket overview 2. Inet. Address 3. TCP/IP server sockets 4. TCP/IP client sockets 5. Datagrams 6. URL 7. URLConnection Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 24

TCP/IP Client-Server sockets Java. net Inet. Address URLConnection TCP This class implements Server sockets

TCP/IP Client-Server sockets Java. net Inet. Address URLConnection TCP This class implements Server sockets Server. Socket This class implements Client sockets. Datagram. Packet Datagram. Socket Multicast. Socket Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 25

TCP/IP Server. Socket Class § The Server. Socket class (java. net) can be used

TCP/IP Server. Socket Class § The Server. Socket class (java. net) can be used to create a server socket. § This object is used to establish communication with the clients. Constructor Server. Socket(int port) Creates a server socket, bound to the specified port. Method public Socket accept() Unit-1 JAVA NETWORKING returns the socket and establish a connection between server and client. Darshan Institute of Engineering & Technology 26

TCP/IP Server Sockets Syntax Server. Socket ss; ss=new Server. Socket(Port_no); Example Server. Socket ss=new

TCP/IP Server Sockets Syntax Server. Socket ss; ss=new Server. Socket(Port_no); Example Server. Socket ss=new Server. Socket(1111); Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 27

TCP/IP Server program My. Server. java 1. import java. io. *; // required data

TCP/IP Server program My. Server. java 1. import java. io. *; // required data input/output stream 2. import java. net. *; //required for Socket Class 3. public class My. Server { 4. public static void main(String[] args){ 5. try{ 6. Server. Socket ss=new Server. Socket(1111); 7. Socket s=ss. accept(); //establishes connection 8. Data. Input. Stream dis= 9. new Data. Input. Stream (s. get. Input. Stream()); 10. String str=(String)dis. read. UTF(); 11. System. out. println("message= "+str); 12. ss. close(); 13. } 14. catch(Exception e){System. out. println(e); } 15. } 16. } Output message= Hello Server socket Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 28

TCP/IP Client Sockets: Socket Class The client in socket programming must know two information:

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 Socket(Inet. Address address, int port) Creates a stream socket and connects it to the specified port number at the specified IP address. Method public Input. Stream get. Input. Stream() returns the Input. Stream attached with this socket public Output. Stream get. Output. Stream() returns the Output. Stream attached with this socket. Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 29

TCP/IP Client Sockets Syntax Socket my. Client; //Creates object of Socket Class my. Client

TCP/IP Client Sockets Syntax Socket my. Client; //Creates object of Socket Class my. Client = new Socket("Machine name", Port. Number); 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 Darshan Institute of Engineering & Technology 30

TCP/IP Client Sockets: Program 1. import java. net. *; //required for Socket Class 2.

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 My. Client { 4. public static void main(String[] args) 5. { try{ 6. Socket s = new Socket("localhost", 1111); 7. Data. Output. Stream dout= new Object of Socket class Data. Output. Stream(s. get. Output. Stream()); 8. dout. write. UTF("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 Darshan Institute of Engineering & Technology 31

TCP/IP Client-Server program My. Server. java My. Client. java import java. io. *; import

TCP/IP Client-Server program My. Server. java My. Client. java import java. io. *; import java. net. *; import java. io. *; public class My. Server { public class My. Client { public static void main(String[] args){ try { Server. Socket ss=new Server. Socket(1111); Socket s=new Socket("localhost", 1111); Socket s=ss. accept(); Data. Output. Stream dout=new Data. Output. Stream(s. get. Output. Stream()); Data. Input. Stream dis=new Data. Input. Stream (s. get. Input. Stream()); String str=(String)dis. read. UTF(); dout. write. UTF("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); } } //psvm }//class Output message= Hello Server Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 32

Unit-1: Java Networking Topic 1. Network Basics and Socket overview 2. Inet. Address 3.

Unit-1: Java Networking Topic 1. Network Basics and Socket overview 2. Inet. Address 3. TCP/IP server sockets 4. TCP/IP client sockets 5. Datagrams 6. URL 7. URLConnection Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 33

Datagrams Java. net Inet. Address URLConnection Server. Socket This class represents datagram packet. a

Datagrams Java. net Inet. Address URLConnection Server. Socket This class represents datagram packet. a Datagram. Packet UDP Unit-1 JAVA NETWORKING Datagram. Socket Represents a socket for sending and receiving datagram packets. Darshan Institute of Engineering & Technology 34

Datagrams: Datagram. Socket class § Datagram. Socket class represents a connection-less socket for sending

Datagrams: Datagram. Socket class § Datagram. Socket 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 Datagram. Socket() it creates a datagram socket and binds it with the available Port Number on the localhost machine. Datagram. Socket(int port) it creates a datagram socket and binds it with the given Port Number. Datagram. Socket(int port, it creates a datagram socket and binds it with the Inet. Address address) specified port number and host address. Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 35

Datagrams: Datagram. Packet class § Java Datagram. Packet is a message that can be

Datagrams: Datagram. Packet class § Java Datagram. Packet 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. buffer for holding the incoming datagram. number of bytes to read. Constructor Datagram. Packet(byte[] barr, int length) It creates a datagram packet. This constructor is used to receive the packets. Datagram. Packet(byte[] barr, int length, It creates a datagram packet. This Inet. Address address, int port) constructor is used to send the packets. Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 36

Example of Sending Datagram. Packet by Datagram. Socket import java. net. *; //required for

Example of Sending Datagram. Packet by Datagram. Socket import java. net. *; //required for Datagram Class public class DSender{ public static void main(String[] args) throws Exception { Datagram. Socket ds = new Datagram. Socket(); String str = "Message sent by Datagram socket"; Inet. Address ip = Inet. Address. get. By. Name("127. 0. 0. 1"); Datagram. Packet dp = new Datagram. Packet (str. get. Bytes(), str. length(), ip, 3000); ds. send(dp); ds. close(); } } Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 37

Example of Receiving Datagram. Packet by Datagram. Socket import java. net. *; public class

Example of Receiving Datagram. Packet by Datagram. Socket import java. net. *; public class DReceiver{ public static void main(String[] args) throws Exception { Datagram. Socket ds = new Datagram. Socket(3000); byte[] buf = new byte[1024]; Datagram. Packet dp = new Datagram. Packet(buf, 1024); ds. receive(dp); String str = new String(dp. get. Data(), 0, dp. get. Length()); System. out. println(str); ds. close(); } } Output Message sent by Datagram socket Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 38

Example of Sending and Receiving Datagram. Packet DReceiver. java DSender. java import java. net.

Example of Sending and Receiving Datagram. Packet DReceiver. java DSender. java import java. net. *; public class DSender{ public static void main(String[] args) throws Exception{ Datagram. Socket ds= new Datagram. Socket(); String str = "Message sent by Datagram socket"; Inet. Address ip = Inet. Address. get. By. Name("127. 0. 0. 1"); Datagram. Packet dp = new Datagram. Packet (str. get. Bytes(), str. length(), ip, 3000); ds. send(dp); ds. close(); } } import java. net. *; public class DReceiver{ public static void main(String[] args ) throws Exception { Datagram. Socket ds = new Datagram. Socket(3000); byte[] buf = new byte[1024]; Datagram. Packet dp = new Datagram. Packet(buf, 1024); ds. receive(dp); String str = new String (dp. get. Data(), 0, dp. get. Length()); System. out. println(str); ds. close(); } } Output Message sent by Datagram socket Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 39

Unit-1: Java Networking Topic 1. Network Basics and Socket overview 2. Inet. Address 3.

Unit-1: Java Networking Topic 1. Network Basics and Socket overview 2. Inet. Address 3. TCP/IP client sockets 4. TCP/IP server sockets 5. Datagrams 6. URL 7. URLConnection Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 40

URL: Uniform Resource Locator § The Java URL class represents an URL. § This

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 protocol. http: //10. 255. 1. 1: 8090/httpclient. html Server name or IP Address Unit-1 JAVA NETWORKING File Name or directory name Darshan Institute of Engineering & Technology 41

URL Constructor URL(String url) Creates a URL object from the String representation. Example URL

URL Constructor URL(String url) Creates a URL object from the String representation. Example URL url=new URL("http: //www. darshan. ac. in"); Method public URLConnection open. Connection() throws IOException This method of URL class returns the object of URLConnection class Example URLConnection urlcon=url. open. Connection(); Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 42

Unit-1: Java Networking Topic 1. Network Basics and Socket overview 2. Inet. Address 3.

Unit-1: Java Networking Topic 1. Network Basics and Socket overview 2. Inet. Address 3. TCP/IP client sockets 4. TCP/IP server sockets 5. Datagrams 6. URL 7. URLConnection Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 43

URLConnection § URLConnection is the superclass of all classes that represent a communications link

URLConnection § 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 Darshan Institute of Engineering & Technology 44

URLConnection Method public Input. Stream get. Input. Stream() throws IOException Returns an input stream

URLConnection Method public Input. Stream get. Input. Stream() throws IOException Returns an input stream that reads from this open connection. public Output. Stream get. Output. Stream() Returns an output stream that writes to this throws IOException connection. Unit-1 JAVA NETWORKING Darshan Institute of Engineering & Technology 45

URLConnection : Program import java. io. *; //required for input stream import java. net.

URLConnection : Program import java. io. *; //required for input stream import java. net. *; //required for URL & URLConnection public class URLConnection. Demo { public static void main(String[] args){ try{ URL url=new URL("http: //www. darshan. ac. in"); URLConnection urlcon=url. open. Connection(); Input. Stream stream=urlcon. get. Input. Stream(); 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 Darshan Institute of Engineering & Technology 46

Important Questions: GTU 1 What is Server Socket? Explain in detail with an example.

Important Questions: GTU 1 What is Server Socket? Explain in detail with an example. Discuss the difference between the Socket and Server. Socket class. 2 What is Datagram Socket? Explain in detail with example. 3 Write a TCP or UDP client and server program to do the following: Client send : Welcome to Gujarat Technological UNIVERSITY Response from Server: ytisrevinu LACIGOLONHCEt TARAJUg TO EMOCLEw 4 Write a client-server program using TCP or UDP where the client sends 10 numbers and server responds with the numbers in sorted order. 5 Write a TCP Client-Server program to get the Date & Time details from Server on the Client request. 6 Write a client server program using TCP where client sends two numbers and server responds with sum of them. 7 Write a client server program using TCP where client sends a string and server checks whether that string is palindrome or not and responds with appropriate message. Unit-1 JAVA NETWORKING [Win-16] [Win-17] [Sum-18] [Win-16] [Sum-16] [Win-18] [Sum-16] [Sum-15] [Win-16] [Win-15] [Win-17] [Sum-18] Darshan Institute of Engineering & Technology 47