SlideShare ist ein Scribd-Unternehmen logo
1 von 18
NETWORKING IN JAVA   AN EFFORT BY:- ANKUR AGRAWAL B.TECH(CSE 5 th  SEM)
TCP & UDP TCP(TRANSMISSION CONTROL PROTOCOL) TCP is a connection-based protocol that provides a reliable flow of data between two computers. e.g. : Telephone call. Web browsing. UDP(USER DATAGRAM PROTOCOL) UDP  is a protocol that sends independent packets of data, called datagrams, from one computer to another with no guarantees about arrival. UDP is not connection-based like TCP. e.g.: ping command.
PORT Ports are identified by a 16-bit number. The TCP and UDP protocols use PORTS to map incoming data to a particular process running on a computer.
INTERNET ADDRESSING & DNS(DOMAIN NAME SYSTEM) Every computer on a network is known by a unique number which is known as IP Address. Generally we use IPV4 which consists of 4 BYTES. (e.g. 172.17.167.4)etc. Websites have both a friendly address, called a URL, and an IP address. People use URLs to find websites, but computers use IP addresses to find websites. DNS translates URLs into IP addresses (and vice versa).
NETWORKING CLASSES(java.net PACKAGE) InetAddress :- This class is used to encapsulate both the numerical IP address and the domain name for that address. To create an InetAddress object we have to use one of the available factory methods:- 1.static InetAddress getLocalHost( ) throws UnknownHostException 2.static InetAddress getByName(String hostName) throws UnknownHostException 3.static InetAddress[ ] getAllByName(String hostName) throws UnknownHostException
import java.net.*; public class prog1 { public static void main(String args[])throws UnknownHostException { InetAddress in; in=InetAddress.getLocalHost(); System.out.println(&quot;local host name=&quot;+in); in=InetAddress.getByName(&quot;www.google.com&quot;); System.out.println(&quot;ip of google::&quot;+in); InetAddress dn[]=InetAddress.getAllByName(&quot;www.google.com&quot;); System.out.println(&quot;ALL NAMES FOR GOOGLE&quot;); for(int i=0;i<dn.length;i++) System.out.println(dn[i]); } } PROGRAM TO ILLUSTRATE InetAddress
URL:- URL is an acronym for   Uniform Resource Locator   and is a reference (an address) to a resource on the Internet. http :// www.google.com:80/index.htm PROTOCOL IDENTIFIER RESOURCE NAME CONSTRUCTOR:- 1. URL(String urlSpecifier) 2.URL(String protocolName, String hostName, int port, String path) 3.URL(String protocolName, String hostName, String path) All of these constructor throws MalformedURLException.
PROGRAM TO ILLUSTRATE URL import java.io.*; import java.net.*; public class prog2 { public static void main(String args[])throws MalformedURLException { URL u=new URL(&quot;http://www.google.com:80/index.htm&quot;); System.out.println(&quot;HOST NAME=&quot;+u.getHost()); System.out.println(&quot;PORT =&quot;+u.getPort()); System.out.println(&quot;PROTOCOL=&quot;+u.getProtocol()); System.out.println(&quot;PATH=&quot;+u.getFile()); } }
THE openStream() method It is used for directly reading from a URL. import java.net.*;  import java.io.*;  public class prog3 {   public static void main(String[] args) throws Exception  { URL yahoo = new URL(&quot;http://www.yahoo.com/&quot;);  BufferedReader in = new BufferedReader( new  InputStreamReader( yahoo.openStream()));  String inputLine;  while ((inputLine = in.readLine()) != null) System.out.println(inputLine); In.close();  } }
READING FROM  A  URL THROUGH URLConnection OBJECT We can create URLConnection object using the    openConnection() method of URL object. import java.net.*;  import java.io.*;  public class prog4 { public static void main(String[ ] args) throws Exception  { URL yahoo = new URL(&quot;http://www.yahoo.com/&quot;);  URLConnection yc = yahoo.openConnection();  BufferedReader in = new BufferedReader( new InputStreamReader(  yc.getInputStream()));  String inputLine;  while ((inputLine = in.readLine()) != null)   System.out.println(inputLine);  in.close();  } }
SOCKET PROGRAMMING ,[object Object],[object Object],[object Object],[object Object],[object Object]
WHAT IS A SOCKET A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent.  ,[object Object],[object Object],[object Object],We can initialize a Socket object using  following constructor– Socket(String hostName, int port) We can initialize a ServerSocket object using  following constructor- ServerSocket s = new ServerSocket(port no.) It will establishes a server that monitors port no.
Java Sockets ServerSocket(1234) Socket(“128.250.25.158”, 1234) Output/write stream Input/read stream It can be host_name like “mandroo.cs.mu.oz.au” Client Server
LET’S DEAL WITH ServerSocket CLASS accept():- ServerSocket has a method called accept( ), which is a blocking call that will wait for a client to initiate communications, and then return with a normal Socket that is then used for communication with the client. e.g . ServerSocket s=new ServerSocket(8189); establishes a server that monitors port 8189.  The command    Socket sp = s.accept(); tells the program to wait indefinitely until a client connects to that port. Once someone connects to this port by sending the correct request over the network, this method returns a Socket object that represents the connection that was made.
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],PROGRAM 1:- IMPLEMENTING SERVER SOCKET AND COMMUNICATION BETWEEN CLIENT AND SERVER.(THERE IS ONLY ONE CLIENT  AT A TIME) IF MORE THAN 1 CLIENT WANT TO COMMUNIACATE THEN THEY HAVE TO WAIT TILL ONE FINISHES.
PROG2:-COMMUNIACTION BETWEEN MULTIPLE CLIENTS AND A SERVER USING SOCKETS SIMULTANEOUSLY(no need to wait).(ServerSocket) import java.io.*; import java.net.*; import java.util.*; public class prog6 { public static void  main(String args[]) throws Exception { int i=1; ServerSocket s=new ServerSocket(8120); System.out.println(&quot; SERVER WAITS FOR THE CLIENTS TO      BE CONNECTED...........&quot;); while(true) { Socket sp=s.accept(); System.out.println(&quot;client no.&quot;+i+&quot;connected&quot;); Runnable r=new netthread(sp,i); Thread t=new Thread(r); t.start(); // it begins the execution of thread beginning    at the run() method  i++; } } }
class netthread implements Runnable { Socket soc; int cl; public netthread(Socket k,int j) { soc=k; cl=j; } public void run()  { try {   try   { Scanner in=new     Scanner(soc.getInputStream()); PrintWriter out=new PrintWriter(soc.getOutputStream(),true); out.println(&quot;WRITE STH. WRITE BYE TO EXIT &quot;); Boolean f=true; while(f==true && in.hasNextLine()==true) { String line=in.nextLine(); if(line.trim().equals(&quot;BYE&quot;)) { f=false; break; } else out.println(&quot;ECHO::&quot;+line); }   }  finally { soc.close(); System.out.println(&quot;CLIENT NO.&quot;+cl+&quot;DISCONNECTED&quot;); }   }catch(IOException e) {   System.out.println(e); } } }
 

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Web Servers (ppt)
Web Servers (ppt)Web Servers (ppt)
Web Servers (ppt)
 
Servlet life cycle
Servlet life cycleServlet life cycle
Servlet life cycle
 
Socket programming using java
Socket programming using javaSocket programming using java
Socket programming using java
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Servlet
Servlet Servlet
Servlet
 
Servlets
ServletsServlets
Servlets
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
 
Hypertext transfer protocol and hypertext transfer protocol secure(HTTP and H...
Hypertext transfer protocol and hypertext transfer protocol secure(HTTP and H...Hypertext transfer protocol and hypertext transfer protocol secure(HTTP and H...
Hypertext transfer protocol and hypertext transfer protocol secure(HTTP and H...
 
System Administration DCU
System Administration DCUSystem Administration DCU
System Administration DCU
 
Java media framework
Java media frameworkJava media framework
Java media framework
 
WCF Fundamentals
WCF Fundamentals WCF Fundamentals
WCF Fundamentals
 
Telnet
TelnetTelnet
Telnet
 
Web server
Web serverWeb server
Web server
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
IIS
IISIIS
IIS
 
Windowforms controls c#
Windowforms controls c#Windowforms controls c#
Windowforms controls c#
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
ADO .Net
ADO .Net ADO .Net
ADO .Net
 

Andere mochten auch

Network Socket Programming with JAVA
Network Socket Programming with JAVANetwork Socket Programming with JAVA
Network Socket Programming with JAVADudy Ali
 
Java networking programs - theory
Java networking programs - theoryJava networking programs - theory
Java networking programs - theoryMukesh Tekwani
 
Netty Cookbook - Table of contents
Netty Cookbook - Table of contentsNetty Cookbook - Table of contents
Netty Cookbook - Table of contentsTrieu Nguyen
 
Basics of java programming language
Basics of java programming languageBasics of java programming language
Basics of java programming languagemasud33bd
 
Networking Java Socket Programming
Networking Java Socket ProgrammingNetworking Java Socket Programming
Networking Java Socket ProgrammingMousmi Pawar
 
Socket Programming Tutorial
Socket Programming TutorialSocket Programming Tutorial
Socket Programming TutorialJignesh Patel
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java ProgrammingMath-Circle
 

Andere mochten auch (9)

Network Socket Programming with JAVA
Network Socket Programming with JAVANetwork Socket Programming with JAVA
Network Socket Programming with JAVA
 
Sockets
SocketsSockets
Sockets
 
Java networking programs - theory
Java networking programs - theoryJava networking programs - theory
Java networking programs - theory
 
Netty Cookbook - Table of contents
Netty Cookbook - Table of contentsNetty Cookbook - Table of contents
Netty Cookbook - Table of contents
 
Basics of java programming language
Basics of java programming languageBasics of java programming language
Basics of java programming language
 
Networking Java Socket Programming
Networking Java Socket ProgrammingNetworking Java Socket Programming
Networking Java Socket Programming
 
Socket Programming Tutorial
Socket Programming TutorialSocket Programming Tutorial
Socket Programming Tutorial
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
 
Optical fiber
Optical fiberOptical fiber
Optical fiber
 

Ähnlich wie Networking & Socket Programming In Java

Ähnlich wie Networking & Socket Programming In Java (20)

Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Socket Programming it-slideshares.blogspot.com
Socket  Programming it-slideshares.blogspot.comSocket  Programming it-slideshares.blogspot.com
Socket Programming it-slideshares.blogspot.com
 
A.java
A.javaA.java
A.java
 
network programing lab file ,
network programing lab file ,network programing lab file ,
network programing lab file ,
 
Socket programming
Socket programming Socket programming
Socket programming
 
15network Programming Clients
15network Programming Clients15network Programming Clients
15network Programming Clients
 
Network Programming Clients
Network Programming ClientsNetwork Programming Clients
Network Programming Clients
 
Networking in Java
Networking in JavaNetworking in Java
Networking in Java
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Java 1
Java 1Java 1
Java 1
 
Pemrograman Jaringan
Pemrograman JaringanPemrograman Jaringan
Pemrograman Jaringan
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13
 
Networks lab
Networks labNetworks lab
Networks lab
 
Networks lab
Networks labNetworks lab
Networks lab
 
28 networking
28  networking28  networking
28 networking
 
Os 2
Os 2Os 2
Os 2
 
Tcp/ip server sockets
Tcp/ip server socketsTcp/ip server sockets
Tcp/ip server sockets
 

Kürzlich hochgeladen

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 

Kürzlich hochgeladen (20)

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 

Networking & Socket Programming In Java

  • 1. NETWORKING IN JAVA AN EFFORT BY:- ANKUR AGRAWAL B.TECH(CSE 5 th SEM)
  • 2. TCP & UDP TCP(TRANSMISSION CONTROL PROTOCOL) TCP is a connection-based protocol that provides a reliable flow of data between two computers. e.g. : Telephone call. Web browsing. UDP(USER DATAGRAM PROTOCOL) UDP is a protocol that sends independent packets of data, called datagrams, from one computer to another with no guarantees about arrival. UDP is not connection-based like TCP. e.g.: ping command.
  • 3. PORT Ports are identified by a 16-bit number. The TCP and UDP protocols use PORTS to map incoming data to a particular process running on a computer.
  • 4. INTERNET ADDRESSING & DNS(DOMAIN NAME SYSTEM) Every computer on a network is known by a unique number which is known as IP Address. Generally we use IPV4 which consists of 4 BYTES. (e.g. 172.17.167.4)etc. Websites have both a friendly address, called a URL, and an IP address. People use URLs to find websites, but computers use IP addresses to find websites. DNS translates URLs into IP addresses (and vice versa).
  • 5. NETWORKING CLASSES(java.net PACKAGE) InetAddress :- This class is used to encapsulate both the numerical IP address and the domain name for that address. To create an InetAddress object we have to use one of the available factory methods:- 1.static InetAddress getLocalHost( ) throws UnknownHostException 2.static InetAddress getByName(String hostName) throws UnknownHostException 3.static InetAddress[ ] getAllByName(String hostName) throws UnknownHostException
  • 6. import java.net.*; public class prog1 { public static void main(String args[])throws UnknownHostException { InetAddress in; in=InetAddress.getLocalHost(); System.out.println(&quot;local host name=&quot;+in); in=InetAddress.getByName(&quot;www.google.com&quot;); System.out.println(&quot;ip of google::&quot;+in); InetAddress dn[]=InetAddress.getAllByName(&quot;www.google.com&quot;); System.out.println(&quot;ALL NAMES FOR GOOGLE&quot;); for(int i=0;i<dn.length;i++) System.out.println(dn[i]); } } PROGRAM TO ILLUSTRATE InetAddress
  • 7. URL:- URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet. http :// www.google.com:80/index.htm PROTOCOL IDENTIFIER RESOURCE NAME CONSTRUCTOR:- 1. URL(String urlSpecifier) 2.URL(String protocolName, String hostName, int port, String path) 3.URL(String protocolName, String hostName, String path) All of these constructor throws MalformedURLException.
  • 8. PROGRAM TO ILLUSTRATE URL import java.io.*; import java.net.*; public class prog2 { public static void main(String args[])throws MalformedURLException { URL u=new URL(&quot;http://www.google.com:80/index.htm&quot;); System.out.println(&quot;HOST NAME=&quot;+u.getHost()); System.out.println(&quot;PORT =&quot;+u.getPort()); System.out.println(&quot;PROTOCOL=&quot;+u.getProtocol()); System.out.println(&quot;PATH=&quot;+u.getFile()); } }
  • 9. THE openStream() method It is used for directly reading from a URL. import java.net.*; import java.io.*; public class prog3 { public static void main(String[] args) throws Exception { URL yahoo = new URL(&quot;http://www.yahoo.com/&quot;); BufferedReader in = new BufferedReader( new InputStreamReader( yahoo.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); In.close(); } }
  • 10. READING FROM A URL THROUGH URLConnection OBJECT We can create URLConnection object using the openConnection() method of URL object. import java.net.*; import java.io.*; public class prog4 { public static void main(String[ ] args) throws Exception { URL yahoo = new URL(&quot;http://www.yahoo.com/&quot;); URLConnection yc = yahoo.openConnection(); BufferedReader in = new BufferedReader( new InputStreamReader( yc.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); } }
  • 11.
  • 12.
  • 13. Java Sockets ServerSocket(1234) Socket(“128.250.25.158”, 1234) Output/write stream Input/read stream It can be host_name like “mandroo.cs.mu.oz.au” Client Server
  • 14. LET’S DEAL WITH ServerSocket CLASS accept():- ServerSocket has a method called accept( ), which is a blocking call that will wait for a client to initiate communications, and then return with a normal Socket that is then used for communication with the client. e.g . ServerSocket s=new ServerSocket(8189); establishes a server that monitors port 8189. The command Socket sp = s.accept(); tells the program to wait indefinitely until a client connects to that port. Once someone connects to this port by sending the correct request over the network, this method returns a Socket object that represents the connection that was made.
  • 15.
  • 16. PROG2:-COMMUNIACTION BETWEEN MULTIPLE CLIENTS AND A SERVER USING SOCKETS SIMULTANEOUSLY(no need to wait).(ServerSocket) import java.io.*; import java.net.*; import java.util.*; public class prog6 { public static void main(String args[]) throws Exception { int i=1; ServerSocket s=new ServerSocket(8120); System.out.println(&quot; SERVER WAITS FOR THE CLIENTS TO BE CONNECTED...........&quot;); while(true) { Socket sp=s.accept(); System.out.println(&quot;client no.&quot;+i+&quot;connected&quot;); Runnable r=new netthread(sp,i); Thread t=new Thread(r); t.start(); // it begins the execution of thread beginning at the run() method i++; } } }
  • 17. class netthread implements Runnable { Socket soc; int cl; public netthread(Socket k,int j) { soc=k; cl=j; } public void run() { try { try { Scanner in=new Scanner(soc.getInputStream()); PrintWriter out=new PrintWriter(soc.getOutputStream(),true); out.println(&quot;WRITE STH. WRITE BYE TO EXIT &quot;); Boolean f=true; while(f==true && in.hasNextLine()==true) { String line=in.nextLine(); if(line.trim().equals(&quot;BYE&quot;)) { f=false; break; } else out.println(&quot;ECHO::&quot;+line); } } finally { soc.close(); System.out.println(&quot;CLIENT NO.&quot;+cl+&quot;DISCONNECTED&quot;); } }catch(IOException e) { System.out.println(e); } } }
  • 18.