Chapter4 NW
Chapter4 NW
Chapter4 NW
Networking Basics
Marks:10
Prepared by :
Prof. Biradar R.Y.
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.
Port is represented by a positive (16-bit)
integer value.
Sockets provide the communication
mechanism between two computers using
TCP/IP.
Socket is a combination of an IP address and a
port number.
2
Protocol
A protocol is a set of rules basically that is
followed for communication.
IP (Internet Protocol): Low level routing protocol.
Divides data into small packets and send on given
address. It does not guarantee to deliver the
packets.
Transmission Control Protocol (TCP): Higher level
protocol. Reliability to deliver data. Connection
oriented protocol.
User Datagram Protocol (UDP): Next to TCP. It
support fast, connectionless, unreliable transport
of data packets.
3
Transmission Control Protocol (TCP)
Example: HTTP, FTP, and Telnet.
4
Difference Between TCP & UDP
TCP UDP
Connection oriented Connectionless
Reliable Unreliable
Retransmit No retransmission
Slower data transmission Faster data transmission
Require more cost Less cost than TCP
Gives acknowledgement No acknowledgement
IP address: 2 version
IPv4 : 32–bits and in decimal (Now)
IPv6 : 128-bits and in hexadecimal (Future)
Port
used to uniquely identify different
applications
acts as a communication endpoint between
applications.
associated with the IP address for
communication between two applications.
Port is of 16-bit address.
8
Reserved Ports
Ports are used to identify right application.
IP address is used to identify right computer.
Port number range : 0 to 65535
1024 ports are reserved.
Port numbers between 0 and 1,023 are reserved for
standard services, such as email, FTP, and HTTP.
Protocol Reserved Port
FTP 21
Telnet 23
SMTP 25
HTTP 80
Proxy Server
It is mediator between real web server
and
a client applications like web browser.
Mr. Nilesh
Vishwasra
Features of Proxy Server
Filters specific request
stores data in catche for future use
Improving performance
Provides security
monitoring
Mr. Nilesh
Vishwasra
Socket Programming
• A socket is one end-point of a two-way
communication link between two programs running
on the network.
• Socket is combination of an IP address and a port
number.
• Socket classes are used to represent the connection
between a client program and a server program.
Socket Programming
• can be connection-oriented or connection-less.
• Socket
Connection
Oriented
• ServerSocket
Connection
•DatagramSocket
less • DatagramPacket
Server-Client Communication
Socket Programming with TCP
• Supports Connection-oriented ,reliable,
bidirectional and stream based
communication
• initial handshaking phase
• The package java.net provides support
for
• sockets programming.
import java.net.*;
• Contains classes and interfaces to
encapsulate the “Socket” Paradigm.
Classes & Interfaces from java.net
package
InetAddress
Socket
URL
URLConnection
ServerSocket
DatagramSocket
DatagramPacket
17
InetAddress class
Represents IP address
19
All methods generate : UnknownHostException
Instance Methods
boolean equals(Object other)
byte[] getAddress(): Return four element of
IP address.
String getHostAddress(): Return host
address associated with InetAddress.
String getHostName(): Return host name.
boolean isMultiCastAddress()
20
URL
URL is Uniform Resource Locator.
21
URL
URL string consist of following parts:
protocol
Host name or address
Port number
File or resource location.
22
http://www.msbte.com:80/index.html
• Exapmle:
http://www.msbte.com:80/index.html
URL(String url)
URL(String protocol, String hostname, int
port, String path)
URL(URL obj, String url)
25
Methods of URL
Method Description
String getProtocol() It returns the protocol of the URL.
} Output
catch(Exception e) Protocol: http
{ Host Name: msbte.org.in
System.out.println(e); Port Number: -1
} File Name: /index.html
}
URLConnection Class
URLConnection is an abstract class
public URLConnection
openConnection()throws
IOException{ }
29
URLConnection Class methods
long getExpiration(): Return expiration
date
and time in miliseconds.
InputStream getInputStream() : Used to
get contents of resource.
30
import java.net.*;
import java.util.*;
import java.io.*;
public class UCDemo
{
public static void main(String[] args)
{
try
{
URL url=new URL("http://www.javatpoint.com/java-tutorial");
URLConnection uc=url.openConnection();
System.out.println("length: "+uc.getContentLength());
System.out.println("date of response: "+ new Date(uc.getDate()));
System.out.println("date of last modification: "+new Date(uc.getLastModified()));
System.out.println("content type: "+uc.getContentType());
InputStream in=uc.getInputStream();
int c;
while((c=in.read())!=-1)
{
System.out.print((char)c);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Socket Class
• implements one side of a two-way connection
• Used to create client sockets, is designed to
connect to server socket and initiate protocol
exchanges.
• Socket is implemented at the client side to
send request to the port of the machine
where ServerSocket is listening.
• When Socket object is created, it establishes
the connection b/w client and server.
Socket Class
• Constructors:-
Socket(String hostname, int port)
Socket(InetAdrress ipaddr, int port)
receive=new BufferedReader(new
InputStreamReader(client.getInputStream()));
send=new PrintWriter(client.getOutputStream(),true);
in=new BufferedReader(new
InputStreamReader(System.in));
String msgsend="";
while(!msgsend.equals("bye"))
{
msgsend=in.readLine();
send.println(msgsend);
String msgreceive=receive.readLine();
if(msgreceive!=null)
System.out.println("From Server="+msgreceive);
}
client.close();
}
catch(Exception e){}
}
}
import java.net.*;
import java.io.*;
class TCPServer
{
public static void main(String args[])
{
BufferedReader receive;
PrintWriter send;
BufferedReader in;
try
{ ServerSocket server=new ServerSocket(3000);
System.out.println("Server is ready....");
Socket sock=server.accept();
receive=new BufferedReader(new
InputStreamReader(sock.getInputStream()));
send=new PrintWriter(sock.getOutputStream(),true);
in=new BufferedReader(new
InputStreamReader(System.in));
String msgreceive="";
while(true)
{
msgreceive=receive.readLine();
if(msgreceive!=null)
System.out.println("From Client="+msgreceive);
if(msgreceive.equals("bye"))
{
sock.close();
break;
}
String msgsend=in.readLine();
send.println(msgsend);
}
}
catch(Exception e){}
}
}
Socket Programming with
•
UDP
Connectionless and unreliable service.
• There isn’t an initial handshaking phase.
• Doesn’t have a pipe. No streams are attached to
the sockets.
• transmitted data may be received out of order, or
lost
• Datagrams are bundles of information passed
between machine.
• Datagram communication through Two classes
• DatagramPacket
• DatagramSocket
DatagramPacket
• Data container
• Each packet contains
InetAddress
Port of destination
Data
• Constructor:
• For Receiving data
DatagramPacket(byte data[],int size)
DatagramPacket(byte data[],int offset, int size)
• For Sending data
DatagramPacket(byte data[],int size, InetAddress
ipAddr,int port)
DatagramPacket(byte data[],int offset, int
size,InetAddress ipAddr, int port)
Methods
–void receive( DatagramPacket p)
–void send( DatagramPacket p)
import java.io.*;
import java.net.*;
import java.util.*;
public class UDPClient
{
public static void main(String[] args)throws Exception
{
DatagramSocket client= new DatagramSocket();
InetAddress serverip=InetAddress.getLocalHost();
try {
whois.connect(WhoisClient.DEFAULT_HOST);
System.out.println(whois.query("=google.com"));
whois.disconnect();
} catch (IOException e) {
System.err.println("Error I/O exception: " + e.getMessage());
return;
}
}