lecture6 - Academic Csuohio

Download Report

Transcript lecture6 - Academic Csuohio

EEC-484/584
Computer Networks
Lecture 6
Wenbing Zhao
[email protected]
(Part of the slides are based on Drs. Kurose & Ross’s slides
for their Computer Networking book, and on materials supplied by
Dr. Louise Moser at UCSB and Prentice-Hall)
2
Outline
•
•
•
•
•
Mock quiz
Email, FTP
Socket programming
Physical layer – transmission theory
Ethereal Labs: can be downloaded at
– http://www-net.cs.umass.edu/ethereal-labs/
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
3
Electronic Mail
user mailbox
Three major components:
• user agents
• mail servers
• simple mail transfer protocol:
SMTP
outgoing
message queue
user
agent
mail
server
SMTP
SMTP
User Agent
• “mail reader”
mail
• composing, editing, reading
server
mail messages
• outgoing, incoming
user
messages stored on server
SMTP
user
agent
mail
server
user
agent
user
agent
user
agent
agent
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
4
Electronic Mail: Mail Servers
user
agent
Mail Servers
mail
• mailbox contains incoming
server
messages for user
SMTP
• message queue of outgoing
(to be sent) mail messages
SMTP
• SMTP protocol between mail
SMTP
servers to send email
mail
messages
server
– client: sending mail server
user
agent
– “server”: receiving mail
user
server
agent
Spring Semester 2006
EEC-484/584: Computer Networks
user
agent
mail
server
user
agent
user
agent
Wenbing Zhao
5
SMTP [RFC 2821]
• Uses TCP to reliably transfer email message from
client to server, port 25
• Direct transfer: sending server to receiving server
– The receiving server may be either the ultimate
destination or an intermediate
• Three phases of transfer
– Handshaking (greeting)
– Transfer of messages
– Closure
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
6
SMTP [RFC 2821]
• Command/response interaction
– Commands: ASCII text
– Response: status code and phrase
• SMTP requires message (header & body) to be
in 7-bit ASCII
• SMTP server uses CRLF.CRLF to determine
end of message
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
Alice Sends Message to Bob
1) Alice uses UA to compose
message and “to”
[email protected]
2) Alice’s UA sends message to
her mail server; message
placed in message queue
3) Client side of SMTP opens
TCP connection with Bob’s
mail server
1
user
agent
Spring Semester 2006
2
mail
server
3
4) SMTP client sends Alice’s
message over the TCP
connection
5) Bob’s mail server places
the message in Bob’s
mailbox
6) Bob invokes his user agent
to read message
mail
server
4
5
EEC-484/584: Computer Networks
6
user
agent
Wenbing Zhao
7
8
Sample SMTP Interaction
S:
C:
S:
C:
S:
C:
S:
C:
S:
C:
C:
C:
S:
C:
S:
220 hamburger.edu
HELO crepes.fr
250 Hello crepes.fr, pleased to meet you
MAIL FROM: <[email protected]>
250 [email protected]... Sender ok
RCPT TO: <[email protected]>
250 [email protected] ... Recipient ok
DATA
354 Enter mail, end with "." on a line by itself
Do you like ketchup?
How about pickles?
.
250 Message accepted for delivery
QUIT
221 hamburger.edu closing connection
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
9
Comparison of SMTP and HTTP
• HTTP: pull
• SMTP: push
• Both have ASCII command/response
interaction, status codes
• HTTP: each object encapsulated in its own
response message
• SMTP: multiple objects sent in multipart
message
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
10
Mail Message Format
SMTP: protocol for
exchanging email msgs
RFC 822: standard for text
message format:
• Header lines, e.g.,
header
blank
line
body
– To:
– From:
– Subject:
different from SMTP commands!
• Body: the “message”, ASCII characters only
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
11
Message Format: Multimedia Extensions
• MIME: Multipurpose Internet Mail Extensions, RFC 2045, 2056
• Additional lines in msg header declare MIME content type
• Purpose: Define encoding rules for non-ASCII messages
MIME version
method used
to encode data
multimedia data
type, subtype,
parameter declaration
encoded data
Spring Semester 2006
From: [email protected]
To: [email protected]
Subject: Picture of yummy crepe.
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Type: image/jpeg
base64 encoded data .....
.........................
......base64 encoded data
EEC-484/584: Computer Networks
Wenbing Zhao
12
FTP: File Transfer Protocol
user
at host
FTP
FTP
user
client
interface
file transfer
local file
system
FTP
server
remote file
system
• Transfer file to/from remote host, server on port 21
• Client/server model
– Client: side initiates transfer (either to/from remote)
– Server: remote host
• FTP: RFC 959
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
13
FTP: Separate Control,
Data Connections
• Control connection: “out
of band”
• FTP server maintains
“state”: current directory,
earlier authentication
TCP control connection
port 21
FTP
client
TCP data connection
port 20
FTP
server
Active FTP is shown
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
14
FTP Commands and Responses
Sample commands:
• sent as ASCII text over control channel
• USER username
• PASS password
• LIST return list of file in current directory
• RETR filename retrieves (gets) file
• STOR filename stores (puts) file onto remote
host
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
15
FTP Commands and Responses
Sample return codes
• status code and phrase (as in HTTP)
• 331 Username OK, password required
• 125 data connection already open;
transfer starting
• 425 Can’t open data connection
• 452 Error writing file
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
16
Socket Programming
Goal: Learn how to build client/server application
that communicate using sockets
socket
Socket API
• Explicitly created, used,
released by apps
• Client/server paradigm
• Two types of transport
service via socket API:
– Unreliable datagram
– Reliable, byte streamoriented
Spring Semester 2006
a host-local,
application-created,
OS-controlled interface
(a “door”) into which
application process can
both send and receive
messages to/from another
application process
EEC-484/584: Computer Networks
Wenbing Zhao
17
Socket Programming with TCP
Socket: a door between application process and
end-to-end transport protocol (UCP or TCP)
TCP service: reliable transfer of bytes from one
process to another
controlled by
application
developer
controlled by
operating
system
process
process
socket
TCP with
buffers,
variables
internet
controlled by
operating
system
host or
server
host or
server
Spring Semester 2006
socket
TCP with
buffers,
variables
controlled by
application
developer
EEC-484/584: Computer Networks
Wenbing Zhao
18
Socket Programming with TCP
Client must contact server
• Server process must first be running
• Server must have created socket (door) that welcomes
client’s contact
Client contacts server by:
• Creating client-local TCP socket
• Specifying IP address, port number of server process
• When client creates socket: client TCP establishes
connection to server TCP (Java only)
– For C/C++, you need to call connect() explicitly
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
19
Socket Programming with TCP
• When contacted by client, server TCP creates
new socket for server process to communicate
with client
– Allows server to talk with multiple clients
– Source port numbers used to distinguish
clients
application viewpoint
TCP provides reliable, in-order
transfer of bytes (“pipe”)
between client and server
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
Client/Server Socket Interaction: TCP
Server (running on hostid)
Client
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()
TCP
wait for incoming
connection request connection
connectionSocket =
welcomeSocket.accept()
setup
create socket,
connect to hostid, port=x
clientSocket =
Socket()
send request using
clientSocket
read request from
connectionSocket
write reply to
connectionSocket
read reply from
clientSocket
close
connectionSocket
Spring Semester 2006
close
clientSocket
EEC-484/584: Computer Networks
Wenbing Zhao
20
21
Stream Jargon (Java)
Spring Semester 2006
Clien
Stream
t
Process
proc
Output
ess
Stream
client
clientSocket
TCP
socket
to network
EEC-484/584: Computer Networks
inFromServer
Input
outToServer
• A stream is a sequence
of characters that flow
into or out of a process
• An input stream is
attached to some input
source for the process,
e.g., keyboard or socket
• An output stream is
attached to an output
source, e.g., monitor or
socket
inFromUser
Keyboard Monitor
Input
Stream
TCP
Socket
from network
Wenbing Zhao
22
Socket Programming with TCP
Example client-server app:
1) Client reads line from standard input (inFromUser
stream) , sends to server via socket (outToServer
stream)
2) Server reads line from socket
3) Server converts line to uppercase, sends back to client
4) Client reads, prints modified line from socket
(inFromServer stream)
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
23
Example: Java client (TCP)
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
Create
input stream
Create
client socket,
connect to server
Create
output stream
attached to socket
Spring Semester 2006
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("hostname", 6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
EEC-484/584: Computer Networks
Wenbing Zhao
24
Example: Java Client (TCP)
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();
}
}
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
Example: Java Server (TCP)
import java.io.*;
import java.net.*;
class TCPServer {
Create
welcoming socket
at port 6789
Wait, on welcoming
socket for contact
by client
Create input
stream, attached
to socket
Spring Semester 2006
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
EEC-484/584: Computer Networks
Wenbing Zhao
25
Example: Java Server (TCP)
Create output
stream, attached
to socket
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
Read in line
from socket
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
Write out line
to socket
outToClient.writeBytes(capitalizedSentence);
}
}
}
Spring Semester 2006
End of while loop,
loop back and wait for
another client connection
EEC-484/584: Computer Networks
Wenbing Zhao
26
27
Socket Programming with UDP
UDP: no “connection” between
client and server
• No handshaking
• Sender explicitly attaches IP
address and port of
destination to each packet
• Server must extract IP
address, port of sender from
received packet
Spring Semester 2006
UDP: transmitted data
may be received out of
order, or lost
application viewpoint
UDP provides unreliable transfer
of groups of bytes (“datagrams”)
between client and server
EEC-484/584: Computer Networks
Wenbing Zhao
28
Client/Server Socket Interaction: UDP
Server (running on hostid)
create socket,
port=x, for
incoming request:
serverSocket =
DatagramSocket()
read request from
serverSocket
write reply to
serverSocket
specifying client
host address,
port number
Spring Semester 2006
Client
create socket,
clientSocket =
DatagramSocket()
Create, address (hostid, port=x,
send datagram request
using clientSocket
read reply from
clientSocket
close
clientSocket
EEC-484/584: Computer Networks
Wenbing Zhao
29
Example: Java Client (UDP)
packet (recall
that TCP sent
“byte stream”)
sendPacket
Output: sends
Clien
Stream
t
Process
proc
UDP
ess
Packet
client
Client UDP
TCP
Socket
socket
to network
Spring Semester 2006
Input: receives
receivePacket
Input
Monitor
inFromUser
Keyboard
packet (recall that
TCP received
“byte stream”)
UDP
Packet
from network
EEC-484/584: Computer Networks
Wenbing Zhao
Example: Java Client (UDP)
import java.io.*;
import java.net.*;
Create
input stream
Create
client socket
Translate
hostname to IP
address using DNS
class UDPClient {
public static void main(String args[]) throws Exception
{
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("hostname");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
30
31
Example: Java Client (UDP)
Create datagram
with data-to-send,
length, IP addr, port
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
Send datagram
to server
clientSocket.send(sendPacket);
Read datagram
from server
clientSocket.receive(receivePacket);
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
String modifiedSentence =
new String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
Example: Java Server (UDP)
import java.io.*;
import java.net.*;
Create
datagram socket
at port 9876
class UDPServer {
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
Create space for
received datagram
Receive
datagram
Spring Semester 2006
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
EEC-484/584: Computer Networks
Wenbing Zhao
32
Example: Java Server (UDP)
String sentence = new String(receivePacket.getData());
Get IP addr
port #, 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
datagram
to socket
serverSocket.send(sendPacket);
}
}
}
Spring Semester 2006
End of while loop,
loop back and wait for
another datagram
EEC-484/584: Computer Networks
Wenbing Zhao
33
34
Theoretical Basis for Data
Communication
• Fourier analysis
• Bandwidth-limited signals
• Maximum data rate of channel
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
35
Fourier Analysis
• Info is transmitted by varying voltage or current
• Let f(t) be value of voltage or current at time t, any
well-behaved periodic function g(t) with period T
can be represented as Fourier series
n-th harmonics
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
36
Fourier Analysis
• Fundamental frequency f=1/T
• an and bn are sine and cosine amplitudes
of nth harmonics (terms)
• The amplitudes and constant are given by
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
37
Fourier Analysis
• The more harmonics we include in transmission, the more
accurate the signal is
• Bottom line: we must at least include 1 harmonic
Sending a byte ‘b’
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
38
Fourier Analysis
• The more harmonics we include in transmission, the
easier to reconstruct the signal
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
39
Bandwidth-Limited Signals
• Problem with physical medium
– Signal attenuates during propagation
– Signals with different frequencies might attenuate
different => distortion of the signal shape
• Cutoff frequency: The Fourier amplitudes are
transmitted undiminished from 0 up to some
frequency fc
– fc is measured in cycles/sec or Hertz (Hz)
– fc is usually determined as the frequency at which half
the power gets through
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
40
Bandwidth-Limited Signals
• Bandwidth: The range of frequencies
transmitted without being strongly attenuated,
i.e., 0 - fc
• The bandwidth is a physical property of the
transmission medium and usually depends the
construction, thickness, and length of the
medium
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
41
Bandwidth-Limited Signals
• Artificial limitation on bandwidth: A filter is
introduced into the circuit to limit the amount of
bandwidth available to each customer
– For example, a telephone wire may have a bandwidth
of 1 MHz for short distances, but telephone
companies add a filter restricting each customer to
about 3100 Hz
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
42
Bandwidth and Data Rate
• Data rate: how many bits can you send per
second?
– Higher data rate, shorter T, or higher fundamental
frequency f
– You need to allow at least a couple of harmonics to go
through to reconstruct the signal
• Higher data rate => requires higher bandwidth
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
43
Bandwidth and Data Rate
• Example:
–
–
–
–
–
Voice line cut off frequency ~ 3000Hz
Data rate is b bits/sec
During each period, send 8 bits => f = b/8 Hz
If b = 9600 bps
Number of highest harmonic ~ 3000/(b/8) = 24000/b
= 2.5 => poor reception
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
44
Nyquist Theorem
• For noiseless channels
• Number of sampling needed to reconstruct an arbitrary
signal transmitted through a low-pass filter of bandwidth H
=> 2H
• Sampling: measuring the continuous signal's value every T
units of time
Shown in
Frequency domain
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
45
Nyquist Theorem
• Max data rate = 2H log2V bits/sec, where
signal consists of V discrete levels
– Ex: H = 3000 Hz, V = 2 (binary)
max data rate = 2*3000*log22 = 6000 bits/sec
– Ex: H = 3000 Hz, V = 64
max data rate = 2*3000*log264 = 36,000
bits/sec
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
46
Shannon Theorem
•
•
•
•
•
For noisy channels: actual medium has thermal noise
Max data rate = H log2(1+S/N) bits/sec
Signal to noise ratio = signal power / noise power = S/N
Often use decibel (dB) as unit for S/N: 10 log10S/N
Ex: H = 3000 Hz, S/N = 30dB = 1000
max data rate = 3000*log2(1+1000) = 30,000
upper bound is hard to reach, 9600 bits/sec is good
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
47
Optional Homework
• Q1. Television channels are 6 MHz wide.
How many bits/sec can be sent if fourlevel digital signals are used? Assume a
noiseless channel.
• Q2. A noiseless 4-kHz channel is sampled
every 1 msec. What is the maximum data
rate? (Assuming each sample is 16 bits)
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao
48
Optional Homework
• Q3. What signal-to-noise ratio is needed to
put a T1 carrier (1.544 Mbps) on a 50-kHz
line?
• Q4. If a binary signal is sent over a 3-kHz
channel whose signal-to-noise ratio is 20
dB, what is the maximum achievable data
rate?
Spring Semester 2006
EEC-484/584: Computer Networks
Wenbing Zhao