SlideShare una empresa de Scribd logo
1 de 33
Network Programming
http://www.java2all.com
Introduction
http://www.java2all.com
Network Programming Introduction
Java supports Network Programming to communicate
with other machines.
Let`s start with Network Programming Introduction.
http://www.java2all.com
Network Programming Introduction
As we all know that Computer Network means a group of
computers connect with each other via some medium and transfer
data between them as and when require.
Java supports Network Programming so we can make such
program in which the machines connected in network will send and
receive data from other machine in the network by programming.
The first and simple logic to send or receive any kind of data
or message is we must have the address of receiver or sender. So
when a computer needs to communicate with another computer, it`s
require the other computer’s address.
Java networking programming supports the concept of
socket. A socket identifies an endpoint in a network. The socket
communication takes place via a protocol.
http://www.java2all.com
The Internet Protocol is a lower-level, connection less (means
there is no continuing connection between the end points) protocol for
delivering the data into small packets from one computer (address) to
another computer (address) across the network (Internet). It does not
guarantee to deliver sent packets to the destination.
The most widely use a version of IP today is IPv4, uses a 32 bit
value to represent an address which are organized into four 8-bits chunks.
However new addressing scheme called IPv6, uses a 128 bit value to
represent an address which are organized into four 16-bits chunks. The
main advantage of IPv6 is that it supports much larger address space than
does IPv4. An IP (Internet Protocol) address uniquely identifies the
computer on the network.
IP addresses are written in a notation using numbers separated by
dots is called dotted-decimal notation. There are four 8 bits value between
0 and 255 are available in each IP address such as 127.0.0.1 means local-
host, 192.168.0.3 etc.
http://www.java2all.com
It`s not an easy to remember because of so many numbers, they
are often mapped to meaningful names called domain names such as
mail.google.com There is a server on Internet who is translate the host
names into IP addresses is called DNS (Domain Name Server).
NOTE: Internet is the global network of millions of computer and
the any computer may connect the Internet through LAN (Local Area
Network), Cable Modem, ISP (Internet Service Provider) using dialup.
When a user pass the URL like java2all.com in the web-browser
from any computer, it first ask to DNS to translate this domain name into
the numeric IP address and then sends the request to this IP address. This
enables users to work with domain names, but the internet operates on IP
addresses.
Here in java2all.com the “com” domain is reserved for commercial
sites; then “java2all” is the company name.
http://www.java2all.com
The Higher-level protocol used in with the IP are TCP (Transmission
Control Protocol) and UDP (User Datagram Protocol).
The TCP enables two host to make a connection and exchange the
stream of data, so it`s called Stream-based communication. TCP guarantees
delivery of data and also guarantees that streams of data will be delivered
in the same order in which they are sent. The TCP can detect the lost of
transmission and so resubmit them and hence the transmissions are
lossless and reliable.
The UDP is a standard, directly to support fast, connectionless host-
to-host datagram oriented model that is used over the IP and exchange the
packet of data so it`s called packet-based communication. The UDP cannot
guarantee lossless transmission.
JAVA supports both TCP and UDP protocol families.
http://www.java2all.com
Java InetAddress Class
http://www.java2all.com
Java InetAddress Class is used to encapsulate the two thing.
1. Numeric IP Address
2. The domain name for that address.
The InetAddress can handle both IPv4 and IPv6 addressses. It has
no visible constructors so to create its object, the user have to use one of
the available in-built static methods.
The commonly used InetAddress in-built methods are:
(1) getLocalHost(): It returns the InetAddress object that represents the
local host contain the name and address both. If this method unable to
find out the host name, it throw an UnknownHostException.
Syntax:
Static InetAddress getLocalHost() throws UnknownHostException
http://www.java2all.com
(2) getByName(): It returns an InetAddress for a host name passed to it as
a parameter argument. If this method unable to find out the host name, it
throw an UnknownHostException.
Syntax:
Static InetAddress getByName(String host_name) throws
UnknownHostException
(3) getAllByName(): It returns an array of an InetAddress that represent all
of the addresses that a particular name resolve to it. If this method can’t
find out the name to at least one address, it throw an
UnknownHostException.
Syntax:
Static InetAddress[] getAllByName(String host_name) throws
UnknownHostException
http://www.java2all.com
Program: Write down a program which demonstrate an InetAddress class.
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddress_Demo
{
public static void main(String[] args)
{
String name = “”;
try {
System.out.println(“HOST NAME - Numeric Address : “+InetAddress.getLocalHost());
InetAddress ip = InetAddress.getByName(name);
System.out.println(“HOST DEFAULT-NAME / IP :”+ip);
System.out.println(“HOST IP-ADDRESS : “+ip.getHostAddress());
System.out.println(“HOST DEFAULT-NAME : “+ip.getHostName());
}
catch (UnknownHostException e) {
System.out.println(“Not find the IP-ADDRESS for :”+name);
}
}
}
Output:
HOST NAME - Numeric Address : Ashutosh-
2c89cd5e0a/127.0.0.1
HOST DEFAULT-NAME / IP : localhost/127.0.0.1
HOST IP-ADDRESS : 127.0.0.1
HOST DEFAULT-NAME : localhost
http://www.java2all.com
socket programming in java
http://www.java2all.com
socket programming in java is very important topic and concept of
network programming.
Java network Programming supports the concept of socket. A
socket identifies an endpoint in a network. The socket communication take
place via a protocol.
A socket can be used to connect JAVA Input/Output system to other
programs that may resides either on any machine on the Internet or on the
local machine.
http://www.java2all.com
TCP/IP Sockets:
TCP/IP sockets are used to implement point-to-point, reliable,
bidirectional, stream-based connections between hosts on the Internet.
There are two types of TCP sockets available in java:
(1) TCP/IP Client Socket
(2) TCP/IP Server Socket
(1) TCP/IP Client Socket:
The Socket class (available in java.net package) is for the Client Socket.
It is designed to connect to server sockets and initiate protocol exchange.
There are two constructers used to create client sockets type object.
(a) Socket(String host_name,int port) throws
UnknownHostException,IOException it creates a socket that is
connected to the given host_name and port number.
http://www.java2all.com
(b) Socket(InetAddress ip,int port) throws IOException it creates a
socket using a pre-existing InetAddress object and a port number.
(2) TCP/IP Server Socket:
The ServerSocket class (available in java.net package) is for the
Server. It is designed to be a “listener”, which waits for clients to connect
before doing anything and that listen for either local or remote client
programs to connect to them on given port.
When you create ServerSocket it will register itself with the system
as having an interest in client connection.
Syntax:
ServerSocket(int port) throws IOException
http://www.java2all.com
Program: Write down a program which demonstrate the Socket programming
for passing the message from server to client.
Client.java:
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) {
System.out.println(“Sending a request.....”);
try {
Socket s = new Socket(“127.0.0.1”,1564);
System.out.println(“connected successfully.....”);
BufferedReader br = new BufferedReader(new
InputStreamReader(s.getInputStream()));
System.out.println(“response from server...”);
System.out.println(“Client side : “+br.readLine()); s.close();
}
catch (UnknownHostException e) {
System.out.println(“Not find the IP-ADDRESS for :”+e);
}
catch (IOException e) {
System.out.println(“Not Found data for Socket : “+e);
}
}
}
http://www.java2all.com
Server.java:
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
public static void main(String[] args)
{
try
{
ServerSocket ss = new ServerSocket(1564);
System.out.println("waiting for request....");
Socket s = ss.accept();
System.out.println("Request accepted");
PrintStream ps = new PrintStream(s.getOutputStream());
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Input the data at server : ");
ps.print(br.readLine());
s.close();
ss.close();
}
catch (Exception e)
{
System.out.println("Not Found data for Socket : "+e);
}
}
} http://www.java2all.com
For Output follow the below step:
(1) Run server.java
Console:
waiting for request....
(2) Run Client.java
Console:
waiting for request....
Request accepted
Input the data at server:
(3) Now enter the message at console
Input the data at server:
welcome at server
(4) Then press Enter.
http://www.java2all.com
(5) Sending a request.....
connected successfully.....
response from server...
Client side: welcome at server
Program 2: Write down a program for addition the two different
variable by Socket programming.
Program 3: Write down a program which demonstrate the Socket
programming for passing the message from client to server and also apply
EXIT properties.
http://www.java2all.com
java socket programming example
http://www.java2all.com
Two variable addition and passing message from client to server
two different java socket programming example is given below but before
going through program directly you should have some knowledge about
Java Network Programming and Socket.
These both things are already available in previous chapter so you
can learn from there.
Now let`s move to program 1.
http://www.java2all.com
Program: Write down a program for addition the two different
variable by Socket programming.
Client_Addition.java:
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client_Addition
{
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1",1868);
PrintStream ps = new PrintStream(s.getOutputStream());
Scanner sc = new Scanner(System.in);
System.out.println("Enter first value: ");
int i1 = sc.nextInt();
ps.println(i1);
ps.flush();
System.out.println("Enter second value: ");
int i2 = sc.nextInt();
ps.println(i2);
ps.flush();
s.close();
}
catch (UnknownHostException e) {
System.out.println("Not find the IP-ADDRESS for :"+e);
}
catch (IOException e) {
System.out.println("Not Found data for Socket : "+e);
}
}
http://www.java2all.com
Server_Addition.java
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server_Addition
{
public static void main(String[] args)
{
try
{
System.out.println("Server run successfully......");
ServerSocket sc = new ServerSocket(1868);
Socket s = sc.accept();
BufferedReader br = new BufferedReader(new
InputStreamReader(s.getInputStream()));
int i1 = Integer.parseInt(br.readLine());
int i2 = Integer.parseInt(br.readLine());
System.out.println("Addition: "+(i1+i2));
s.close();
sc.close();
}
catch (IOException e)
{
System.out.println("Not Found data for Socket : "+e);
}
}
} http://www.java2all.com
For Output follow the below step:
(1) Run Server_Addition.java
Console:
Server run successfully......
(2) Run Client.java
Console:
Enter first value:
5
Enter second value:
25
(3) Now, press Enter
(4) Server run successfully......
Addition: 30
http://www.java2all.com
java socket programming example 2:
Program: Write down a program which demonstrate the Socket
programming for passing the message from client to server and also
apply EXIT properties.
Client.java:
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client
{
public static void main(String[] args) {
System.out.println("Sending a request.....");
Try {
Socket s = new Socket("127.0.0.1",1235);
System.out.println("connected successfully.....");
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
PrintStream ps = new PrintStream(s.getOutputStream());
BufferedReader brs = new BufferedReader(new
InputStreamReader(s.getInputStream()));
while(true)
{
System.out.println("input the data....");
String st = br.readLine();
ps.println(st);
http://www.java2all.com
if(st.equals("exit"))
{
System.exit(1);
}
System.out.println("data returned");
System.out.println(st);
}
}
catch (UnknownHostException e)
{
System.out.println("Not find the IP-ADDRESS for :"+e);
}
catch (IOException e)
{
System.out.println("Not Found data for Socket : "+e);
}
}
}
http://www.java2all.com
Server.java:
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(1235);
System.out.println("waiting for request....");
Socket s = ss.accept();
System.out.println("Request accepted");
BufferedReader br = new BufferedReader(new
InputStreamReader(s.getInputStream()));
while(true)
{
String st = br.readLine();
if(st.equals("exit")==true)
{
System.out.println("connection lost.....");
System.exit(1);
}
System.out.println("Message from client: "+st);
}
}
catch (IOException e) {
System.out.println("Not Found data for Socket : "+e);
}
}
}
http://www.java2all.com
For Output follow the below step:
(1) Put the both file in the bin folder at jdk.
For example: C:Program Files (x86)Javajdk1.6.0bin.
(2) Open Command Prompt & reach up to bin path.
http://www.java2all.com
(3) Compile the Server.java & Client.java
…bin>javac Server.java
…bin>javac Client.java
(4) Run the Server.java
…bin>java Server
http://www.java2all.com
(5) Open new command prompt:
(6) Now revise step-2.
(7) Run the Client.java.
…bin>java Client
http://www.java2all.com
Check the Message at Server Side Command Prompt.
(8) Write down the message on Client Side Command Prompt Like:
Input the data…
Ashutosh
(9) Now Press Enter & Check the Output at Both Windows.
http://www.java2all.com
http://www.java2all.com
(10) If want to Exit then type exit on Client side Window.
Like: Input the data…
exit
http://www.java2all.com

Más contenido relacionado

La actualidad más candente (20)

Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Servlet life cycle
Servlet life cycleServlet life cycle
Servlet life cycle
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java applet
Java appletJava applet
Java applet
 
Servlets
ServletsServlets
Servlets
 
Files in java
Files in javaFiles in java
Files in java
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
Json
JsonJson
Json
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Java IO
Java IOJava IO
Java IO
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Applets
AppletsApplets
Applets
 
ADO .Net
ADO .Net ADO .Net
ADO .Net
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
Interface in java
Interface in javaInterface in java
Interface in java
 

Destacado

Destacado (8)

Java I/O
Java I/OJava I/O
Java I/O
 
Ppt of socket
Ppt of socketPpt of socket
Ppt of socket
 
Handling I/O in Java
Handling I/O in JavaHandling I/O in Java
Handling I/O in Java
 
Socket programming-tutorial-sk
Socket programming-tutorial-skSocket programming-tutorial-sk
Socket programming-tutorial-sk
 
A Short Java Socket Tutorial
A Short Java Socket TutorialA Short Java Socket Tutorial
A Short Java Socket Tutorial
 
Socket Programming Tutorial
Socket Programming TutorialSocket Programming Tutorial
Socket Programming Tutorial
 
Networking Java Socket Programming
Networking Java Socket ProgrammingNetworking Java Socket Programming
Networking Java Socket Programming
 
Socket programming
Socket programmingSocket programming
Socket programming
 

Similar a Network programming in java - PPT

Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13Sasi Kala
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in JavaTushar B Kute
 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programmingashok hirpara
 
Basic Networking in Java
Basic Networking in JavaBasic Networking in Java
Basic Networking in Javasuraj pandey
 
Networking & Socket Programming In Java
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In JavaAnkur Agrawal
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in JavaTushar B Kute
 
Module 1 networking basics-2
Module 1   networking basics-2Module 1   networking basics-2
Module 1 networking basics-2Ankit Dubey
 
Socket Programming it-slideshares.blogspot.com
Socket  Programming it-slideshares.blogspot.comSocket  Programming it-slideshares.blogspot.com
Socket Programming it-slideshares.blogspot.comphanleson
 
Networking
NetworkingNetworking
NetworkingTuan Ngo
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagarNitish Nagar
 

Similar a Network programming in java - PPT (20)

Java 1
Java 1Java 1
Java 1
 
Java networking
Java networkingJava networking
Java networking
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
A.java
A.javaA.java
A.java
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programming
 
Basic Networking in Java
Basic Networking in JavaBasic Networking in Java
Basic Networking in Java
 
Networking & Socket Programming In Java
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In Java
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Module 1 networking basics-2
Module 1   networking basics-2Module 1   networking basics-2
Module 1 networking basics-2
 
Java
JavaJava
Java
 
Socket Programming it-slideshares.blogspot.com
Socket  Programming it-slideshares.blogspot.comSocket  Programming it-slideshares.blogspot.com
Socket Programming it-slideshares.blogspot.com
 
28 networking
28  networking28  networking
28 networking
 
Sockets
SocketsSockets
Sockets
 
Networking
NetworkingNetworking
Networking
 
Networking
NetworkingNetworking
Networking
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
 

Más de kamal kotecha

Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Examplekamal kotecha
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods pptkamal kotecha
 
Java rmi example program with code
Java rmi example program with codeJava rmi example program with code
Java rmi example program with codekamal kotecha
 
Jdbc example program with access and MySql
Jdbc example program with access and MySqlJdbc example program with access and MySql
Jdbc example program with access and MySqlkamal kotecha
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types pptkamal kotecha
 
String and string buffer
String and string bufferString and string buffer
String and string bufferkamal kotecha
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of javakamal kotecha
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7kamal kotecha
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operatorkamal kotecha
 

Más de kamal kotecha (20)

Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
 
Java rmi example program with code
Java rmi example program with codeJava rmi example program with code
Java rmi example program with code
 
Java rmi
Java rmiJava rmi
Java rmi
 
Jdbc example program with access and MySql
Jdbc example program with access and MySqlJdbc example program with access and MySql
Jdbc example program with access and MySql
 
Jdbc api
Jdbc apiJdbc api
Jdbc api
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
JSP Error handling
JSP Error handlingJSP Error handling
JSP Error handling
 
Jsp element
Jsp elementJsp element
Jsp element
 
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
Interface
InterfaceInterface
Interface
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
Class method
Class methodClass method
Class method
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Control statements
Control statementsControl statements
Control statements
 
Jsp myeclipse
Jsp myeclipseJsp myeclipse
Jsp myeclipse
 
basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operator
 

Último

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 

Último (20)

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 

Network programming in java - PPT

  • 3. Network Programming Introduction Java supports Network Programming to communicate with other machines. Let`s start with Network Programming Introduction. http://www.java2all.com
  • 4. Network Programming Introduction As we all know that Computer Network means a group of computers connect with each other via some medium and transfer data between them as and when require. Java supports Network Programming so we can make such program in which the machines connected in network will send and receive data from other machine in the network by programming. The first and simple logic to send or receive any kind of data or message is we must have the address of receiver or sender. So when a computer needs to communicate with another computer, it`s require the other computer’s address. Java networking programming supports the concept of socket. A socket identifies an endpoint in a network. The socket communication takes place via a protocol. http://www.java2all.com
  • 5. The Internet Protocol is a lower-level, connection less (means there is no continuing connection between the end points) protocol for delivering the data into small packets from one computer (address) to another computer (address) across the network (Internet). It does not guarantee to deliver sent packets to the destination. The most widely use a version of IP today is IPv4, uses a 32 bit value to represent an address which are organized into four 8-bits chunks. However new addressing scheme called IPv6, uses a 128 bit value to represent an address which are organized into four 16-bits chunks. The main advantage of IPv6 is that it supports much larger address space than does IPv4. An IP (Internet Protocol) address uniquely identifies the computer on the network. IP addresses are written in a notation using numbers separated by dots is called dotted-decimal notation. There are four 8 bits value between 0 and 255 are available in each IP address such as 127.0.0.1 means local- host, 192.168.0.3 etc. http://www.java2all.com
  • 6. It`s not an easy to remember because of so many numbers, they are often mapped to meaningful names called domain names such as mail.google.com There is a server on Internet who is translate the host names into IP addresses is called DNS (Domain Name Server). NOTE: Internet is the global network of millions of computer and the any computer may connect the Internet through LAN (Local Area Network), Cable Modem, ISP (Internet Service Provider) using dialup. When a user pass the URL like java2all.com in the web-browser from any computer, it first ask to DNS to translate this domain name into the numeric IP address and then sends the request to this IP address. This enables users to work with domain names, but the internet operates on IP addresses. Here in java2all.com the “com” domain is reserved for commercial sites; then “java2all” is the company name. http://www.java2all.com
  • 7. The Higher-level protocol used in with the IP are TCP (Transmission Control Protocol) and UDP (User Datagram Protocol). The TCP enables two host to make a connection and exchange the stream of data, so it`s called Stream-based communication. TCP guarantees delivery of data and also guarantees that streams of data will be delivered in the same order in which they are sent. The TCP can detect the lost of transmission and so resubmit them and hence the transmissions are lossless and reliable. The UDP is a standard, directly to support fast, connectionless host- to-host datagram oriented model that is used over the IP and exchange the packet of data so it`s called packet-based communication. The UDP cannot guarantee lossless transmission. JAVA supports both TCP and UDP protocol families. http://www.java2all.com
  • 9. Java InetAddress Class is used to encapsulate the two thing. 1. Numeric IP Address 2. The domain name for that address. The InetAddress can handle both IPv4 and IPv6 addressses. It has no visible constructors so to create its object, the user have to use one of the available in-built static methods. The commonly used InetAddress in-built methods are: (1) getLocalHost(): It returns the InetAddress object that represents the local host contain the name and address both. If this method unable to find out the host name, it throw an UnknownHostException. Syntax: Static InetAddress getLocalHost() throws UnknownHostException http://www.java2all.com
  • 10. (2) getByName(): It returns an InetAddress for a host name passed to it as a parameter argument. If this method unable to find out the host name, it throw an UnknownHostException. Syntax: Static InetAddress getByName(String host_name) throws UnknownHostException (3) getAllByName(): It returns an array of an InetAddress that represent all of the addresses that a particular name resolve to it. If this method can’t find out the name to at least one address, it throw an UnknownHostException. Syntax: Static InetAddress[] getAllByName(String host_name) throws UnknownHostException http://www.java2all.com
  • 11. Program: Write down a program which demonstrate an InetAddress class. import java.net.InetAddress; import java.net.UnknownHostException; public class InetAddress_Demo { public static void main(String[] args) { String name = “”; try { System.out.println(“HOST NAME - Numeric Address : “+InetAddress.getLocalHost()); InetAddress ip = InetAddress.getByName(name); System.out.println(“HOST DEFAULT-NAME / IP :”+ip); System.out.println(“HOST IP-ADDRESS : “+ip.getHostAddress()); System.out.println(“HOST DEFAULT-NAME : “+ip.getHostName()); } catch (UnknownHostException e) { System.out.println(“Not find the IP-ADDRESS for :”+name); } } } Output: HOST NAME - Numeric Address : Ashutosh- 2c89cd5e0a/127.0.0.1 HOST DEFAULT-NAME / IP : localhost/127.0.0.1 HOST IP-ADDRESS : 127.0.0.1 HOST DEFAULT-NAME : localhost http://www.java2all.com
  • 12. socket programming in java http://www.java2all.com
  • 13. socket programming in java is very important topic and concept of network programming. Java network Programming supports the concept of socket. A socket identifies an endpoint in a network. The socket communication take place via a protocol. A socket can be used to connect JAVA Input/Output system to other programs that may resides either on any machine on the Internet or on the local machine. http://www.java2all.com
  • 14. TCP/IP Sockets: TCP/IP sockets are used to implement point-to-point, reliable, bidirectional, stream-based connections between hosts on the Internet. There are two types of TCP sockets available in java: (1) TCP/IP Client Socket (2) TCP/IP Server Socket (1) TCP/IP Client Socket: The Socket class (available in java.net package) is for the Client Socket. It is designed to connect to server sockets and initiate protocol exchange. There are two constructers used to create client sockets type object. (a) Socket(String host_name,int port) throws UnknownHostException,IOException it creates a socket that is connected to the given host_name and port number. http://www.java2all.com
  • 15. (b) Socket(InetAddress ip,int port) throws IOException it creates a socket using a pre-existing InetAddress object and a port number. (2) TCP/IP Server Socket: The ServerSocket class (available in java.net package) is for the Server. It is designed to be a “listener”, which waits for clients to connect before doing anything and that listen for either local or remote client programs to connect to them on given port. When you create ServerSocket it will register itself with the system as having an interest in client connection. Syntax: ServerSocket(int port) throws IOException http://www.java2all.com
  • 16. Program: Write down a program which demonstrate the Socket programming for passing the message from server to client. Client.java: import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import java.net.UnknownHostException; public class Client { public static void main(String[] args) { System.out.println(“Sending a request.....”); try { Socket s = new Socket(“127.0.0.1”,1564); System.out.println(“connected successfully.....”); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); System.out.println(“response from server...”); System.out.println(“Client side : “+br.readLine()); s.close(); } catch (UnknownHostException e) { System.out.println(“Not find the IP-ADDRESS for :”+e); } catch (IOException e) { System.out.println(“Not Found data for Socket : “+e); } } } http://www.java2all.com
  • 17. Server.java: import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(1564); System.out.println("waiting for request...."); Socket s = ss.accept(); System.out.println("Request accepted"); PrintStream ps = new PrintStream(s.getOutputStream()); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Input the data at server : "); ps.print(br.readLine()); s.close(); ss.close(); } catch (Exception e) { System.out.println("Not Found data for Socket : "+e); } } } http://www.java2all.com
  • 18. For Output follow the below step: (1) Run server.java Console: waiting for request.... (2) Run Client.java Console: waiting for request.... Request accepted Input the data at server: (3) Now enter the message at console Input the data at server: welcome at server (4) Then press Enter. http://www.java2all.com
  • 19. (5) Sending a request..... connected successfully..... response from server... Client side: welcome at server Program 2: Write down a program for addition the two different variable by Socket programming. Program 3: Write down a program which demonstrate the Socket programming for passing the message from client to server and also apply EXIT properties. http://www.java2all.com
  • 20. java socket programming example http://www.java2all.com
  • 21. Two variable addition and passing message from client to server two different java socket programming example is given below but before going through program directly you should have some knowledge about Java Network Programming and Socket. These both things are already available in previous chapter so you can learn from there. Now let`s move to program 1. http://www.java2all.com
  • 22. Program: Write down a program for addition the two different variable by Socket programming. Client_Addition.java: import java.io.PrintStream; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class Client_Addition { public static void main(String[] args) { try { Socket s = new Socket("127.0.0.1",1868); PrintStream ps = new PrintStream(s.getOutputStream()); Scanner sc = new Scanner(System.in); System.out.println("Enter first value: "); int i1 = sc.nextInt(); ps.println(i1); ps.flush(); System.out.println("Enter second value: "); int i2 = sc.nextInt(); ps.println(i2); ps.flush(); s.close(); } catch (UnknownHostException e) { System.out.println("Not find the IP-ADDRESS for :"+e); } catch (IOException e) { System.out.println("Not Found data for Socket : "+e); } } http://www.java2all.com
  • 23. Server_Addition.java import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class Server_Addition { public static void main(String[] args) { try { System.out.println("Server run successfully......"); ServerSocket sc = new ServerSocket(1868); Socket s = sc.accept(); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); int i1 = Integer.parseInt(br.readLine()); int i2 = Integer.parseInt(br.readLine()); System.out.println("Addition: "+(i1+i2)); s.close(); sc.close(); } catch (IOException e) { System.out.println("Not Found data for Socket : "+e); } } } http://www.java2all.com
  • 24. For Output follow the below step: (1) Run Server_Addition.java Console: Server run successfully...... (2) Run Client.java Console: Enter first value: 5 Enter second value: 25 (3) Now, press Enter (4) Server run successfully...... Addition: 30 http://www.java2all.com
  • 25. java socket programming example 2: Program: Write down a program which demonstrate the Socket programming for passing the message from client to server and also apply EXIT properties. Client.java: import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; import java.net.UnknownHostException; public class Client { public static void main(String[] args) { System.out.println("Sending a request....."); Try { Socket s = new Socket("127.0.0.1",1235); System.out.println("connected successfully....."); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintStream ps = new PrintStream(s.getOutputStream()); BufferedReader brs = new BufferedReader(new InputStreamReader(s.getInputStream())); while(true) { System.out.println("input the data...."); String st = br.readLine(); ps.println(st); http://www.java2all.com
  • 26. if(st.equals("exit")) { System.exit(1); } System.out.println("data returned"); System.out.println(st); } } catch (UnknownHostException e) { System.out.println("Not find the IP-ADDRESS for :"+e); } catch (IOException e) { System.out.println("Not Found data for Socket : "+e); } } } http://www.java2all.com
  • 27. Server.java: import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(1235); System.out.println("waiting for request...."); Socket s = ss.accept(); System.out.println("Request accepted"); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); while(true) { String st = br.readLine(); if(st.equals("exit")==true) { System.out.println("connection lost....."); System.exit(1); } System.out.println("Message from client: "+st); } } catch (IOException e) { System.out.println("Not Found data for Socket : "+e); } } } http://www.java2all.com
  • 28. For Output follow the below step: (1) Put the both file in the bin folder at jdk. For example: C:Program Files (x86)Javajdk1.6.0bin. (2) Open Command Prompt & reach up to bin path. http://www.java2all.com
  • 29. (3) Compile the Server.java & Client.java …bin>javac Server.java …bin>javac Client.java (4) Run the Server.java …bin>java Server http://www.java2all.com
  • 30. (5) Open new command prompt: (6) Now revise step-2. (7) Run the Client.java. …bin>java Client http://www.java2all.com
  • 31. Check the Message at Server Side Command Prompt. (8) Write down the message on Client Side Command Prompt Like: Input the data… Ashutosh (9) Now Press Enter & Check the Output at Both Windows. http://www.java2all.com
  • 33. (10) If want to Exit then type exit on Client side Window. Like: Input the data… exit http://www.java2all.com