SlideShare a Scribd company logo
Java Networking
Chapter 4
java networking
 Java Networking is a concept of connecting two
or more computing devices together so that we
can share resources. Java socket programming
provides facility to share data between different
computing devices.
Advantage of Java Networking
1. sharing resources
2. centralize software management
Java Networking Terminology
 The widely used java networking terminologies are
given below:
1. IP Address
2. Protocol
3. Port Number
4. MAC Address
5. Connection-oriented and connection-less protocol
6. Socket
1) IP Address
IP address is a unique number assigned to a node of a
network e.g. 192.168.0.1 . It is composed of octets that
range from 0 to 255.
It is a logical address that can be changed.
2) Protocol
A protocol is a set of rules basically that is followed for
communication. For example:
TCP FTP Telnet SMTP POP etc.
3) Port Number
The port number is used to uniquely identify different
applications. It acts as a communication endpoint
between applications.
The port number is associated with the IP address for
communication between two applications.
4) MAC Address
MAC (Media Access Control) Address is a unique
identifier of NIC (Network Interface Controller). A network
node can have multiple NIC but each with unique MAC.
5) Connection-oriented and connection-less protocol
In connection-oriented protocol, acknowledgement is sent
by the receiver. So it is reliable but slow. The example of
connection-oriented protocol is TCP. But, in connection-
less protocol, acknowledgement is not sent by the
receiver. So it is not reliable but fast. The example of
connection-less protocol is UDP.
6) Socket
A socket is an endpoint between two way
communications.
Java Socket Programming
 Java Socket programming is used for communication
between the applications running on different JRE.
Java Socket programming can be connection-oriented
or connection-less.
 Socket and ServerSocket classes are used for
connection-oriented socket programming and
DatagramSocket and DatagramPacket classes are
used for connection-less socket programming.
 The client in socket programming must know two
information:
1. IP Address of Server, and
2. Port number.
Socket class
 A socket is simply an endpoint for communications
between the machines. The Socket class can be
used to create a socket.
Important methods
ServerSocket class
 The ServerSocket class can be used to create a
server socket. This object is used to establish
communication with the clients.
Important methods
Example of Java Socket
Programming
 Let's see a simple of java socket programming in which
client sends a text and server receives it.
File: MyServer.java
1. import java.io.*;
2. import java.net.*;
3. public class MyServer {
4. public static void main(String[] args){
5. try{
6. ServerSocket ss=new ServerSocket(6666);
7. Socket s=ss.accept();//establishes connection
8. DataInputStream dis=new
DataInputStream(s.getInputStream());
9. String str=(String)dis.readUTF();
10. System.out.println("message= "+str);
11. ss.close();
12. }catch(Exception e){System.out.println(e);} } }
 File: MyClient.java
1. import java.io.*;
2. import java.net.*;
3. public class MyClient {
4. public static void main(String[] args) {
5. try{
6. Socket s=new Socket("localhost",6666);
7. DataOutputStream dout=new
DataOutputStream(s.getOutputStream());
8. dout.writeUTF("Hello Server");
9. dout.flush();
10. dout.close();
11. s.close();
12. }catch(Exception e){System.out.println(e);} } }
Java URL
 The Java URL class represents an URL. URL is an
acronym for Uniform Resource Locator. It points to a
resource on the World Wide Web.
For example: 1. http://www.javatpoint.com/java-
tutorial
 A URL contains many information:
1. Protocol: In this case, http is the protocol.
2. Server name or IP Address: In this case,
www.javatpoint.com is the server name.
3. Port Number: It is an optional attribute. If we write
http//ww.javatpoint.com:80/sonoojaiswal/ , 80 is the
port number. If port number is not mentioned in the
URL, it returns -1. 4. File Name or directory name: In
this case, index.jsp is the file name.

Commonly used methods of Java
URL class
 The java.net.URL class provides many methods.
The important methods of URL class are given
below.
 Example of Java URL class
1. //URLDemo.java
2. import java.io.*;
3. import java.net.*;
4. public class URLDemo{
5. public static void main(String[] args){
6. try{
7. URL url=new URL("http://www.javatpoint.com/java-tutorial");
8. System.out.println("Protocol: "+url.getProtocol());
9. System.out.println("Host Name: "+url.getHost());
10. System.out.println("Port Number: "+url.getPort());
11. System.out.println("File Name: "+url.getFile());
12. }catch(Exception e){System.out.println(e);}
13. }
14. } Output: Protocol: http
Host Name: www.javatpoint.com
Port Number: -1
File Name: /java-tutorial

More Related Content

What's hot

Java API: java.net.InetAddress
Java API: java.net.InetAddressJava API: java.net.InetAddress
Java API: java.net.InetAddress
Sayak Sarkar
 
Java Networking
Java NetworkingJava Networking
Java Networking
Sunil OS
 
Python network programming
Python   network programmingPython   network programming
Python network programming
Learnbay Datascience
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
Java Networking
Java NetworkingJava Networking
Java Networking
68SachinYadavSYCS
 
Networking in python by Rj
Networking in python by RjNetworking in python by Rj
Networking in Java
Networking in JavaNetworking in Java
Networking in Java
Tushar B Kute
 
Java Networking
Java NetworkingJava Networking
Java Networking
Ankit Desai
 
Networking in java
Networking in javaNetworking in java
Networking in java
shravan kumar upadhayay
 
Java networking programs - theory
Java networking programs - theoryJava networking programs - theory
Java networking programs - theory
Mukesh Tekwani
 
Socket programming
Socket programmingSocket programming
Socket programming
NemiRathore
 
Build a Micro HTTP Server for Embedded System
Build a Micro HTTP Server for Embedded SystemBuild a Micro HTTP Server for Embedded System
Build a Micro HTTP Server for Embedded System
Jian-Hong Pan
 
java networking
 java networking java networking
java networking
Waheed Warraich
 
Socket programming
Socket programmingSocket programming
Socket programming
MdEmonRana
 
Networking Java Socket Programming
Networking Java Socket ProgrammingNetworking Java Socket Programming
Networking Java Socket ProgrammingMousmi Pawar
 
Java
Java Java
What is Socket Programming in Python | Edureka
What is Socket Programming in Python | EdurekaWhat is Socket Programming in Python | Edureka
What is Socket Programming in Python | Edureka
Edureka!
 
Socket programming in Java (PPTX)
Socket programming in Java (PPTX)Socket programming in Java (PPTX)
Socket programming in Java (PPTX)UC San Diego
 
The Considerations for Internet of Things @ 2017
The Considerations for Internet of Things @ 2017The Considerations for Internet of Things @ 2017
The Considerations for Internet of Things @ 2017
Jian-Hong Pan
 

What's hot (20)

Java API: java.net.InetAddress
Java API: java.net.InetAddressJava API: java.net.InetAddress
Java API: java.net.InetAddress
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Python network programming
Python   network programmingPython   network programming
Python network programming
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Networking in python by Rj
Networking in python by RjNetworking in python by Rj
Networking in python by Rj
 
Networking in Java
Networking in JavaNetworking in Java
Networking in Java
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Networking in java
Networking in javaNetworking in java
Networking in java
 
Java networking programs - theory
Java networking programs - theoryJava networking programs - theory
Java networking programs - theory
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Build a Micro HTTP Server for Embedded System
Build a Micro HTTP Server for Embedded SystemBuild a Micro HTTP Server for Embedded System
Build a Micro HTTP Server for Embedded System
 
java networking
 java networking java networking
java networking
 
Python networking
Python networkingPython networking
Python networking
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Networking Java Socket Programming
Networking Java Socket ProgrammingNetworking Java Socket Programming
Networking Java Socket Programming
 
Java
Java Java
Java
 
What is Socket Programming in Python | Edureka
What is Socket Programming in Python | EdurekaWhat is Socket Programming in Python | Edureka
What is Socket Programming in Python | Edureka
 
Socket programming in Java (PPTX)
Socket programming in Java (PPTX)Socket programming in Java (PPTX)
Socket programming in Java (PPTX)
 
The Considerations for Internet of Things @ 2017
The Considerations for Internet of Things @ 2017The Considerations for Internet of Things @ 2017
The Considerations for Internet of Things @ 2017
 

Similar to Chapter 4

Basic Networking in Java
Basic Networking in JavaBasic Networking in Java
Basic Networking in Java
suraj pandey
 
Java seminar.pptx
Java seminar.pptxJava seminar.pptx
Java seminar.pptx
shirindigitel
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
kamal kotecha
 
A.java
A.javaA.java
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
Lecture25
Lecture25Lecture25
Module 1 networking basics-2
Module 1   networking basics-2Module 1   networking basics-2
Module 1 networking basics-2
Ankit Dubey
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
Nitish Nagar
 
Unit8 java
Unit8 javaUnit8 java
Unit8 javamrecedu
 
Tcp/ip server sockets
Tcp/ip server socketsTcp/ip server sockets
Tcp/ip server sockets
rajshreemuthiah
 
Socket programming
Socket programmingSocket programming
Socket programming
Padmavathione
 
Advanced Java Topics
Advanced Java TopicsAdvanced Java Topics
Advanced Java Topics
Salahaddin University-Erbil
 
Socket Programming by Rajkumar Buyya
Socket Programming by Rajkumar BuyyaSocket Programming by Rajkumar Buyya
Socket Programming by Rajkumar Buyya
iDhawalVaja
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project ReportKavita Sharma
 
5_6278455688045789623.pptx
5_6278455688045789623.pptx5_6278455688045789623.pptx
5_6278455688045789623.pptx
EliasPetros
 
IPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriya
IPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriyaIPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriya
IPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriya
VijiPriya Jeyamani
 

Similar to Chapter 4 (20)

Basic Networking in Java
Basic Networking in JavaBasic Networking in Java
Basic Networking in Java
 
Java seminar.pptx
Java seminar.pptxJava seminar.pptx
Java seminar.pptx
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
A.java
A.javaA.java
A.java
 
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
 
Lecture25
Lecture25Lecture25
Lecture25
 
Module 1 networking basics-2
Module 1   networking basics-2Module 1   networking basics-2
Module 1 networking basics-2
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
Sockets
SocketsSockets
Sockets
 
Unit8 java
Unit8 javaUnit8 java
Unit8 java
 
28 networking
28  networking28  networking
28 networking
 
Tcp/ip server sockets
Tcp/ip server socketsTcp/ip server sockets
Tcp/ip server sockets
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Advanced Java Topics
Advanced Java TopicsAdvanced Java Topics
Advanced Java Topics
 
Socket Programming by Rajkumar Buyya
Socket Programming by Rajkumar BuyyaSocket Programming by Rajkumar Buyya
Socket Programming by Rajkumar Buyya
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project Report
 
5_6278455688045789623.pptx
5_6278455688045789623.pptx5_6278455688045789623.pptx
5_6278455688045789623.pptx
 
IPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriya
IPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriyaIPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriya
IPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriya
 

Recently uploaded

Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
Vlad Stirbu
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.
ViralQR
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 

Recently uploaded (20)

Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 

Chapter 4

  • 2. java networking  Java Networking is a concept of connecting two or more computing devices together so that we can share resources. Java socket programming provides facility to share data between different computing devices. Advantage of Java Networking 1. sharing resources 2. centralize software management
  • 3. Java Networking Terminology  The widely used java networking terminologies are given below: 1. IP Address 2. Protocol 3. Port Number 4. MAC Address 5. Connection-oriented and connection-less protocol 6. Socket
  • 4. 1) IP Address IP address is a unique number assigned to a node of a network e.g. 192.168.0.1 . It is composed of octets that range from 0 to 255. It is a logical address that can be changed. 2) Protocol A protocol is a set of rules basically that is followed for communication. For example: TCP FTP Telnet SMTP POP etc. 3) Port Number The port number is used to uniquely identify different applications. It acts as a communication endpoint between applications. The port number is associated with the IP address for communication between two applications.
  • 5. 4) MAC Address MAC (Media Access Control) Address is a unique identifier of NIC (Network Interface Controller). A network node can have multiple NIC but each with unique MAC. 5) Connection-oriented and connection-less protocol In connection-oriented protocol, acknowledgement is sent by the receiver. So it is reliable but slow. The example of connection-oriented protocol is TCP. But, in connection- less protocol, acknowledgement is not sent by the receiver. So it is not reliable but fast. The example of connection-less protocol is UDP. 6) Socket A socket is an endpoint between two way communications.
  • 6. Java Socket Programming  Java Socket programming is used for communication between the applications running on different JRE. Java Socket programming can be connection-oriented or connection-less.  Socket and ServerSocket classes are used for connection-oriented socket programming and DatagramSocket and DatagramPacket classes are used for connection-less socket programming.  The client in socket programming must know two information: 1. IP Address of Server, and 2. Port number.
  • 7. Socket class  A socket is simply an endpoint for communications between the machines. The Socket class can be used to create a socket. Important methods
  • 8. ServerSocket class  The ServerSocket class can be used to create a server socket. This object is used to establish communication with the clients. Important methods
  • 9. Example of Java Socket Programming  Let's see a simple of java socket programming in which client sends a text and server receives it. File: MyServer.java 1. import java.io.*; 2. import java.net.*; 3. public class MyServer { 4. public static void main(String[] args){ 5. try{ 6. ServerSocket ss=new ServerSocket(6666); 7. Socket s=ss.accept();//establishes connection 8. DataInputStream dis=new DataInputStream(s.getInputStream()); 9. String str=(String)dis.readUTF(); 10. System.out.println("message= "+str); 11. ss.close(); 12. }catch(Exception e){System.out.println(e);} } }
  • 10.  File: MyClient.java 1. import java.io.*; 2. import java.net.*; 3. public class MyClient { 4. public static void main(String[] args) { 5. try{ 6. Socket s=new Socket("localhost",6666); 7. DataOutputStream dout=new DataOutputStream(s.getOutputStream()); 8. dout.writeUTF("Hello Server"); 9. dout.flush(); 10. dout.close(); 11. s.close(); 12. }catch(Exception e){System.out.println(e);} } }
  • 11. Java URL  The Java URL class represents an URL. URL is an acronym for Uniform Resource Locator. It points to a resource on the World Wide Web. For example: 1. http://www.javatpoint.com/java- tutorial  A URL contains many information: 1. Protocol: In this case, http is the protocol. 2. Server name or IP Address: In this case, www.javatpoint.com is the server name. 3. Port Number: It is an optional attribute. If we write http//ww.javatpoint.com:80/sonoojaiswal/ , 80 is the port number. If port number is not mentioned in the URL, it returns -1. 4. File Name or directory name: In this case, index.jsp is the file name. 
  • 12. Commonly used methods of Java URL class  The java.net.URL class provides many methods. The important methods of URL class are given below.
  • 13.  Example of Java URL class 1. //URLDemo.java 2. import java.io.*; 3. import java.net.*; 4. public class URLDemo{ 5. public static void main(String[] args){ 6. try{ 7. URL url=new URL("http://www.javatpoint.com/java-tutorial"); 8. System.out.println("Protocol: "+url.getProtocol()); 9. System.out.println("Host Name: "+url.getHost()); 10. System.out.println("Port Number: "+url.getPort()); 11. System.out.println("File Name: "+url.getFile()); 12. }catch(Exception e){System.out.println(e);} 13. } 14. } Output: Protocol: http Host Name: www.javatpoint.com Port Number: -1 File Name: /java-tutorial