0% found this document useful (0 votes)
14 views41 pages

unit 3 Networking

unit 3 Networking

Uploaded by

Ashad Muneer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views41 pages

unit 3 Networking

unit 3 Networking

Uploaded by

Ashad Muneer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Networking

10/20/2018 GAGAN THAKRAL


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.

10/20/2018 GAGAN THAKRAL


Advantage of Java Networking

• sharing resources
• centralize software management

10/20/2018 GAGAN THAKRAL


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

10/20/2018 GAGAN THAKRAL


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.

10/20/2018 GAGAN THAKRAL


Protocol

A protocol is a set of rules basically that is


followed for communication. For example:
• TCP
• FTP
• Telnet
• SMTP
• POP etc.

10/20/2018 GAGAN THAKRAL


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.

10/20/2018 GAGAN THAKRAL


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.

10/20/2018 GAGAN THAKRAL


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.

10/20/2018 GAGAN THAKRAL


Socket

• A socket is an endpoint between two way


communication.

10/20/2018 GAGAN THAKRAL


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.
10/20/2018 GAGAN THAKRAL
Java Socket Programming
The client in socket programming must know
two information:
• IP Address of Server, and
• Port number.

10/20/2018 GAGAN THAKRAL


Socket class

• A socket is simply an endpoint for


communications between the machines.
• The Socket class can be used to create a
socket.

10/20/2018 GAGAN THAKRAL


Important methods

10/20/2018 GAGAN THAKRAL


ServerSocket class

• The ServerSocket class can be used to create a


server socket.
• This object is used to establish communication
with the clients.

10/20/2018 GAGAN THAKRAL


Important methods

10/20/2018 GAGAN THAKRAL


Example of Java Socket Programming

import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e){System.out.println(e);}
}
}

10/20/2018 GAGAN THAKRAL


import java.io.*;
import java.net.*;
public class MyClient {
public static void main(String[] args) {
try{
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}catch(Exception e){System.out.println(e);}
}
}

10/20/2018 GAGAN THAKRAL


10/20/2018 GAGAN THAKRAL
Example of Java Socket Programming
(Read-Write both side)
import java.net.*;
import java.io.*;
class MyServer{
public static void main(String args[])throws Exception{
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop")){
str=din.readUTF();
System.out.println("client says: "+str);
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
10/20/2018 GAGAN THAKRAL
}}
import java.net.*;
import java.io.*;
class MyClient{
public static void main(String args[])throws Exception{
Socket s=new Socket("localhost",3333);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));

String str="",str2="";
while(!str.equals("stop")){
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
System.out.println("Server says: "+str2);
}

dout.close();
s.close();
}}
10/20/2018 GAGAN THAKRAL
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.
• https://www.krishnacollege.ac.in/pages/comp
uter-science.html

10/20/2018 GAGAN THAKRAL


A URL contains many information
1. Protocol: In this case, http is the protocol.
2. Server name or IP Address: In this case, www.
krishnacollege.ac.in is the server name.
3. Port Number: It is an optional attribute. If we
write http//www.krishnacollege.ac.in:80/pages/
, 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.php is the file name.
10/20/2018 GAGAN THAKRAL
Commonly used methods of Java URL
class

10/20/2018 GAGAN THAKRAL


Example of Java URL class
//URLDemo.java
import java.io.*;
import java.net.*;
public class URLDemo{
public static void main(String[] args){
try{
URL url=new URL("http://www.krishnacollege.ac.in");
System.out.println("Protocol: "+url.getProtocol());
System.out.println("Host Name: "+url.getHost());
System.out.println("Port Number: "+url.getPort());
System.out.println("File Name: "+url.getFile());

}catch(Exception e){System.out.println(e);}
}
}

10/20/2018 GAGAN THAKRAL


10/20/2018 GAGAN THAKRAL
Java URLConnection class

• The Java URLConnection class represents a


communication link between the URL and the
application. This class can be used to read and
write data to the specified resource referred
by the URL.

10/20/2018 GAGAN THAKRAL


How to get the object of
URLConnection class
• The openConnection() method of URL class
returns the object of URLConnection class.
• Syntax:
public URLConnection openConnection()throws
IOException{}

10/20/2018 GAGAN THAKRAL


Displaying source code of a webpage
by URLConnecton class
• The URLConnection class provides many
methods, we can display all the data of a
webpage by using the getInputStream()
method.
• The getInputStream() method returns all the
data of the specified URL in the stream that
can be read and displayed.

10/20/2018 GAGAN THAKRAL


Example of Java URLConnection class
import java.io.*;
import java.net.*;
public class URLConnectionExample {
public static void main(String[] args){
try{
URL url=new URL(" http://www.krishnacollege.ac.in ");
URLConnection urlcon=url.openConnection();
InputStream stream=urlcon.getInputStream();
int i;
while((i=stream.read())!=-1){
System.out.print((char)i);
}
}catch(Exception e){System.out.println(e);}
}
}10/20/2018 GAGAN THAKRAL
Java InetAddress class

• Java InetAddress class represents an IP


address.
• The java.net.InetAddress class provides
methods to get the IP of any host name for
example www.google.com,
www.facebook.com etc.

10/20/2018 GAGAN THAKRAL


Commonly used methods of
InetAddress class

10/20/2018 GAGAN THAKRAL


Example of Java InetAddress class
import java.io.*;
import java.net.*;
public class InetDemo{
public static void main(String[] args){
try{
InetAddress
ip=InetAddress.getByName("www.krishnacollege.ac.in");
System.out.println("Host Name: "+ip.getHostName());
System.out.println("IP Address: "+ip.getHostAddress());
}catch(Exception e){System.out.println(e);}
}
}
10/20/2018 GAGAN THAKRAL
10/20/2018 GAGAN THAKRAL
Java DatagramSocket and
DatagramPacket
• Java DatagramSocket and DatagramPacket
classes are used for connection-less socket
programming.

10/20/2018 GAGAN THAKRAL


Java DatagramSocket class

• Java DatagramSocket class represents a


connection-less socket for sending and
receiving datagram packets.
• A datagram is basically an information but
there is no guarantee of its content, arrival or
arrival time.

10/20/2018 GAGAN THAKRAL


Commonly used Constructors of
DatagramSocket class
• DatagramSocket() throws SocketEeption: it
creates a datagram socket and binds it with the
available Port Number on the localhost machine.
• DatagramSocket(int port) throws SocketEeption:
it creates a datagram socket and binds it with the
given Port Number.
• DatagramSocket(int port, InetAddress address)
throws SocketEeption: it creates a datagram
socket and binds it with the specified port
number and host address.
10/20/2018 GAGAN THAKRAL
Java DatagramPacket class

• Java DatagramPacket is a message that can be


sent or received. If you send multiple packet,
it may arrive in any order. Additionally, packet
delivery is not guaranteed.

10/20/2018 GAGAN THAKRAL


Commonly used Constructors of
DatagramPacket class
• DatagramPacket(byte[] barr, int length): it
creates a datagram packet. This constructor is
used to receive the packets.
• DatagramPacket(byte[] barr, int length,
InetAddress address, int port): it creates a
datagram packet. This constructor is used to
send the packets.

10/20/2018 GAGAN THAKRAL


Example of Sending DatagramPacket
by DatagramSocket
//DSender.java
import java.net.*;
public class DSender{
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
String str = "Welcome java";
InetAddress ip = InetAddress.getByName("127.0.0.1");

DatagramPacket dp = new DatagramPacket(str.getBytes(),


str.length(), ip, 3000);
ds.send(dp);
ds.close();
}
}
10/20/2018 GAGAN THAKRAL
Example of Receiving DatagramPacket
by DatagramSocket
//DReceiver.java
import java.net.*;
public class DReceiver{
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(3000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 1024);
ds.receive(dp);
String str = new String(dp.getData(), 0, dp.getLength());
System.out.println(str);
ds.close();
}
}
10/20/2018 GAGAN THAKRAL

You might also like