midterm review part of the slides are adapted from computer networking: a top down approach jim...

72
Midterm Review Part of the slides are adapted from Computer Networking: A Top Down Approach Jim Kurose, Keith Ross Addison-Wesley, April 2009.

Upload: mercy-sharleen-walsh

Post on 31-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Midterm Review

Part of the slides are adapted from Computer Networking: A Top Down Approach Jim Kurose, Keith RossAddison-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 line3. From command line, extracts command name and

arguments4. 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

• 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

• Easier for protection• Organized files

Hierarchical file system

9

/ (root)

home

staff

bin

zhang

etc

passwd

dev

cdrom tty24

lib

Absolute pathname, path

10

/ (root)

home

staff

bin

zhang

etc

passwd

dev

cdrom tty24

lib

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?

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)

home

staff

bin

zhang

etc

passwd

dev

cdrom tty24

lib

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 directoryls –l: long listing, [zhang@storm CISC1600]$ ls -ltotal 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.pptdrwxr-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) cat [option] … [file] … cat proj1.cpp 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

[ ] means the argument is optional… means there can be multiple arguments of this type

The most important command !!!

man: displaying online manuals Press q to quit, space to scroll down, arrow keys to

roll up/down

man ls

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 rm –i : confirm with user before removing files

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/bashdatewhoecho “Hello world!”

Make file executable by owner (You) using commandchmod 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 8-bit 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

Non-ASCII 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

#!/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’;} | telnet localhost 25

A comment indicating this is a bash script

Pipeline: Feed output of command before it to 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__=0ABBF2A0DFE3F5118f9e8a93df938690918c0ABBF2A0DFE3F511"'

echo -e ' ' echo -e 'This is a multipart message in

MIME format.' echo -e ' ' echo -e '--

0__=0ABBF2A0DFE3F5118f9e8a93df938690918c0ABBF2A0DFE3F511'

echo -e 'Content-Type: text/plain' echo -e 'Content-Disposition:

inline'; echo -e 'this is the body text' echo -e ' ' echo -e '--

0__=0ABBF2A0DFE3F5118f9e8a93df938690918c0ABBF2A0DFE3F511'

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 -0500Received: 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 -0800Message-ID: <[email protected]>Content-Type: multipart/alternative; boundary="_f696b440-0269-4045-973d-50532f13e92b_"X-Originating-IP: [117.192.227.74]From: XXXX<[email protected]>To: <[email protected]>Subject: XXXXX!Date: Fri, 24 Feb 2012 00:58:57 +0530Importance: NormalIn-Reply-To: [email protected]…MIME-Version: 1.0X-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)----------------------------------------

GEOBYTES: Geo IP Location Service

An email (anonymized ) I received, all MIME headers are shown

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 A

;; ANSWER SECTION:www.google.com. 24176 IN CNAME www.l.google.com.www.l.google.com. 135 IN A 74.125.115.103www.l.google.com. 135 IN A 74.125.115.104www.l.google.com. 135 IN A 74.125.115.106www.l.google.com. 135 IN A 74.125.115.147www.l.google.com. 135 IN A 74.125.115.99www.l.google.com. 135 IN 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

source

application

transportnetwork

linkphysical

HtHn M

segment Ht

datagram

destination

application

transportnetwork

linkphysical

HtHnHl M

HtHn M

Ht M

M

networklink

physical

linkphysical

HtHnHl M

HtHn M

HtHn M

HtHnHl M

router

switch

Encapsulationmessage M

Ht M

Hn

frame

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

transportnetworkdata linkphysical

application

transportnetworkdata linkphysical

networkdata linkphysical network

data linkphysical

networkdata linkphysical

networkdata linkphysical

networkdata linkphysical

networkdata linkphysical

networkdata linkphysical

networkdata linkphysical

networkdata linkphysical

networkdata linkphysicalnetwork

data linkphysical

At IP layer, receiving host…

ver length

32 bits

data (variable length,typically a TCP

or UDP segment)

16-bit identifier

header checksum

time tolive

32 bit source IP address

head.len

type ofservice

flgsfragment

offsetupper layer

32 bit destination IP address

Options (if any)

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

IP header

IP payload

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

source port # dest port #

32 bits

applicationdata (message)

other header fields

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

a host-local, application-created, OS-controlled interface (a “door”) into whichapplication process can both send and receive messages to/from another application process (remote or local)

socket

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

process

TCP withbuffers,variables

socket

host orserver

process

TCP withbuffers,variables

socket

host orserver

Internet

controlledby OS

controlled byapp developer

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

process

TCP withbuffers,

variables

socket

controlled byapplicationdeveloper

controlled byoperating

system

host orserver

process

TCP withbuffers,

variables

socket

controlled byapplicationdeveloper

controlled byoperatingsystem

host orserver

internet

Application 2-49

TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server

application viewpoint

Client/Server programming with TCP

Application 2-50

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)

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

closeconnectionSocket

read reply fromclientSocket

closeclientSocket

Client/server socket interaction: TCP

wait for incomingconnection requestconnectionSocket =welcomeSocket.accept()

create socket,port=x, forincoming request:welcomeSocket =

ServerSocket()

create socket,connect to hostid, port=xclientSocket =

Socket()

Server (running on hostid) Client

send request usingclientSocketread request from

connectionSocket

write reply toconnectionSocket

TCP connection setup

Application 2-52

two socketsat server

Demo: example client-server app1) client reads line from standard input

(inFromUser stream) , sends to server via socket (outToServer stream)

2) server reads line from socket3) server converts line to uppercase, sends back

to client4) client reads, prints modified line from socket

(inFromServer stream)

Application 2-53

outT

oSer

ver

to network from network

inFr

omS

erve

r

inFr

omU

ser

keyboard monitor

Process

clientSocket

inputstream

inputstream

outputstream

TCPsocket

Clientprocess

client TCP socket

Stream jargon

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

Application 2-54

Example: Java client (TCP)

import java.io.*; import java.net.*; class TCPClient {

public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence;

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

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

DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

createinput stream

create clientSocket object

of type Socket, connect to server

createoutput stream

attached to socket

Application 2-55

This package defines Socket() and ServerSocket() classes

server port #

server name,e.g., erdos.dsm.fordham.edu

Example: Java client (TCP), cont.

BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

sentence = inFromUser.readLine();

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

modifiedSentence = inFromServer.readLine();

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

clientSocket.close(); } }

createinput stream

attached to socket

send lineto server

read linefrom server

Application 2-56

close socket(clean up behind yourself!)

Example: Java server (TCP)

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

class TCPServer {

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()));

wait, on welcomingsocket accept() method

for client contact create, new socket on return

Application 2-57

createwelcoming socket

at port 6789

create inputstream, attached

to socket

Example: Java server (TCP), cont

DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());

clientSentence = inFromClient.readLine();

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

outToClient.writeBytes(capitalizedSentence); } } }

read in linefrom socket

create outputstream,

attached to socket

write out lineto socket

end of while loop,loop back and wait foranother 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

source port # dest port #

32 bits

applicationdata (message)

other header fields

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)

closeclientSocket

read datagram fromclientSocket

create socket,clientSocket = DatagramSocket()

Client

Create datagram with server IP andport=x; send datagram via clientSocket

create socket,port= x.serverSocket = DatagramSocket()

read datagram fromserverSocket

write reply toserverSocketspecifying client address,port number

Q: what if client is started first? Will the packet be lost?

Example: Java client (UDP)

sendP

ack

et

to network from network

rece

iveP

ack

et

inF

rom

Use

r

keyboard monitor

Process

clientSocket

UDPpacket

inputstream

UDPpacket

UDPsocket

Output: sends packet (recallthat TCP sent “byte stream”)

Input: receives packet (recall thatTCP received “byte stream”)

Clientprocess

client UDP socket

Example: Java client (UDP)

import java.io.*; import java.net.*; 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();

createinput stream

create client socket

translate hostname to IP

address using DNS

Example: Java client (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(); } }

create datagram with data-to-send,

length, IP addr, port

send datagramto server

read datagramfrom server

Example: Java server (UDP)

import java.io.*; import java.net.*; 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) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket);

createdatagram socket

at port 9876

create space forreceived datagram

receivedatagra

m

Example: Java server (UDP), cont

String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase();

sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } } }

get IP addrport #, of

sender

write out datagramto socket

end of while loop,loop back and wait foranother datagram

create datagramto send to client

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/servicestelnet 23/tcptelnet 23/udprtelnet 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