Вы находитесь на странице: 1из 43

Networking

And
Socket Programming

1
Objectives of This Session

Networking basics and OSI model

State what is a protocol

Describe the client-server concept

Describe the concept of port, socket

Internet addressing and URL

State the need for client side scripts

Identify the reasons for popularity of java as a client side script

2
Host based Processing

Application
Server Logic Data

Mainframe

Clients

Terminal 1 Terminal 2 Terminal 3 Terminal 4

3
Client - Server

DBMS

Application
Logic
Network Server

Client Workstation 1 Client Workstation 2 Client Workstation 3 Client Workstation 4

Application Application Application Application


Logic Logic Logic Logic

4
Web Based Architecture

HTTP UNIX
TCP/IP
WEB SERVER
INTERNET

WINDOWS

WEB SERVER MACINTOSH

5
Protocol

• Protocols are rules and procedures for communication.


• Lays out the details of how a computer can send & receive
data on a network.
• Protocols here govern:
Packaging of data into packets.
Speed of transmission.
Recreation of data to its original form.

Eg: of network protocols: TCP/IP, AppleTalk , NetBEUI etc

6
OSI (Open System Interconnection)Model

APPLICATION
PRESENTATION
SESSION
TRANSPORT
NETWORK
DATA LINK
PHYSICAL

7
The OSI Layers

7) Application: Provides different services to the applications

6) Presentation: Converts the information into required format

5) Session: Helps to establish, use and end a connection between


applications running on different computers

4) Transport: Provides end to end communication control

3) Network: Routes the information in the network

2) Data Link: Provides error free transmission of data frames

1) Physical: Connects the entity to the transmission media

8
TCP/IP Architecture

APPLICATION LAYER
TRANSPORT LAYER
NETWORK LAYER
LINK LAYER

9
IP Addressing Scheme (IPV4)

CLASSSES OF IP ADDRESSES
CLASS A 0 Net id Host id Large size N/W

CLASS B 10 Net id Host id Medium size N/W

CLASS C 110 Net id Host id Small size N/W

CLASS D 1110 Multicast group id

Class IP Address Range

A 0.0.0.0 to 127.255.255.255
B 128.0.0.0 to 191.255.255.255
C 192.0.0.0 to 223.255.255.255
D 224.0.0.0 to 239.255.255.255

10
TCP/IP Architecture

HTTP TFTP Ping


FTP Telnet DNS
SMTP SNMP NNTP

TCP UDP

Internet Protocol (IP)

ICMP

ARP RARP

Ethernet, to ken Ring, RS232 ………

11
FTP - File Transport Protocol at the application layer.
Telnet - Remote session at the application layer.
SMTP - Simple Mail Transport Protocol at the application layer.
UDP - User Datagram Protocol is a connection less unreliable protocol working at
the transport layer.
ICMP - Internet Control Message Protocol is used to perform network error
reporting and status. It works at the transport layer.
IP - Internet Protocol is used for software addressing of computers and works at
the data link layer.
ARP - Address Resolution Protocol is used to resolve the hardware address of a
card to package the Ethernet data. It works at the data link layer.
RARP - Reverse Address Resolution Protocol used for disk less computers to
determine their IP address using the network. It works at the data link layer.
NNTP - Network News Transport Protocol is used to link newsgroups for
discussions on the web.

12
SNMP : The Simple Network Management Protocol (SNMP) is an
application layer protocol that facilitates the exchange of management
information between network devices. It is part of the Transmission Control
Protocol/Internet Protocol (TCP/IP) protocol suite. SNMP enables network
administrators to manage network performance, find and solve network
problems, and plan for network growth.

TFTP : Trivial File Transfer Protocol (TFTP) is a simple protocol to transfer


files. It has been implemented on top of the Internet User Datagram
protocol (UDP or Datagram). TFTP is designed to be small and easy to
implement, therefore, lacks most of the features of a regular FTP. TFTP
only reads and writes files (or mail) from/to a remote server. It cannot list
directories, and currently has no provisions for user authentication.

13
TCP protocol

• Provides communications in a heterogeneous


environment.
• Routable, defacto standard for internetworking.
• Connection oriented protocol.
• Relies on IP for addressing & routing
• Guarantees reliable delivery in the order in which
packets were sent.
• E.g.’s of app’s that run on top of TCP:
FTP, TelNet, HTTP

14
UDP protocol

Connectionless protocol.
Relies on IP for addressing & routing
If you send datagram over a UDP protocol, it may
not arrive, if it does, it may arrive out of order etc.
E.g.’s of app’s that run on top of UDP:
SNMP, TFTP

15
Server

Applies to any program that offers a service that


can be reached over a network.
Accepts a request over the network, performs its
service & returns the result to the requester.
Usually servers are implemented as application
programs.

16
Client

An executing program that sends a request to a


server & waits for an response.

Request
client Server
Response

17
Client Side Scripts

HTML
Javascript
VB script
Jscript
Applets

18
Socket & Port

Socket is a logical number comprising of the IP


address(System identification) and the port
number.
A port number is a logical number assigned to a
process. It helps to identify a process on a given
system.
Port numbers upto 1024 are reserved ports or
also called as well-known ports.
A server process is said to “listen” to a port until a
client connects to it.

19
Port

for example
• 21 – FTP
• 23 – Telnet
• 25 – SMTP
• 80 – HTTP
• 109 - POP

20
Sockets

Are used to implement


- bi-directional
- reliable
- persistent
- point-to-point
- stream-based connections.
Sockets shield the programmer from low level
details of network, like media types, packet sizes,
packet retransmission, network addresses etc.
They allow programmer to treat a network
connection as a stream that bytes can be written
into or read from it.
21
Sockets

Java makes network programming easier by


encapsulating connection functionality in socket
classes.
The related classes are grouped together in java.net
package.
A socket can perform seven basic operations
1. Connect to remote machine (prepare to send/receive data)
2. Send data
3. Receive Data
4. Close a connection
5. Bind to a port
6. Listen for incoming data
7. Accept connections from remote machines on the bound port
Java’s Socket class, used by both clients and servers, has methods
corresponding to first four of these operations. Last three are
needed by only serves & are implemented by ServerSocket class
22
Socket class

A basic class for creating client sockets


Supports TCP protocol.
Designed to connect to server sockets & initiate
protocol exchanges
Constructors:
Socket(String hostname, int port)
Socket(InetAddress ia , int port)

23
ServerSocket class

Designed to be a listener which waits on a fixed


port for clients to connect
Once created, it will register itself with the system
as having an interest in the client’s connection.
Constructor of this class reflects the port number
that accepts connections.

24
ServerSocket class

ServerSocket(int port)
ServerSocket(int port, int maxQueue)
ServerSocket(int port, int maxQueue, InetAddress
localAddress)

Socket accept()

25
Socket Programming( Writing Server)

Create Server Socket


Wait for connection
Get I/O streams
Read/Write data
Close the connection

26
Socket Programming ( Writing Client)

Create a Socket
Get I/O streams
Read/Write data
Close the connection

27
java.net classes

InetAddress ServerSocket
DatagramPacket Socket
DatagramSocket SocketImpl
DatagramSocketImpl URL
MulticastSocket URLConnection

28
InetAddress class

represents an Internet Protocol (IP) address


Used to convert between host names & internet
addresses.
Class has no visible constructor.
To create an InetAddress object use one of the
available static methods. (Factory methods i.e.
static methods which return an instance of that
class)

29
InetAddress class – Factory Methods

InetAddress getByName(<url>)
InetAddress[ ] getAllByName(String hostname)
(All addresses that a particular name resolves to)
InetAddress getLocalHost()

- These methods throw UnknownHostException if they are


unable to resolve the host name

30
InetAddress class – Instance Methods

These methods are invoked using the object of


InetAddress class

byte[ ] getAddress() : Returns object’s IP Address


String getHostAddress()
String getHostName()
String toString() : Lists hostname and IP address

31
URL

Uniform Resource Locator (URL) uniquely identifies or


addresses the information on internet.
Every browser uses URLs to identify information on web
URL class in java.net package provides simple, concise API
to access information across the internet using URLs
An URL has four parts.
e.g. http://www.osborne.com:80/index.htm
1st part is protocol to use : e.g. http
2nd part is host name or IP address of the host to use
3rd part is port number which is optional : e.g. 80
4th part is actual file path

32
URL class constructors

Each constructor can throw MalformedURLException

URL(String urlSpecifier)

URL(String protocolName, String hostName, int port, String


path)

URL(String protocolName, String hostName, String path)

If port is not explicitly set, port is -1

33
URL class instance methods

String getProtocol()
int getPort()
String getHost()
String getFile()
String getRef()
URLConnection openConnection()

34
URLConnection class

It is general purpose class for accessing the attributes of a remote


resource
Once you make connection to a remote server, you can use
URLConnection to inspect the properties of the remote object
before actually transporting it locally.
These attributes are exposed by HTTP protocol specification and
hence only make sense for URL objects that are using the HTTP
protocol
URL and URLConnection classes are sufficient for simple
programs that want to connect to HTTP servers to fetch content.

35
Methods in URLConnection class

Int getContentLength() : Returns size in number of bytes


(-1 if length is unavailable)
Long getDate() : Time and date of response in miliseconds
Long getExpiration() : Expiration time and date of resource
(0 if unavaliable)
Long getLastModified() : time and date of last modification
(0 if unavaliable)
InputStream getInputStream() throws IOException : Returns
and InputStream that is linked to the resource. This stream
can be used to obtain content of the resource

36
Datagrams

Datagrams are bundles of information passed between


machines
Once the datagram has been released to its intended target,
there is no assurance that it will arrive or even that someone
will be there to catch it.
When the datagram is received, there is no assurance that it
hasn’t been damaged in transit or that whosoever sent it is
still there to receive a response
Java implements datagrams ona the top of the UDP
protocol by using two classes: DatagramPacket and
DatagramSocket

37
DatagramSocket

This class represents a socket for sending and


receiving datagram packets
A datagram socket is the sending or receiving
point for a packet delivery service
Each packet sent or received on a datagram
socket is individually addressed and routed.

38
Methods of DatagramSocket class

void send(DatagramPacket dp)


Sends the packet to the remote address specified in
the dp packet
void receive(Datagrampacket dp)
Blocking call- waits until it reads a packet from this
DatagramSocket.
void close()
Closes this socket & releases any resource allocated
to the socket object

39
DatagramPacket

This class represents a datagram packet


Datagram packets are used to implement a
connectionless packet delivery service
The packet class contains connection information
as well as the data.

40
Socket Programming

DatagramPacket(byte data[ ], int size)


Specifies a buffer that will receive data & the
size of a packet

DatagramPacket(byte data[ ], int


size,InetAddress ipAddress, int port)
Specifies a target address & port which are used
by datagram Socket to determine where the data
in the packet will be sent.

41
Methods of DatagramPacket

InetAddress getAddress()
Returns destination Inetaddress,typically used for sending.
int getPort()
Returns the port number
byte[] getData()
Returns data contained in datagram(after receiving)
int getLength()
Returns length of valid data contained in the byte array
that's returned from getData() method.

42
Socket Programming

DatagramSocket ds = new DatagramSocket (2000);


byte data[] = new byte[100];
receivePacket = new DatagramPacket(data, data.length);

ds.receive(receivePacket);
ds.send(receivePacket);
receivePacket.getAddress();
receivePacket.getPort();
receivePacket.getLength();
receivePacket.getData();

43

Вам также может понравиться