networking and security - wordpress.com · socket programming sockets provide the communication...
TRANSCRIPT
![Page 1: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/1.jpg)
Chapter 03
Networking and Security
Mr. Nilesh Vishwasrao Patil
Government Polytechnic Ahmednagar
![Page 2: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/2.jpg)
Socket
Network socket is an endpoint of an inter-process communication flow across a computer network.
Sockets provide the communication mechanism between two computers using TCP/IP.
Mr. Nilesh Vishwasrao Patil 2
![Page 3: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/3.jpg)
Socket
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.
User Datagram Protocol (UDP): Next to TCP. It support fast, connectionless, unreliable transport of data packets.
Mr. Nilesh Vishwasrao Patil 3
![Page 4: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/4.jpg)
Difference Between TCP & UDP
Mr. Nilesh Vishwasrao Patil 4
TCP UDP
Connection oriented Connectionless
Reliable Unreliable
Retransmit No retransmission
Slower data transmission Faster data transmission
Require most cost Less cost than TCP
![Page 5: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/5.jpg)
Proxy Server
It is mediator between real web server and a client applications like web browser.
Filter specific request and stored data in catch for future use.
Mr. Nilesh Vishwasrao Patil 5
![Page 6: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/6.jpg)
Reserved Sockets Ports
Port number range : 0 to 65535
1 to 1024 are reserved.
Examples: FTP : 21
Telnet : 23
Email : 25
HTTP: 80
Client request for file from HTTP server, it is called hits.
Mr. Nilesh Vishwasrao Patil 6
![Page 7: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/7.jpg)
Internet Addressing
Internet address is unique number, used to identify each machine uniquely.
IP address: 2 version IPv4 : 32–bits and in decimal (Now)
IPv6 : 128-bits and in hexadecimal (Future)
IPv4 : Divide 32 bits in 4 parts.
Each part range from 0 to 255.
Mr. Nilesh Vishwasrao Patil 7
![Page 8: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/8.jpg)
Internet Addressing
Divided into 5 classes: Class A
Class B
Class C
Class D
Class E
Mr. Nilesh Vishwasrao Patil 8
![Page 9: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/9.jpg)
Assignment
DNS (Domain Name Services)
Internet
Server – Client
Relationship between Java and Internet
Web server and Application server with one example at least.
Mr. Nilesh Vishwasrao Patil 9
![Page 10: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/10.jpg)
Socket Programming
A client program creates a socket on its end of the communication and attempts to connect that socket to a server.
When the connection is made, the server creates a socket object on its end of the communication.
The client and server can now communicate by writing to and reading from the socket.
Mr. Nilesh Vishwasrao Patil 10
![Page 11: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/11.jpg)
Java Sockets Programming
The package java.net provides support for sockets programming.
Typically you import everything defined in this package with:
import java.net.*;
Mr. Nilesh Vishwasrao Patil 11
![Page 12: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/12.jpg)
Classes
InetAddress
Socket
URL
URLConnection
ServerSocket
DatagramSocket
DatagramPacket
Mr. Nilesh Vishwasrao Patil 12
![Page 13: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/13.jpg)
InetAddress class
InetAddress class is encapsulate both numeric IP address (eg .74.125.236.88) and the domain name (eg. www.google.com) for the address.
Interaction with this class by using the Hostname rather than IP address, more conveniently and understandable way.
For example, mostly every internet user don't know IP address for google.com.
Mr. Nilesh Vishwasrao Patil 13
![Page 14: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/14.jpg)
InetAddress class
It has both Factory and Instance methods:
Factory method: is a static method in a class return an instance of
that class.
Instance Methods: is a non-static method.
Mr. Nilesh Vishwasrao Patil 14
![Page 15: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/15.jpg)
About InetAddress class As we know, "new" Keyword is used to create
object to that corresponding class.
InetAddress Class has no visible constructors. to create a InetAddress object.
Factory Method is used to create objects.
Three factory methods: static InetAddress getLocalHost()
static InetAddress getByName(String hostName)
static InetAddress[] getAllByName(String hostName).
All methods generate : UnknownHostException
Mr. Nilesh Vishwasrao Patil 15
![Page 16: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/16.jpg)
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.
int hasCode() : return hashcode of invoking
object.
Boolean isMultiCastAddress()
Mr. Nilesh Vishwasrao Patil 16
![Page 17: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/17.jpg)
URL
URL is Uniform Resource Locator.
It is a formatted string used by email clients, web browsers and different type of software recognize network resource on the internet.
Network resource could be text, documents, plain web pages, programs or graphics.
Mr. Nilesh Vishwasrao Patil 17
![Page 18: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/18.jpg)
URL
URL string consist of following parts: Network protocol
Host name or address
Port number
File or resource location.
URL provides comprehensive form to uniquely identify or address information on the internet.
Java has provided : URL class
Mr. Nilesh Vishwasrao Patil 18
![Page 19: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/19.jpg)
URL
URL string consist of three parts: Network protocol
Host name or address
File or resource location.
URL provides comprehensive form to uniquely identify or address information on the internet.
Java has provided : URL class
Mr. Nilesh Vishwasrao Patil 19
![Page 20: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/20.jpg)
URL
Ex http://www.msbte.com/index.html
URL class has some constructors and it throws MalformedURLException
URL(String url)
URL(String protocol, String hostname, int port, String path)
URL(URL obj, String url)
Mr. Nilesh Vishwasrao Patil 20
![Page 21: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/21.jpg)
URL
String getProtocol()
String getHost()
String toExternalForm()
String getFile()
String getPort()
Mr. Nilesh Vishwasrao Patil 21
![Page 22: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/22.jpg)
URLConnection Class
Used for accessing the attributes of remote resource.
public URLConnection openConnection()throws IOException{} openConnection() of URL class returns the object of
URLConnection class.
Mr. Nilesh Vishwasrao Patil 22
![Page 23: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/23.jpg)
URLConnection Class methods
int getContentLength() : Return size of contents related to resource. If no length then return -1.
String getContentType(): Return type of content of resource.
long getDate() : Return date and time of response
long getLastModified() : return last date and time modified of response
Mr. Nilesh Vishwasrao Patil 23
![Page 24: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/24.jpg)
URLConnection Class methods
Long getExpiration(): Return expiration date and time in miliseconds.
InputStream getInputStream() : Used to get contens of resource.
Mr. Nilesh Vishwasrao Patil 24
![Page 25: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/25.jpg)
Socket Programming
Sockets provide the communication mechanism between two computers using TCP.
A client program creates a socket on its end of the communication and attempts to connect that socket to a server.
When the connection is made, the server creates a socket object on its end of the communication.
The client and server can now communicate by writing to and reading from the socket.
Mr. Nilesh Vishwasrao Patil 25
![Page 26: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/26.jpg)
Socket Programming
Socket class represents a socket.
ServerSocket class provides a mechanism for the server program to listen for clients and establish connections with them.
Mr. Nilesh Vishwasrao Patil 26
![Page 27: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/27.jpg)
Steps to establish connection
The server instantiates a ServerSocket object, denoting which port number communication is to occur on.
The server invokes the accept() method of the ServerSocket class. This method waits until a client connects to the server on the given port.
After the server is waiting, a client instantiates a Socket object, specifying the server name and port number to connect to.
Mr. Nilesh Vishwasrao Patil 27
![Page 28: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/28.jpg)
Steps to establish connection
The constructor of the Socket class attempts to connect the client to the specified server and port number. If communication is established, the client now has a Socket object capable of communicating with the server.
On the server side, the accept() method returns a reference to a new socket on the server that is connected to the client's socket.
Mr. Nilesh Vishwasrao Patil 28
![Page 29: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/29.jpg)
Steps to establish connection
Each socket has both an OutputStream and an InputStream.
The client's OutputStream is connected to the server's InputStream,
Client's InputStream is connected to the server's OutputStream.
Mr. Nilesh Vishwasrao Patil 29
![Page 30: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/30.jpg)
ServerSocket Constructor
public ServerSocket(int port)
public ServerSocket(int port, int backlog)
public ServerSocket(int port, int backlog, InetAddress address)
public ServerSocket()
Mr. Nilesh Vishwasrao Patil 30
![Page 31: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/31.jpg)
ServerSocket Methods
public int getLocalPort() : Return port number of server socket is listening.
public Socket accept() : Waits for an incoming client.
public void setSoTimeout(int timeout) : Sets the time-out value for how long the server socket waits for a client
during the accept().
public void bind(SocketAddress host, int backlog) : Binds the socket to the specified server and port in the SocketAddress object. Use this method if you instantiated
the ServerSocket using the no-argument constructor.
Mr. Nilesh Vishwasrao Patil 31
![Page 32: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/32.jpg)
Socket Constructor
public Socket(String host, int port)
public Socket(InetAddress host, int port).
public Socket(String host, int port, InetAddress localAddress, int localPort)
public Socket(InetAddress host, int port, InetAddress localAddress, int localPort)
public Socket()
Mr. Nilesh Vishwasrao Patil 32
![Page 33: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/33.jpg)
Socket Methods
public void connect(SocketAddress host, int timeout)
public InetAddress getInetAddress()
public int getPort()
public int getLocalPort()
public SocketAddress getRemoteSocketAddress()
public InputStream getInputStream()
public OutputStream getOutputStream()
public void close()
Mr. Nilesh Vishwasrao Patil 33
![Page 34: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/34.jpg)
Sockets
Client socket, welcoming socket (passive) and connection socket (active)
Mr. Nilesh Vishwasrao Patil 34
![Page 35: Networking and Security - WordPress.com · Socket Programming Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7fd8b1e783d15fe81120a9/html5/thumbnails/35.jpg)
Client/server socket interaction: TCP
wait for incoming connection request connectionSocket = welcomeSocket.accept()
create socket, port=x, for incoming request: welcomeSocket =
ServerSocket()
create socket, connect to hostid, port=x clientSocket =
Socket()
close connectionSocket
read reply from clientSocket
close clientSocket
Server (running on hostid) Client
send request using clientSocket read request from
connectionSocket
write reply to connectionSocket
TCP connection setup
Mr. Nilesh Vishwasrao Patil 35