Home directory - Computer and Information Science

Download Report

Transcript Home directory - Computer and Information Science

Midterm Review
Part of the slides are adapted from Computer Networking: A Top Down
Approach
Jim Kurose, Keith Ross
Addison-Wesley, April 2009.
Outline
Introduction to basic Unix commands, bash
scripting
 Sending attachment in email via telnet
 DNS lookup tool: dig
 Review with a focus on

 Transport layer and application layer
 Understanding socket
 A sample client/server in JAVA
How to send attachment in
email using telnet?

Need to
 Encode word doc (or other files)
 Type an EMAIL message following standard (RFC822 and
MIME), copy & paste encoded file
 Become tedious if doing it by hand

Scripting language: bash to the rescue




Basic building block: commands
Provide control structure for selection, loop
Provide support for variables, expression
No need to compile (in contrast to C/C++, Java)
Shell: a quick start

An important tool for sys. admin !!
 Automation: no need to type a long sequence of
commands to accomplish a task each time
 write a shell script with these commands, and run the
shell script each time

PuTTy, a telnet/ssh client
 a free and open source terminal emulator application
 a window in your desktop that works like old time
terminal commonly used to interact with Unix machines

After you log on, you are talking to shell
4
Your first encounter: shell


Shell: interactive command interpreter
A program that does the following
1. Displays a prompt message, e.g., [zhang@storm ~]$
2. Waits for user to type in a command line
3. From command line, extracts command name and
arguments
4. Searches for the program, and load the program to
runs it.
5. When program finishes, shell continues with 1
6. command “exit” or “ctrl-d” to end shell program
5
Check/Change Login Shell
Many variations: shell, csh, bash, tcsh, ksh
 To check the shell you are using, type
command

echo $SHELL
 Display value of a variable named SHELL
login shell: default shell for a user, specified
in /etc/passwd
 To change your login shell, use command

 chsh
6
UNIX command line

Command name and arguments:
$ command [ [ - ] option (s) ]
[option argument (s) ] [ command argument (s) ]
 Command arguments are mostly file or directory names
• cp prog1.cpp prog1.cpp.bak
 Options: used to control behavior of the command
• head -20 lab1.cpp
• wc –w lab2.cpp ##count how many words
• Some options come with option argument
– sort –k 1 data.txt
– // use the first column of data.txt as the key to sort
7
Unix File

Files: store information
 a sequence of 0 or more bytes containing arbitrary
information

What's in a filename?
 Case matters, no longer limited to 14 chars
 Special characters such as -, spaces are allowed,
but you shouldn’t use them in filename
• Can you think of the reason ?
 Dot files are hidden, i.e., normally not listed by
command ls
• To display all files, including hidden files, use ls -a
Hierarchical file system
• Directory: a file that can hold other files
• Advantages of hierarchical file system:
• Files can have same names, as long as they are
under different directories
/ (root)
• Easier for protection
• Organized files dev
lib
bi
home
et
n
cdrom
tty24
c
staff
passwd
zhang
9
Absolute pathname, path
/ (root)
dev
cdrom
tty24
bi
n
home
staff
et
c
lib
passwd
zhang
 Pathname of a file/directory: location of file/directory in the
file system
 How do you tell other where your prog. Is located ?
 Absolute pathname: path name specified relative to root, i.e.,
starting with the root (/)
 e.g., /home/staff/zhang
 What’s the absolute pathname for the “passwd” file?
10
Home directory

Every user has a home directory created
for him/her
 When you log in, you are in your home directory
 In home directory, a user usually has permission
to create files/directories, remove files ..
 ~ to refer to current user’s home directory
 ~username to refer to username’s home
directory
Current directory & Relative
Pathname
Tiring to specify absolute pathname each
time
 To make life easier

 User can move around the file system, shell
remembers where user is (i.e., current
directory)
• To check your current directory, use command:
pwd
 Use relative path name: specified relative to
current directory
Command for change current
directory (move around)
Usage: cd [directory]
[zhang@storm Work]$ cd
[zhang@storm ~]$ pwd
/home/staff/zhang
[zhang@storm ~]$ cd Work
[zhang@storm Work]$ pwd
/home/staff/zhang/Work
[zhang@storm Work]$ cd ..
[zhang@storm ~]$ pwd
/home/staff/zhang
[zhang@storm ~]$
13
Relative pathname


Absolute pathname: specified relative to root
Relative pathname: specified relative to current directory
 . (current directory), .. (parent directory, one level up)
 If current directory is at /home/staff/zhang, what is the
relative pathname of the file passwd?
• ../../../etc/passwd: go one level up, go one level up, go one
level up, go to etc, passwd is there
/ (root)
dev
cdrom
tty24
bi
n
home
staff
zhang
et
c
lib
passwd
Relative pathname
For all commands that take file/directory
name as arguments, you can use pathnames
 Example:





cd /home/staff/zhang/public_html
pico CISC3130/index.html
cd .. (go up one level to parent directory)
cp ../prog2.cpp prog2.cpp
Getting around in the file
system

ls: list directory contents
 ls [OPTION] … [FILE]
ls: list files/directories under current directory
ls –l: long listing,
[zhang@storm CISC1600]$ ls -l
total 59180
-rw-r--r-- 1 zhang staff 509952 Sep 7 13:02 3_types.ppt
-rw-r--r-- 1 zhang staff 593408 Sep 14 23:38 4_computation.ppt
-rw-r--r-- 1 zhang staff 1297 Sep 2 12:18 account.html
-rw-r--r-- 1 zhang staff 3304448 Nov 7 18:24 ArrayVector1.ppt
drwxr-xr-x 2 zhang staff 4096 Dec 8 22:36 Codes
Getting around in the file
system

To create a subdirectory:






mkdir [option]… directory…
cd
mkdir CISC3130
cd CISC3130
mkdir lab1
To remove a directory:
 rmdir [option]… directory…
 Report failure if directory is not empty
• Can use rm –rf to remove non-empty directory
File Viewing Commands



cat: concatenate files and display on standard
output (i.e., the terminal window)
[ ] means the argument is optional
 cat [option] … [file] …
… means there can be multiple
 cat proj1.cpp
arguments of this type
 cat proj1.cpp proj2.cpp
 cat –n proj1.cpp // display the file with line #
more, less: file perusal filter (i.e., displaying file one
screen at a time)
 more proj1.cpp
head, tail: display the beginning or ending lines of a
file
The most important command !!!
man ls

man: displaying online manuals
 Press q to quit, space to scroll down, arrow keys
to roll up/down
File manipulation commands


rm: remove one or multiple files or directories
 rm [option] … FILE …
 rm temp
 rm temp1 temp2
Wildcards (metacharacter) can be used in command
line
 Letter * matches with any string
• rm *.o: remove all .o files

 ?: match any one character
 [abc]: match with letter a or b or c
rm –r: remove directories and their sub-dirs
recursively
File manipulation commands (2)

cp: copy file or directory
 cp [OPTION] SOURCE DESTINATION

To make a backup copy of your program
before dramatic change
 cp proj1.cpp proj1.cpp.bak

To make a backup copy of a whole
directory
 cp –r lab1_dir lab1_dir_backup
 -R, -r, --recursive: copy directories recursively
File manipulation commands (3)

mv: move (rename) files/directories
 mv [OPTION] SOURCE DEST
• Rename SOURCE to DEST
• mv proj1.cpp lab1.cpp
 mv [OPTION]… SOURCE… DIRECTORY
• Move SOURCE to DIRECTORY
• mv lab1.cpp lab2.cpp CISC3130
Our first shell script

Edit a file named firstscript with the sequence of
commands, e.g.,
#!/bin/bash
date
who
echo “Hello world!”

Make file executable by owner (You) using
command
chmod u+x firstscript

Run script by typing following command line
 ./firstscript
More on this later

Useful commands:
 grep, head, tail, …

bash construct
 Pipeline, loop,…
Outline
Introduction to basic Unix commands, bash
scripting
 Sending attachment in email via telnet
 DNS lookup tool: dig
 Review with a focus on

 Transport layer and application layer
 Understanding socket
 A sample client/server in JAVA
Message format Standard: RFC
822

Internet e-mail: designed for plain text
messages
 many systems expect messages to only contain
printable characters from 7-bit (first bit of 8bit byte is zero) ASCII character set.

Potential problems
 if message includes extended 8-bit (first bit is
a one) characters, such as the various accented
letters.
 Send files, such as images, sound, video,
spreadsheets, documents and programs which
can contain any combination of 8-bit binary
data.
MIME

Multipurpose Internet Mail Extensions (MIME)
 to allow e-mail to contain multimedia content, binary
files, and text files using non-ASCII character sets, all
while still adhering to the RFC 822 message format
 to allow multiple files or pieces of content to be sent in a
single email
MIME standard

RFC 2045 Part One: Format of Internet Message Bodies
 fundamental concepts and structure

RFC 2046 Part Two: Media Types
 MIME media types and subtypes, media types with standard
encoding

RFC2047 Part Three: Message Header Extensions for NonASCII Text
 How to modify RFC 822 headers to carry non-ASCII text.

RFC 2048 Part Four: Registration Procedures
 How to register additional media types for use with MIME.

RFC 2049 Part Five: Conformance Criteria and Examples
 additional implementation information and examples MIME
usage
Solutions

Encode binary data (attachment) into
ASCII characters before sending
 To email systems that messages travels
through, it is just text.
At receiving end, message is decoded back
into original file
 Your mail client automates encoding and
decoding

Encoding vs encryption
Encoding: to allow some information to be
stored in, or pass through, a medium that
can't handle the data directly.
 Encryption: prevent unauthorized persons
from view or using some information.
 It's possible for a message to use both
encoding and encryption.
 Encoding schemes used in email:

 Uuencode, MIME, Base64, Quoted-Printable,
 Binhex and yEnc
Base64

Base64: a standard method for converting 8-bit
binary information into a limited subset of ASCII
characters
 for safe transport through e-mail systems, and other
systems that are not 8-bit safe.

With OpenSSL, it is very easy to encode and
decode Base64 data:
$ openssl enc -base64 -in myfile -out myfile.b64
$ openssl enc -d -base64 -in myfile.b64 -out myfile.decrypt

Base64 results in a transmitted message about
37% larger than original
A comment indicating this is a bash script
#!/bin/bash
{
sleep 5; echo 'HELO storm.cis.fordham.edu';
sleep 3; echo 'MAIL FROM:[email protected]';
sleep 3; echo 'RCPT TO: [email protected]';
sleep 3; echo 'DATA';
sleep 3; echo -e 'To:[email protected]';
echo -e 'MIME-Version: 1.0';
echo -e 'Content-Type: application/msword';
echo -e 'Content-Transfer-Encoding: base64\n\n';
openssl enc -base64 -in lab2.doc -out lab2.b64
cat lab2.b64;
rm lab2.b64;
echo '.';
sleep 10;
echo ‘QUIT’;
Pipeline: Feed output of command before it to
} | telnet localhost 25
the input of command after it
{
sleep 5; echo 'HELO
storm.cis.fordham.edu';
sleep 3; echo 'MAIL
FROM:[email protected]';
sleep 3; echo 'RCPT TO:
[email protected]';
sleep 3; echo 'DATA';
echo -e
'To:[email protected]'
echo -e 'Subject: an email with
attachment'
echo -e 'MIME-Version: 1.0'
echo -e 'Content-Type:
multipart/mixed;boundary="0__=0AB
BF2A0DFE3F5118f9e8a93df938690
918c0ABBF2A0DFE3F511"'
echo -e ' '
echo -e 'This is a multipart message
in MIME format.'
echo -e ' '
echo -e 'Content-Type: text/plain'
echo -e 'Content-Disposition:
inline';
echo -e 'this is the body text'
echo -e ' '
echo -e '-0__=0ABBF2A0DFE3F5118f9e8a
93df938690918c0ABBF2A0DFE3
F511'
echo -e 'Content-Type:
application/msword;'
echo -e 'Content-Disposition:
attachment;';
openssl enc -base64 -in lab2.doc out lab2.b64
cat lab2.b64;
echo '.';
sleep 10;
echo 'QUIT';
} | tee log | telnet localhost 25
Return-Path: <[email protected]>
Received: from blu0-omc2-s14.blu0.hotmail.com (blu0-omc2-s14.blu0.hotmail.com [65.55.111.89])
by storm.cis.fordham.edu (8.14.5/8.14.5) with ESMTP id q1NJVSCk032029
for <[email protected]>; Thu, 23 Feb 2012 14:31:28 -0500
Received: from BLU134-W6 ([65.55.111.73]) by blu0-omc2-s14.blu0.hotmail.com with Microsoft
SMTPSVC(6.0.3790.4675); Thu, 23 Feb 2012 11:28:56 -0800
Message-ID: <[email protected]>
Content-Type: multipart/alternative;
boundary="_f696b440-0269-4045-973d-50532f13e92b_"
X-Originating-IP: [117.192.227.74]
GEOBYTES: Geo IP
From: XXXX<[email protected]>
Service
To: <[email protected]>
Subject: XXXXX!
Date: Fri, 24 Feb 2012 00:58:57 +0530
Importance: Normal
In-Reply-To: [email protected]
…
MIME-Version: 1.0
X-OriginalArrivalTime: 23 Feb 2012 19:28:56.0616 (UTC) FILETIME=[628E8A80:01CCF261]
Parts/Attachments:
1 OK ~19 lines Text (charset: ISO-8859-1)
2 Shown ~41 lines Text (charset: ISO-8859-1)
----------------------------------------
An email (anonymized ) I received, all MIME headers are shown
Location
Outline
Introduction to basic Unix commands, bash
scripting
 Sending attachment in email via telnet
 DNS lookup tool: dig
 Review with a focus on

 Transport layer and application layer
 Understanding socket
 A sample client/server in JAVA
Dig fun
nslookup:
 dig: command-line tool for querying DNS
name servers

 for information about host addresses, mail
exchanges, name servers, and related
information
[zhang@storm ~]$ dig www.google.com
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.2.rc1.fc16 <<>> www.google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 63471
;; flags: qr rd ra; QUERY: 1, ANSWER: 7, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;www.google.com.
IN
;; ANSWER SECTION:
www.google.com.
24176 IN
www.l.google.com.
135 IN
www.l.google.com.
135 IN
www.l.google.com.
135 IN
www.l.google.com.
135 IN
www.l.google.com.
135 IN
www.l.google.com.
135 IN
A
CNAME www.l.google.com.
A
74.125.115.103
A
74.125.115.104
A
74.125.115.106
A
74.125.115.147
A
74.125.115.99
A
74.125.115.105
;; Query time: 1 msec
;; SERVER: 150.108.4.11#53(150.108.4.11)
;; WHEN: Tue Mar 6 15:01:50 2012
;; MSG SIZE rcvd: 148
dig query examples

Get address(es) for yahoo.com
 dig yahoo.com A +noall +answer

get a list of yahoo's mail servers
 dig yahoo.com MX +noall +answer

get a list of DNS servers authoritative for
yahoo.com
 dig yahoo.com NS +noall +answer

get all of the above

 dig yahoo.com ANY +noall +answer
query a specified DNS server
 dig @ns1.google.com www.google.com
Reverse DNS lookup

Use -x option to lookup hostname
associated with an IP address.
$ dig -x 204.152.184.167 +short
 to map the names in a given subnet:
#!/bin/bash
NET=18.7.22
for n in $(seq 1 254);
do
ADDR=${NET}.${n}
echo -e "${ADDR}\t$(dig -x ${ADDR} +short)"
done
Outline
Introduction to basic Unix commands, bash
scripting
 Sending attachment in email via telnet
 DNS lookup tool: dig
 Review/Summary with a focus on

 Transport layer and application layer
 Understanding socket
 A sample client/server in JAVA
Internet protocol stack

application: supporting network
applications
 FTP, SMTP, POP, HTTP, POP, DNS,
DHCP, MIME, telnet, skype, …

transport: process-process data
transfer
 TCP, UDP

network: routing of datagrams from
source to destination
 IP, routing protocols, ICMP

link: data transfer between
neighboring network elements
 Ethernet, 802.111 (WiFi), PPP

physical: bits “on the wire”
application
transport
network
link
physical
Encapsulation
source
message
segment
M
Ht
M
datagram Hn Ht
M
frame Hl Hn Ht
M
application
transport
network
link
physical
link
physical
switch
destination
M
Ht
M
Hn Ht
Hl Hn Ht
M
M
application
transport
network
link
physical
Hn Ht
Hl Hn Ht
M
M
network
link
physical
Hn Ht
M
router
Network layer


IP address
 Assigned to network
interface, not host
 A regular desktop with
multiple interface (each
connected to a subnet)
can act as router
Router examines header
fields in all IP datagrams
passing through it
 Packet forwarding based on
destination address & routing
table
application
transport
network
data link
physical
network
data link
physical
network
data link
physical
network
data link
physical
network
data link
physical
network
data link
physical
network
network
data link
data link
physical
physical
network
data link
physical
network
data link
physical
network
data link
physical
network
data link
physical
application
transport
network
data link
physical
At IP layer, receiving host…
IP layer needs to deliver/
demultiplex datagram to
appropriate upper layer
protocol
Based on upper layer protocol
IANA maintains registered
protocol numbers
 Unix host stores protocols
numbers in /etc/protocols
32 bits
head. type of
length
service
len
fragment
16-bit identifier flgs
offset
time to upper
header
layer
live
checksum
ver
IP header
32 bit source IP address
32 bit destination IP address
Options (if any)
data
(variable length,
typically a TCP IP payload
or UDP segment)
At transport layer, receiving host…

TCP or UDP: which upper
layer to deliver/demultiplex
message to?
 Source port #, dest port # in
TCP/UDP header
 source IP address, dest IP
address in IP header

host uses IP addresses & port
numbers to direct segment to
appropriate application that
has a socket bind to the port
 Next: TCP demultiplexing
 UDP demultiplexing
32 bits
source port #
dest port #
other header fields
application
data
(message)
TCP/UDP segment format
At TCP, receiving host…

TCP layer uses 4-tuple to direct segment to socket
(a door to application)





source IP address
source port number
dest IP address
dest port number
For example: web server simultaneously serves
multiple client requests
 non-persistent HTTP will have different socket for each
request
Socket API


introduced in BSD4.1 UNIX, 1981
Socket created, used, released by apps
 Support client/server application paradigm

two types of transport service via socket API:
 unreliable datagram: UDP
 reliable, byte stream-oriented: TCP
socket
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 (remote or local)
Application 2-47
Sockets: networking API

socket: a “door”/”mailbox”
between application process and
transport protocol (UCP or TCP)
 sending process shoves
message out door
• relies on transport
infrastructure (including all
lower layers) to deliver
message to receiver
 Receiving process opens door
to receive message
• relies on transport
infrastructure to receive and
direct message to the door
host or
server
host or
server
process
controlled by
app
developer
process
socket
socket
TCP with
buffers,
variables
TCP with
buffers,
variables
Interne
t
controlled
by OS
Discussion: How is socket similar to
mailbox in post mail system?
Socket-programming using TCP
TCP: reliable transfer of bytes from one process to
another
controlled by
application
developer
controlled by
operating
system
process
process
socket
TCP with
buffers,
variables
socket
TCP with
buffers,
variables
host or
server
controlled by
application
developer
controlled by
operating
system
internet
host or
server
application viewpoint
TCP provides reliable, in-order
transfer of bytes (“pipe”)
between client and server
Application 2-49
Client/Server programming with
TCP

TCP: connection-oriented
 both sides maintain info
• congestion window size, last acked seq #,..

How to set up TCP connection:
 server process must first be running:
server create socket (door) that welcomes
client’s contact, bind to a well-known port #
 Client contact server to establish a
connection: create a TCP socket while
specifying IP address, port # of server
process
 Behind the scean: TCP three-way handshake
(see TCP protocol for details)
Application 2-50
TCP Three way handshake
Step 1: client host sends
TCP SYN segment to
server
 specifies initial seq #
 no data
Step 2: server host receives
SYN, replies with
SYNACK segment
 server allocates
buffers
 specifies server initial
seq. #
Step 3: client receives
SYNACK, replies with
ACK segment, which may
contain data
Client/server socket interaction: TCP
Server (running on hostid)
Client
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()
two sockets
at server
TCP
wait for incoming
connection request connection
connectionSocket =
welcomeSocket.accept()
read request from
connectionSocket
write reply to
connectionSocket
close
connectionSocket
setup
create socket,
connect to hostid, port=x
clientSocket =
Socket()
send request using
clientSocket
read reply from
clientSocket
close
clientSocket
Application 2-52
Demo: 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)
Application 2-53
Stream jargon


input
stream
Client
Process
process
output
stream
inFromServer

Stream: a sequence of
characters that flow into
or out of a process
input stream is attached to
some input source for the
process, e.g., keyboard,
socket, file, …
output stream is attached
to an output source, e.g.,
monitor, socket, file, …
Key: stream interface
provides an abstraction,
i.e., no matter what’s the
actual source/dest, reading
from input stream/writing
to output stream are same
outToServer

monitor
inFromUser
keyboard
input
stream
client
TCP
clientSocket
socket
to network
TCP
socket
from network
Application 2-54
Example: Java client (TCP)
import java.io.*;
import java.net.*;
class TCPClient {
create
input stream
create
clientSocket object
of type Socket,
connect to server
create
output stream
attached to socket
This package defines Socket()
and ServerSocket() classes
public static void main(String argv[]) throws Exception
{
server name,
String sentence;
e.g., erdos.dsm.fordham.edu
String modifiedSentence;
server port #
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("hostname", 6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
Application 2-55
Example: Java client (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);
close socket
clientSocket.close();
(clean up behind yourself!)
}
}
Application 2-56
Example: Java server (TCP)
import java.io.*;
import java.net.*;
class TCPServer {
create
welcoming socket
at port 6789
wait, on welcoming
socket accept() method
for client contact create,
new socket on return
create input
stream, attached
to socket
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()));
Application 2-57
Example: Java server (TCP), cont
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);
}
}
}
end of while loop,
loop back and wait for
another client connection
Application 2-58
TCP Socket: summary


Application layer: use transport protocol service
via socket API (TCP socket, or UDP socket)
TCP socket: identified by (srcIP, srcPort#,
destIP, dstPort#)
 Server: a welcome socket for accepting connection
(on a well-know port #)
 Client: connect to server’s welcome socket
• client port # is dynamically assigned by TCP layer
(ensures same port # is not assigned to two
processes, reserved port #s are not assigned)
 A host can have multiple TCP connections with a single
web servers
Discussion: “fake” web proxy

Modify TCPServer.java to display message received to
standard output
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
System.out.println(clientSentence);



Set your browser to use TCPServer as proxy
You will be able to see what requests are generated by
web browser!
To be a real proxy, needs to incorporate TCPClient in
order to make request to web server
At transport layer, receiving host…

TCP or UDP: which upper
layer to deliver/demultiplex
message to?
 Source port #, dest port # in
TCP/UDP header
 source IP address, dest IP
address in IP header

host uses IP addresses & port
numbers to direct segment to
appropriate application that
has a socket bind to the port
 TCP demultiplexing
 NOW: UDP demultiplexing
32 bits
source port #
dest port #
other header fields
application
data
(message)
TCP/UDP segment format
At UDP, receiving host

UDP provides unreliable transfer of groups of bytes
(“datagrams”) between client and server
 data may be received out of order, or lost



UDP: connection-less protocol
 no handshaking to establish connection
 No states stored at sender/receiver
Sender: attaches IP address and port of dest to each
packet
Destination demultiplex:
 Direct packet to application/socket based on 2-tuple: IP addr,
port #
 How to send response?
• Sender IP address, port of sender can be extracted from IP/UDP
header
Client/server socket interaction: UDP
Server (running on hostid)
create socket,
port= x.
serverSocket =
DatagramSocket()
read datagram from
serverSocket
write reply to
serverSocket
specifying
client address,
port number
Client
create socket,
clientSocket =
DatagramSocket()
Create datagram with server IP and
port=x; send datagram via
clientSocket
read datagram from
clientSocket
close
clientSocket
Q: what if client is started first? Will the packet be lost?
Example: Java client (UDP)
input
stream
Client
Process
monitor
inFromUser
keyboard
Input: receives
process
packet (recall
thatTCP received
“byte stream”)
UDP
packet
receivePacket
packet (recall
that TCP sent “byte
stream”)
sendPacket
Output: sends
UDP
packet
client
UDP
clientSocket
socket
to network
UDP
socket
from network
Example: Java client (UDP)
import java.io.*;
import java.net.*;
class UDPClient {
public static void main(String args[]) throws Exception
{
create
input stream
create
client socket
translate
hostname to IP
address using DNS
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();
Example: Java client (UDP), cont.
create datagram
with data-to-send,
length, IP addr, port
send datagram
to server
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
read datagram
from server
clientSocket.receive(receivePacket);
String modifiedSentence =
new String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}
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];
create space for
received datagram
receive
datagram
while(true)
{
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
Example: Java server (UDP), cont
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);
}
}
}
end of while loop,
loop back and wait for
another datagram
Socket: summary

Transport protocol: end-to-end communication
between hosts in Internet
 TCP: a reliable byte stream
 UDP: a best-effort datagram service

Application layer: use transport protocol service
via socket API
 TCP socket: identified by (srcIP, srcPort#, destIP,
dstPort#)
• Server: a welcome socket for accepting connection
• Client: connect to server’s welcome socket
 UDP socket identified by (IP, port#)
Socket: summary

We know host name of server/host we want to
communicate
 DNS Lookup provides its IP addr

How about port # to connect to (for TCP), or send
pkt to (for UDP)?
 1-1024: registered ports, IANA maintains a registry of
services that use these port number
•
•
•
•
E.g., HTTP service: TCP port 80,
This means web server’s welcome socket is at port 80
SMTP server: TCP port 25
DHCP server: UDP port 67
 1024 above: unregistered ports
• Free to use
Usage of port number
Unix system: defines port numbers in
/etc/services (based on IANA services
version: last updated 2011-06-10)
 To find port number used by telnet:

[zhang@storm ~]$ grep telnet /etc/services
telnet
23/tcp
telnet
23/udp
rtelnet
107/tcp
# Remote Telnet
…
Summary
Introduction to basic Unix commands, bash
scripting
 Sending attachment in email via telnet
 DNS lookup tool: dig
 Review with a focus on

 Transport layer and application layer
 Understanding socket
 A sample client/server in JAVA

Next assignment:
 Practice with simple bash scripting