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

Chap 1

Uploaded by

chandanaa0210
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 views48 pages

Chap 1

Uploaded by

chandanaa0210
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/ 48

Module 1

5th sem BCA


▪ Ken Thompson and Dennis Ritchie developed UNIX in concert with the C language
at Bell Telephone Laboratories, Murray Hill, New Jersey, in 1969
▪ In 1978, Bill Joy was leading a project at Cal Berkeley to add many new features to
UNIX, such as virtual memory and full-screen display capabilities
▪ By early 1984, just as Bill was leaving to found SunMicrosystems, he shipped
4.2BSD, commonly known as Berkeley UNIX
▪ 4.2BSD came with a fast file system, reliable signals, interprocess communication,
and, most important, networking
▪ The term network programming refers to writing programs that execute across
multiple devices (computers), in which the devices are all connected to each other
using a network.
▪ The java.net package of the J2SE APIs contains a collection of classes and
interfaces that provide the low-level communication details, allowing you to write
programs that focus on solving the problem at hand.
• TCP − TCP stands for Transmission Control Protocol, which allows for reliable
communication between two applications. TCP is typically used over the Internet
Protocol, which is referred to as TCP/IP.

• UDP − UDP stands for User Datagram Protocol, a connection-less protocol that
allows for packets of data to be transmitted between applications.
OSI LAYER TCP LAYER
Transmission control protocol (TCP) User datagram protocol (UDP)

▪ TCP is a connection-oriented protocol. ▪ UDP is the Datagram-oriented protocol.


Connection-orientation means that the This is because there is no overhead for
communicating devices should establish opening a connection, maintaining a
a connection before transmitting data connection, and terminating a
and should close the connection after connection. UDP is efficient for broadcast
transmitting the data. and multicast types of network
transmission.
Internet Protocol (IP) is a low-level routing protocol that
breaks data into small packets and sends them to an
address across a network, which does not guarantee to
deliver said packets to the destination

Transmission Control Protocol (TCP) is a higher-level


protocol that manages to robustly string together these
packets, sorting and retransmitting them as necessary to
reliably transmit your data

User Datagram Protocol (UDP), sits next to TCP and can be


used directly to support fast, connectionless, unreliable
transport of packets
• A server is anything that has some resource that can be
shared
• Compute servers which provides computing power, print
servers, which manage a collection of printers; disk
servers, which provide networked disk space; and web
servers, which store web pages
• A client is simply any other entity that wants to gain
access to a particular server
• The server is a permanently available resource, while the
client is free to “unplug” after it is has been served
▪ A server process is said to “listen” to a port until a client
connects to it
▪ A server is allowed to accept multiple clients connected to the
same port number, although each session is unique
▪ To manage multiple client connections, a server process must
be multithreaded or have some other means of multiplexing
the simultaneous I/O
▪ A socket is one endpoint of a two way communication link between two programs
running on the network.
▪ The socket provides bidirectional FIFO Communication facility over the network.
▪ A socket connecting to the network is created at each end of the communication.
Each socket has a specific address. This address is composed of an IP address and a
port number.
▪ Two classes are being used:
1. Socket - Communicate with client and server [we can read and write the message]
2. Server Socket - used at server-side.
The accept() method of Server Socket class blocks the console until the client is
connected.
Socket class Server Socket class

Method Description

1) public Socket accept() returns the socket and establish a


connection between server and
client.

2) public synchronized void close() closes the server socket.


▪ TCP and UDP supports 64K sockets, from socket number 0 to socket
number 65,535.
▪ Socket number between 0 to 1023 are reserved for popular
application protocols (e.g., Port 80 for HTTP, Port 443 for HTTPS, Port
21 for FTP, Port 23 for Telnet, Port 25 for SMTP, Port 110 for POP3, etc.
▪ Port number on and above 1024 are available
For example, HTTP is the protocol that web browsers and servers
use to transfer hypertext pages and images
▪ A packet is a small segment of a larger message.
▪ Data sent over computer networks is divided into packets. These
packets are then recombined by the computer or device that
receives them.
▪ TCP/IP sockets are used to implement reliable, bidirectional,
persistent, point-to-point, stream-based connections between hosts
on the Internet.
▪ A socket can be used to connect Java’s I/O system to other programs
that may reside either on the local machine or on any other machine
on the Internet.
▪ 2 types of socket – Server Socket [Server side]
-Socket Class [Client side]
▪ A proxy server is a system or router that provides a
gateway between users and the internet.
▪ It helps prevent cyber attackers from entering a private
network.
▪ Proxy server has its own IP adress
❑Currently, IPv4 is by far the most widely used scheme
❑There are 32 bits in an IPv4 IP address, and we often refer to
them as a sequence of four numbers between 0 and 255 separated
by dots (.)
❑The first few bits define which class of network, lettered A, B, C, D,
or E, the address represents
❑Most Internet users are on a class C network, since there are over
two million networks in class C
❑The first byte of a class C network is between 192 and 224, with
the last byte actually identifying an individual computer among the
256 allowed on a single class C network
URL
The URL provides a reasonably intelligible form to uniquely identify or
address information on the Internet

// Demonstrate URL.
import java.net.*;
class URLDemo {
public static void main(String args[]) throws MalformedURLException
{
URL hp = new URL("http://www.osborne.com/downloads");
System.out.println("Protocol: " + hp.getProtocol());
System.out.println("Port: " + hp.getPort());
System.out.println("Host: " + hp.getHost());
System.out.println("File: " + hp.getFile());
System.out.println("Ext:" + hp.toExternalForm());
}
}
URL
CONNECTION
URLConnection is a general-purpose class for accessing the
attributes of a remote resource

Once you make a connection to a remote server, you can use


URLConnection to inspect the properties of the remote object before
actually transporting it locally

we can create a URLConnection using the openConnection( )


method of a URL object
URL CONNECTION
import java.net.*;
import java.io.*;
import java.util.Date;
class UCDemo
{
public static void main(String args[]) throws Exception {
//int c;
URL hp = new URL("http://www.internic.net");
URLConnection hpCon = hp.openConnection();
long d = hpCon.getDate(); // get date
if(d==0)
System.out.println("No date information.");
else
System.out.println("Date: " + new Date(d));
// get content type
System.out.println("Content-Type: " + hpCon.getContentType());
}
}
JAVA AND THE NET

❑ Java supports both the TCP and UDP protocol families

❑ TCP is used for reliable stream-based I/O across the network

❑UDP supports a simpler, hence faster, point-to-point datagram-


oriented model
JAVA AND THE NET
The Networking Classes and Interfaces

❑ The classes contained in the java.net package are listed here:


JAVA AND THE NET
The Networking Classes and Interfaces

❑ The java.net package’s interfaces are listed here:


INETADDRESS
Numerical
IP address

InetAddress
class

Domain
name
▪ IP address -> main factor for communication
▪ Ip address -> 32 bit or 128 bits
▪ Inet address class provides methods to get Ip of any host name ( server or client side)
▪ Address- It is a logical or physical address that uniquely identifies a host or a machine in a
telecommunication network.

1. Unicast 2. multicast 3.broadcast


FACTORY METHODS

❑ The InetAddress class has no visible constructors

❑To create an InetAddress object, you have to use one of the available factory methods

❑ Three commonly used InetAddress factory methods are

1. static InetAddress getLocalHost( ) throws UnknownHostException


getLocalHost( ) method simply returns the InetAddress object that represents the local host
FACTORY METHODS
2. static InetAddress getByName(String hostName)
throws UnknownHostException

The getByName( ) method returns an InetAddress for a host name


passed to it

3. static InetAddress[ ] getAllByName(String hostName)


throws UnknownHostException

The getAllByName( ) factory method returns an array of


InetAddresses that represent all of the addresses that a particular
name resolves to. It will also throw an UnknownHostException if it
can’t resolve the name to at least one address
FACTORY METHODS

Example: To print the addresses and names of the local


machine and two well-known Internet web sites
import java.net.*;
class InetAddressTest
{
public static void main(String args[]) throws UnknownHostException
{
InetAddress Address = InetAddress.getLocalHost();
System.out.println(Address);
Address = InetAddress.getByName("osborne.com");
System.out.println(Address);
InetAddress SW[] = InetAddress.getAllByName("www.nba.com");
for (int i=0; i<SW.length; i++)
System.out.println(SW[i]);
}
}
FACTORY METHODS

Output

default/206.148.209.138
osborne.com/198.45.24.162
www.nba.com/64.241.238.153
www.nba.com/64.241.238.142
INSTANCE METHODS
The InetAddress class also has several other methods, which can
be used on the objects returned by the methods
Methods Description
boolean equals(Object Returns true if this object has the same Internet
other) address as other
byte[ ] getAddress( ) Returns a byte array that represents the object’s
Internet address in network byte order

String getHostAddress( ) Returns a string that represents the host address


associated with the InetAddress object

String getHostName( ) Returns a string that represents the host name


associated with the InetAddress object

boolean isMulticastAddress( Returns true if this Internet address is a multicast


) address. Otherwise, it returns false

String toString( ) Returns a string that lists the host name and the IP
address for convenience
▪ TCP is a Network Protocol that stands for Transfer Control Protocol,
which allows well-founded communication between applications.
▪ The communication mechanism between two systems, using TCP, can
be established using Sockets and is known as Socket Programming.
▪ Socket programming is a concept of Network Programming, that
suggests writing programs that are executed across multiple systems,
which are connected to each other using a network.
Mechanism of socket programming
• A client creates a socket at its end of transmission and strives to
connect the socket to the server. When a connection is established,
the server creates a socket at its end and, the client and server can
now ready communicate through writing and reading methods.
Steps in TCP connection
1. An object of ServerSocket is instantiated, and desired port number is specified, on which connection is
going to take place.
2. The accept method of ServerSocket is invoked, in order to hold the server in listening mode. This
method won’t resume until a client is connected to the server through the given port number.
3. Now, on the client-side, an object of Socket is instantiated, and desired port number and IP address is
specified for the connection.
4. An attempt is made, for connecting the client to the server using the specified IP address and port
number. If the attempt is successful, the client is provided with a Socket that is capable of
communicating to the respective server, with write and read methods. If unsuccessful, the desired
exception is raised.
5. Since a client is connected to the server, accept method on the server-side resumes, providing
a Socket that is capable of communicating to the connected client.
6. Once the communication is completed, terminate the sockets on both, the server and the client-side.
TCP/IP CLIENT SOCKETS

❑TCP/IP sockets are used to implement reliable, bidirectional, persistent, point-to- point, stream-
based connections between hosts on the Internet

❑A socket can be used to connect Java’s I/O system to other programs that may reside either on
the local machine or on any other machine on the Internet

❑There are two kinds of TCP sockets in Java. One is for servers, and the other is for clients

❑The ServerSocket class is designed to be a “listener,” which waits for clients to connect
before doing anything

❑The Socket class is designed to connect to server sockets and initiate protocol exchanges
TCP/IP CLIENT SOCKETS

❑ Constructors used to create client sockets

Socket(String hostName, int port) Creates a socket connecting the


local host to the named host
and port; can throw an
UnknownHostException or
an IOException
Socket(InetAddress ipAddress, Creates a socket using a
int port) preexisting InetAddress object
and a port; can throw an
IOException
TCP/IP CLIENT SOCKETS

A socket can be examined at any time for the address and port
information associated with it, by use of the following methods

InetAddress getInetAddress( Returns the InetAddress


) associated with the
Socket object
int getPort( ) Returns the remote port
to which this Socket
object is connected
int getLocalPort( ) Returns the local port to
which this Socket object
is connected
TCP/IP CLIENT SOCKETS

❑Once the Socket object has been created, it can also be


examined to gain access to the input and output streams
associated with it

❑Each of these methods can throw an IOException if the sockets


have been invalidated by a loss of connection on the Net

InputStream getInputStream( ) Returns the InputStream


associated with the invoking
socket
OutputStream Returns the OutputStream
getOutputStream( ) associated with the invoking
socket
Client side programming
• First to establish socket connection. Socket Connection is when the two machines have
information about each other’s network location (IP Address) and the TCP port.
• Connection:
Socket socket = new Socket(IP Address, port number);
//IP Address of the server
//TCP port Number on which the server is listening.
• Communication:
The communication is held using the output and input streams. These streams are responsible for
transmitting the information in the form of byte array.
• Terminating Connection:
The socket connection needs to be terminated explicitly once the communication is fulfilled.
TCP/IP SERVER SOCKETS

The ServerSocket class is used to create servers that listen for


either local or remote client programs to connect to them on
published ports

When you create a ServerSocket, it will register itself with the


system as having an interest in client connections

The constructors for ServerSocket reflect the port number that


you wish to accept connections on and, optionally, how long you
want the queue for said port to be

The queue length tells the system how many client connections it
can leave pending before it should simply refuse connections. The
default is 50
TCP/IP SERVER SOCKETS

ServerSocket(int port) Creates server socket on the


specified port with a queue
length of 50
ServerSocket(int port, Creates a server socket on the
int maxQueue) specified port with a maximum
queue length of maxQueue

ServerSocket(int port, Creates a server socket on the


int maxQueue, specified port with a maximum
InetAddress localAddress) queue length of maxQueue. On a
multihomed host, localAddress
specifies the IP address to which
this socket binds
▪ Server Socket is required, which will wait for the client in the listening mode,
on a particular TCP port.
▪ This Server Socket holds until a Client Socket is connected successfully.
▪ As soon as the client is connected, another Socket comes into existence that
will enable data sharing between the respective client and server. A
temporary Socket is created to handle the request from that particular client
and our main server socket will be free again, to listen to other requests.
Hence we have Server Socket and Socket on the server-side.
Steps in server side programming
1. Creating Server Socket:
The Server Socket is instantiated on a specific port number. Then, the server starts listening
for the client requests coming in for that port number. The accept method waits until a client
connects to the server. After a successful connection, accept returns a Socket that will
facilitate the communication.
ServerSocket sersock = new ServerSocket(9990);
2. Communication:
For data transfer, the getOutputStream method is used to send the output through the
socket, and input is received using getInputStream method.
The Server keeps receiving messages until it receives the terminator.
3. Terminating Connection:
Once the response is sent, the socket, along with the input and output streams needs to be
closed explicitly.
DATAGRAM
❑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 whoever sent it is still there to receive a response

❑Java implements datagrams on top of the UDP protocol by using two classes:
The DatagramPacket object is the data container, while the
DatagramSocket is the mechanism used to send or receive the
DatagramPackets
DatagramPacket
DATAGRA - Constructors
MS
❑DatagramPacket(byte data[ ], int size) - specifies a buffer that will
receive data, and the size of a packet

❑DatagramPacket(byte data[ ], int offset, int size) - allows you to


specify an offset into the buffer at which data will be stored.

❑DatagramPacket(byte data[ ], int size, InetAddress ipAddress, int


port) - specifies a target address and port, used by a
DatagramSocket to determine where the data in the packet will be
sent
❑DatagramPacket(byte data[ ], int offset, int size, InetAddress
ipAddress, int port) - transmits packets beginning at the specified
offset into the data
DATAGRA
MS
DatagramPacket - Methods

InetAddress Returns the destination InetAddress,


getAddress( typically used for sending
)
int getPort( ) Returns the port number
byte[ ] getData( ) Returns the byte array of data contained
in the datagram. Mostly used to retrieve
data from the datagram after it has been
received
int getLength( ) Returns the length of the valid data
contained in the byte array that would be
returned from the getData( )method.

You might also like