Network

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 37

Networking

Basic Communication Models

• Communication between the individual components of


a distributed system can occur in two basic ways:
using either shared memory or message passing.
• Shared memory is an indirect form of communication,
as both partners exchanging information do not
communicate directly with each other, but via a third
component: the shared memory.
• Message passing is a direct form of communication
between the sender and receiver by means of a
communication medium. Two functions are generally
available for the execution of message exchange,
usually called send and receive.
Basic Communication Models (cont.)
• Send is defined as: send(r: recevier, m: message)
– This function sends the message m to the receiver r.

• Receive is defined as: receive(s: sender, b: buffer)


– This function waits for a message from sender s and writes it in
buffer b (part of the memory made available for the application
process).

• The basic form of exchange of a single message can be


combined with more complex models. One of the most
important of these models is the client-server model.
– In this model, the communication partners adopt the role of either
a client or a server. A server is assigned to administer access to
a certain resource, while a client wishes to use the resource.
Message Exchange in the Client-Server Model
Client Server

Message 1: Access
to resource

Processing

s
f acces
su l t o
ge 2: R e
Messa
Internet Reference Model
Telnet HTTP DNS
Application
FTP Layer
SMTP NFS SNMP

TCP UDP Transport


Transmission User Datagram
Control Protocol Protocol
Layer

ICMP
OSPF/RIP Internet Control Message
Routing IP Network Layer
Information Internet Protocol
IGMP
Internet Group Management

ARP RARP Link Layer


Address Resolution
(LLC +)
MAC Reverse
Address Resolution
Internet Reference Model (cont.)

• The Link Layer describes the possible sub-networks of


the Internet and their medium access protocols. These
are, for example, Ethernets, token rings, FDDI, or ISDN
networks. To its upper layer, the link layer offers
communication between two computers in the same
sub-network as a service.
• The Network Layer unites all the sub-networks to
become the Internet. The service offered involves
making communication possible between any two
computers on the Internet. The network layer accesses
the services of the link layer, in that a connection
between two computers in different networks is put
together for many small connections in the same
network.
Internet Reference Model (cont.)

• The Transport Layer oversees the connection of two (or


more) processes between computers communicating with
each other via the network layer.
• The Application Layer makes application-specific services
available for inter-process communication. These
standardized services include e-mail, file transfer and the
World Wide Web.
• Within the layers, protocols are used for the production of a
service. Protocols are instances which can be implements
either in hardware or software, and communicate with their
partner instances in the same levels, but on other
computers. It is only this cooperation that enables the
service to be produced for the next level up.
Internet Reference Model (cont.)

• The TCP/IP Protocol constitutes the core of Internet


communication technology in the transport and network
layers.
• Every computer on the Internet always has an
implementation of both protocols, TCP (Transmission
Control Protocol) and IP (Internet Protocol).
• The task of IP is to transfer data from one Internet
computer (the sender) to another (the receiver). On
this basis, TCP then organizes the communication
between the two processes on these two computers.
Some Important Application Layer
Internet Protocols
• Telnet – makes a terminal emulation available on the remote computer.
The protocol enables logins to other computers using the network.
• HTTP – (Hypertext Transport Protocol) is the underlying protocol of the
World Wide Web. It is responsible for the transfer of hypertext
documents.
• SMTP – (Simple Mail Transfer Protocol) is the protocol used for the
transfer of e-mail messages.
• FTP – (File Transfer Protocol) is able to manage filestores on a server
and enables clients to access files.
• SNMP – (Simple Network Management Protocol) is used for network
management on the Internet.
• DNS – (Domain Name Service) is responsible for the mapping of
symbolic names to IP addresses.
• NFS – (Network File System) makes the basic functionality for a
distributed file system available.
The Architecture of a Web Service

Client Server
WWW browser WWW server
(Internet Explorer) (Apache)

HTTP HTTP

Port 80

TCP TCP

Request: Get http://cs.ucf.edu/courses/cop4610L/spr2005/favorites.html

Response: file html contents


url demo
import java.io.*;
import java.net.*;
class UrlDemo
{
public static void main(String args[]) throws MalformedURLException
{
URL hp=new URL("http://www.vrsiddhartha.ac.in/client");
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("Exit:"+hp.toExternalForm());

}
}
Networking
• Java’s fundamental networking capabilities are declared
by classes and interfaces of the java.net package,
through which Java offers stream-based communications.
• The classes and interfaces of java.net also offer
packet-based communications for transmitting individual
packets of information. This is most commonly used to
transmit audio and video over the Internet.
• We will focus on both sides of the client-server
relationship.
• The client requests that some action be performed, and
the server performs the action and responds to the client.
Networking (cont.)
• A common implementation of the request-response
model is between Web browsers and Web servers.
– When a user selects a Web site to browse through a
browser (a client application), a request is sent to the
appropriate Web server (the server application). The
server normally responds to the client by sending the
appropriate HTML Web page.
java.net
• “High-level” APIs
– Implement commonly used protocols such as HTTP, FTP, etc.

• “Low-level” APIs
– Socket-based communications
• Applications view networking as streams of data
• Connection-based protocol
• Uses TCP (Transmission Control Protocol)
– Packet-based communications
• Individual packets transmitted
• Connectionless service
• Used UDP (User Datagram Protocol)
Internet Reference Model

Application Layer
(HTTP, FTP, DNS, etc.)

Transport Layer
(TCP, UDP)

Network Layer
(IP)

Link and Physical Layer

See page 21 in part 1 for a more detailed version of this diagram.


Sockets
• Java’s socket-based communications enable applications to
view networking as if it were file I/O. In other words, a
program can read from a socket or write to a socket as
simply as reading from a file or writing to a file.
• A socket is simply a software construct that represents one
endpoint of a connection.
• Stream sockets enable a process to establish a connection
with another process. While the connection is in place,
data flows between the processes in continuous streams.
• Stream sockets provide a connection-oriented service. The
protocol used for transmission is the popular TCP
(Transmission Control Protocol). Provides reliable , in-
order byte-stream service
Sockets (cont.)
• Datagram sockets transmit individual packets of
information. This is typically not appropriate for use by
everyday programmers because the transmission
protocol is UDP (User Datagram Protocol).
• UDP provides a connectionless service. A
connectionless service does not guarantee that
packets arrive at the destination in any particular order.
• With UDP, packets can be lost or duplicated.
Significant extra programming is required on the
programmer’s part to deal with these problems.
• UDP is most appropriate for network applications that
do not require the error checking and reliability of TCP.
Sockets (cont.)
• Under UDP there is no “connection” between the
server and the client. There is no “handshaking”.
• The sender explicitly attaches the IP address and
port of the destination to each packet.
• The server must extract the IP address and port of
the sender from the received packet.
• From an application viewpoint, UDP provides
unreliable transfer of groups of bytes (“datagrams”)
between client and server.
Example: client/server socket interaction via UDP
Server (running on hostid) Client
create socket, port=x
for incoming request: create socket
serverSocket = DatagramSocket() clientSocket = DatagramSocket()

create, address(hostid, port=x)


read request from serverSocket send datagram request using clientSocket

Write reply to serverSocket read reply from clientSocket


specifying client host address, port
number
close clientSocket
import java.io.*; Example: Java server using UDP
import java.net.*;

class UDPServer {
public static void main(String args[]) throws Exception
{
//Create datagram socket on port 9876
DatagramSocket serverSocket = new DatagramSocket(9876);

byte[] sendData = new byte[1024];


byte[] receiveData = new byte[1024];

while (true)
{
//create space for the received datagram
DatagramPacket receivePacket = new
DatagramPacket(receiveData,
receiveData.length);
//receive the datagram
serverSocket.receive(receivePacket);

String sentence = new String(receivePacket.getData());


Example: Java server using UDP (cont.)

//get IP address and port number of sender


InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence =
sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
//create datagram to send to client
DatagramPacket sendPacket = new
DatagramPacket(sendData, sendData.length, IPAddress, port);
//write out the datagram to the socket
serverSocket.send(sendPacket);
} //end while loop
}
}
Example: Java client using UDP
import java.io.*;
import java.net.*;

class UDPClient {
public static void main(String args[]) throws Exception
{
//Create input stream
BufferedReader inFromUser = new BufferedReader(new
InputStreamReader(System.in));
//Create client socket
DatagramSocket clientSocket = new DatagramSocket();
//Translate hostname to IP address using DNS
InetAddress IPAddress = InetAddress.getByName("localhost");

byte[] sendData = new byte[1024];


byte[] receiveData = new byte[1024];

String sentence = inFromUser.readLine();


sendData = sentence.getBytes();
Example: Java client using UDP (cont.)
DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);

DatagramPacket receivePacket = new DatagramPacket(receiveData,


receiveData.length);

clientSocket.receive(receivePacket);

String modifiedSentence = new String(receivePacket.getData());

System.out.println("FROM SERVER: " + modifiedSentence);


clientSocket.close();
}
}
Start UDP server
executing

Start UDP client executing

Client sends a message


(datagram) to the server

Server responds by
returning the datagram
to the client in all capital
letters
Socket Programming with TCP
• Server process must first be running (must have created a
socket). Recall that TCP is not connectionless.
• Client contacts the server by creating client-local socket
specifying IP address and port number of server process.
Client TCP establishes connection to server TCP.
• When contacted by client, server TCP creates a new
socket for server process to communicate with client.
– Allows server to talk with multiple clients
– Source port numbers used to distinguish clients

• From application viewpoint: TCP provides reliable, in-order


transfer of bytes (“pipe”) between client and server.
Establishing a Simple Server Using Stream Sockets
Five steps to create a simple stream server in Java:
1. ServerSocket object. Registers an available port and a
maximum number of clients.
2. Each client connection handled with a Socket object. Server
blocks until client connects.
3. Sending and receiving data
• OutputStream to send and InputStream to receive data.
• Methods getInputStream and getOutputStream on
Socket object.
4. Process phase. Server and client communicate via streams.
5. Close streams and connections.
Establishing a Simple Client Using Stream Sockets

Four steps to create a simple stream client in Java:


1. Create a Socket object for the client.
2. Obtains Socket’s InputStream and OutputStream.
3. Process information communicated.
4. Close streams and Socket.
Example: client/server socket interaction via TCP
Server (running on hostid)
create socket, port=x
for incoming request:
welcomeSocket = ServerSocket()
Client

TCP connection setup


wait for incoming connection request create socket
conncectionSocket = Connect to hostid, port = x
welcomeSocket.accept() clientSocket = Socket()

read request from connectionSocket

send request using clientSocket


write reply to connectionSocket

read reply from clientSocket


close connectionSocket

close clientSocket
Example: Java server using TCP
//simple server application using TCP

import java.io.*;
import java.net.*;

class TCPServer {
public static void main (String args[]) throws Exception
{
String clientSentence;
String capitalizedSentence;

//create welcoming socket at port 6789


ServerSocket welcomeSocket = new ServerSocket(6789);

while (true) {
//block on welcoming socket for contact by a client
Socket connectionSocket = welcomeSocket.accept();

//create input stream attached to socket


BufferedReader inFromClient = new BufferedReader(new
InputStreamReader
(connectionSocket.getInputStream()));
Example: Java server using TCP (cont.)

//read in line from the socket


clientSentence = inFromClient.readLine();

//process
capitalizedSentence = clientSentence.toUpperCase() + '\n';

//create output stream attached to socket


DataOutputStream outToClient = new

DataOutputStream(connectionSocket.getOutputStream());
//write out line to socket
outToClient.writeBytes(capitalizedSentence);
}
}
}
Example: Java client using TCP
//simple client application using TCP

import java.io.*;
import java.net.*;

class TCPClient {
public static void main (String args[]) throws Exception
{
String sentence;
String modifiedSentence;

//create input stream


BufferedReader inFromUser = new BufferedReader(new
InputStreamReader(System.in));

//create client socket and connect to server


Socket clientSocket = new Socket("localhost", 6789);

//create output stream attached to socket


DataOutputStream outToServer = new
DataOutputStream(clientSocket.getOutputStream());
Example: Java client using TCP (cont.)
//create input stream attached to socket
BufferedReader inFromServer = new BufferedReader(new
InputStreamReader (clientSocket.getInputStream()));

sentence = inFromUser.readLine();

//send line to server


outToServer.writeBytes(sentence + '\n');

//read line from server


modifiedSentence = inFromServer.readLine();

System.out.println("FROM SERVER: " + modifiedSentence);

clientSocket.close();
}
}
Start TCP Server executing

Start a TCP Client executing and


send message to server.

Server responds and client process


terminates. The server is still
executing.

Another client begins execution


and the cycle repeats.
Client program
import java.io.*;
import java.net.*; while(true)
class bclient {
{ System.out.println("enter for msg
public static void main(String args[]) for server");
{ s=dis.readLine();
DataInputStream dis; pw.println(s);
BufferedReader br; System.out.println("server msg
is"+br.readLine());
PrintWriter pw;
}
Socket cs;
}
String ss,s; catch(Exception e)
try
{System.out.println(e);
{
}
dis=new DataInputStream(System.in);
}
cs=new Socket("localhost",100); }
br=new BufferedReader(new
InputStreamReader(cs.getInputStream()));
pw=new
PrintWriter(cs.getOutputStream(),true);
Server program
import java .io.*; while(true) {
import java.net.*; System.out.println("client msg
class bserver is"+br.readLine());
{ System.out.println("enter msg for
client");
public static void main(String args[])
s1=dis.readLine();
{
DataInputStream dis;
pw.println(s1);
PrintWriter pw; System.out.println("client msg
Socket cs; is"+br.readLine());
ServerSocket S; pw.println(dis.readLine());
String s1;
BufferedReader br;
try }
{
S=new ServerSocket(100); }
cs=S.accept(); catch(Exception e)
dis=new DataInputStream(System.in); {
br=new BufferedReader(new System.out.println(e);
InputStreamReader(cs.getInputStream())); }
pw=new PrintWriter(cs.getOutputStream(),true);
}
}

You might also like