SlideShare a Scribd company logo
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.
Most read
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.
Most read
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.
Most read
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 URL s and  URLConnection s provide a relatively high-level mechanism for accessing resources on the Internet. Sometimes your programs require lower-level network communication, for example, when you want to write a client-server application.  The communication that occurs between the client and the server must be  reliable . TCP  provides a reliable, point-to-point communication channel . To communicate over TCP, a client program and a server program establish a connection to one another.  Each program binds a  socket  to its end of the connection. To communicate, the client and the server each reads from and writes to the socket bound to the connection.
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.  In java.net package there are two classes for socket purpose. Socket ServerSocket  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.
import java.io.*; import java.net.*; import java.util.*; public class prog5 { public static void main(String args[])throws    Exception   { ServerSocket s=new ServerSocket(8120); System.out.println(“SERVER IS  WAITING FOR THE CLIENTS TO BE  CONNECTED…..”); while(true) { Socket sp=s.accept();    Scanner in=new Scanner(sp.getInputStream()); PrintWriter  out=newPrintWriter(sp.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(“BYE&quot;)) { f=false; break; } else out.Println(&quot;echo::&quot;+line) } Sp.Close(); } } } 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;\n SERVER WAITS FOR THE CLIENTS TO      BE CONNECTED...........&quot;); while(true) { Socket sp=s.accept(); System.out.println(&quot;\nclient no.&quot;+i+&quot;connected\n&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); } } }
 

More Related Content

What's hot (20)

JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
Ganesh Samarthyam
 
Java Networking
Java NetworkingJava Networking
Java Networking
Sunil OS
 
Web container and Apache Tomcat
Web container and Apache TomcatWeb container and Apache Tomcat
Web container and Apache Tomcat
Auwal Amshi
 
Cryptography for Java Developers: Nakov jProfessionals (Jan 2019)
Cryptography for Java Developers: Nakov jProfessionals (Jan 2019)Cryptography for Java Developers: Nakov jProfessionals (Jan 2019)
Cryptography for Java Developers: Nakov jProfessionals (Jan 2019)
Svetlin Nakov
 
ASP.NET Web API
ASP.NET Web APIASP.NET Web API
ASP.NET Web API
habib_786
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
Jafar Nesargi
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)
teach4uin
 
Socket programming
Socket programmingSocket programming
Socket programming
harsh_bca06
 
JDBC Java Database Connectivity
JDBC Java Database ConnectivityJDBC Java Database Connectivity
JDBC Java Database Connectivity
Ranjan Kumar
 
Java 8 streams
Java 8 streamsJava 8 streams
Java 8 streams
Manav Prasad
 
Servlet Filter
Servlet FilterServlet Filter
Servlet Filter
AshishSingh Bhatia
 
Socket programming
Socket programmingSocket programming
Socket programming
MdEmonRana
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
NAVER Engineering
 
C Esercizi Indirizzamento
C Esercizi IndirizzamentoC Esercizi Indirizzamento
C Esercizi Indirizzamento
acapone
 
Oops in java
Oops in javaOops in java
Oops in java
baabtra.com - No. 1 supplier of quality freshers
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
Information Technology
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
Joshua Long
 
Introduction to JSON & AJAX
Introduction to JSON & AJAXIntroduction to JSON & AJAX
Introduction to JSON & AJAX
Collaboration Technologies
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
Ganesh Samarthyam
 
Java Networking
Java NetworkingJava Networking
Java Networking
Sunil OS
 
Web container and Apache Tomcat
Web container and Apache TomcatWeb container and Apache Tomcat
Web container and Apache Tomcat
Auwal Amshi
 
Cryptography for Java Developers: Nakov jProfessionals (Jan 2019)
Cryptography for Java Developers: Nakov jProfessionals (Jan 2019)Cryptography for Java Developers: Nakov jProfessionals (Jan 2019)
Cryptography for Java Developers: Nakov jProfessionals (Jan 2019)
Svetlin Nakov
 
ASP.NET Web API
ASP.NET Web APIASP.NET Web API
ASP.NET Web API
habib_786
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
Jafar Nesargi
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)
teach4uin
 
Socket programming
Socket programmingSocket programming
Socket programming
harsh_bca06
 
JDBC Java Database Connectivity
JDBC Java Database ConnectivityJDBC Java Database Connectivity
JDBC Java Database Connectivity
Ranjan Kumar
 
Socket programming
Socket programmingSocket programming
Socket programming
MdEmonRana
 
C Esercizi Indirizzamento
C Esercizi IndirizzamentoC Esercizi Indirizzamento
C Esercizi Indirizzamento
acapone
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
Joshua Long
 

Viewers also liked (8)

Network Socket Programming with JAVA
Network Socket Programming with JAVANetwork Socket Programming with JAVA
Network Socket Programming with JAVA
Dudy Ali
 
Sockets
SocketsSockets
Sockets
sivindia
 
Java networking programs - theory
Java networking programs - theoryJava networking programs - theory
Java networking programs - theory
Mukesh Tekwani
 
Netty Cookbook - Table of contents
Netty Cookbook - Table of contentsNetty Cookbook - Table of contents
Netty Cookbook - Table of contents
Trieu Nguyen
 
Basics of java programming language
Basics of java programming languageBasics of java programming language
Basics of java programming language
masud33bd
 
Networking Java Socket Programming
Networking Java Socket ProgrammingNetworking Java Socket Programming
Networking Java Socket Programming
Mousmi Pawar
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
Math-Circle
 
Optical fiber
Optical fiberOptical fiber
Optical fiber
Math-Circle
 
Network Socket Programming with JAVA
Network Socket Programming with JAVANetwork Socket Programming with JAVA
Network Socket Programming with JAVA
Dudy Ali
 
Java networking programs - theory
Java networking programs - theoryJava networking programs - theory
Java networking programs - theory
Mukesh Tekwani
 
Netty Cookbook - Table of contents
Netty Cookbook - Table of contentsNetty Cookbook - Table of contents
Netty Cookbook - Table of contents
Trieu Nguyen
 
Basics of java programming language
Basics of java programming languageBasics of java programming language
Basics of java programming language
masud33bd
 
Networking Java Socket Programming
Networking Java Socket ProgrammingNetworking Java Socket Programming
Networking Java Socket Programming
Mousmi Pawar
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
Math-Circle
 
Ad

Similar to Networking & Socket Programming In Java (20)

Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
arnold 7490
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
Tushar B Kute
 
A.java
A.javaA.java
A.java
JahnaviBhagat
 
Networking in Java
Networking in JavaNetworking in Java
Networking in Java
Tushar B Kute
 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programming
ashok hirpara
 
28 networking
28  networking28  networking
28 networking
Ravindra Rathore
 
Socket Programming it-slideshares.blogspot.com
Socket  Programming it-slideshares.blogspot.comSocket  Programming it-slideshares.blogspot.com
Socket Programming it-slideshares.blogspot.com
phanleson
 
5_6278455688045789623.pptx
5_6278455688045789623.pptx5_6278455688045789623.pptx
5_6278455688045789623.pptx
EliasPetros
 
Java networking
Java networkingJava networking
Java networking
Arati Gadgil
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
Nitish Nagar
 
Socket Programming in Java.ppt yeh haii
Socket Programming in Java.ppt  yeh haiiSocket Programming in Java.ppt  yeh haii
Socket Programming in Java.ppt yeh haii
inambscs4508
 
javanetworking
javanetworkingjavanetworking
javanetworking
Arjun Shanka
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
Rakesh Madugula
 
Chapter 4 slides
Chapter 4 slidesChapter 4 slides
Chapter 4 slides
lara_ays
 
Please look at the attach See.doc. I am getting this error all th.docx
Please look at the attach See.doc. I am getting this error all th.docxPlease look at the attach See.doc. I am getting this error all th.docx
Please look at the attach See.doc. I am getting this error all th.docx
randymartin91030
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project Report
Kavita Sharma
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
Ebisa Bekele
 
Socket & Server Socket
Socket & Server SocketSocket & Server Socket
Socket & Server Socket
Hemant Chetwani
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
Tushar B Kute
 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programming
ashok hirpara
 
Socket Programming it-slideshares.blogspot.com
Socket  Programming it-slideshares.blogspot.comSocket  Programming it-slideshares.blogspot.com
Socket Programming it-slideshares.blogspot.com
phanleson
 
5_6278455688045789623.pptx
5_6278455688045789623.pptx5_6278455688045789623.pptx
5_6278455688045789623.pptx
EliasPetros
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
Nitish Nagar
 
Socket Programming in Java.ppt yeh haii
Socket Programming in Java.ppt  yeh haiiSocket Programming in Java.ppt  yeh haii
Socket Programming in Java.ppt yeh haii
inambscs4508
 
Chapter 4 slides
Chapter 4 slidesChapter 4 slides
Chapter 4 slides
lara_ays
 
Please look at the attach See.doc. I am getting this error all th.docx
Please look at the attach See.doc. I am getting this error all th.docxPlease look at the attach See.doc. I am getting this error all th.docx
Please look at the attach See.doc. I am getting this error all th.docx
randymartin91030
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project Report
Kavita Sharma
 
Ad

Recently uploaded (20)

Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free DownloadViral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
Edge AI and Vision Alliance
 
Secure Access with Azure Active Directory
Secure Access with Azure Active DirectorySecure Access with Azure Active Directory
Secure Access with Azure Active Directory
VICTOR MAESTRE RAMIREZ
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free DownloadViral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
Edge AI and Vision Alliance
 
Secure Access with Azure Active Directory
Secure Access with Azure Active DirectorySecure Access with Azure Active Directory
Secure Access with Azure Active Directory
VICTOR MAESTRE RAMIREZ
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 

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. SOCKET PROGRAMMING URL s and URLConnection s provide a relatively high-level mechanism for accessing resources on the Internet. Sometimes your programs require lower-level network communication, for example, when you want to write a client-server application. The communication that occurs between the client and the server must be reliable . TCP provides a reliable, point-to-point communication channel . To communicate over TCP, a client program and a server program establish a connection to one another. Each program binds a socket to its end of the connection. To communicate, the client and the server each reads from and writes to the socket bound to the connection.
  • 12. 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. In java.net package there are two classes for socket purpose. Socket ServerSocket 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.
  • 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. import java.io.*; import java.net.*; import java.util.*; public class prog5 { public static void main(String args[])throws Exception { ServerSocket s=new ServerSocket(8120); System.out.println(“SERVER IS WAITING FOR THE CLIENTS TO BE CONNECTED…..”); while(true) { Socket sp=s.accept(); Scanner in=new Scanner(sp.getInputStream()); PrintWriter out=newPrintWriter(sp.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(“BYE&quot;)) { f=false; break; } else out.Println(&quot;echo::&quot;+line) } Sp.Close(); } } } 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.
  • 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;\n SERVER WAITS FOR THE CLIENTS TO BE CONNECTED...........&quot;); while(true) { Socket sp=s.accept(); System.out.println(&quot;\nclient no.&quot;+i+&quot;connected\n&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.