212 f network programming and design tcp/ip 5 march 2009

124
212 F Network Programming and Design TCP/IP 5 March 2009 Philip Tsang

Upload: naif

Post on 22-Jan-2016

29 views

Category:

Documents


0 download

DESCRIPTION

212 F Network Programming and Design TCP/IP 5 March 2009. Philip Tsang. New Learning Ecology Innovation in LT&R : 3C + C Skills, YouTube and Podcasting. Contents. C ritical Thinking. T&L. Collaborative. Communicative. Agenda for TCP/IP. Review of Quizzes of 25 /27 Feb - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 212 F  Network  Programming and Design TCP/IP  5 March 2009

212 F Network

Programming and Design TCP/IP

5 March 2009Philip Tsang

Page 2: 212 F  Network  Programming and Design TCP/IP  5 March 2009

New Learning EcologyInnovation in LT&R: 3C + C Skills, YouTube and Podcasting

Critical Thinking

Communicative

T&LT&L

Collaborative

Contents

Page 3: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Agenda for TCP/IP

• Review of Quizzes of 25 /27 Feb• Cover Key concepts in Unit 3• TCP/IP architecture & Basic concepts• Internet Layer

IP addressing, Subnet, IP Routing, Routing Table, Routing protocols, CIDR, ICMP

• Address translationARP : IP address -> MAC addressRARP : MAC address -> IP address

• Transport LayerTCP (Transmission Control Protocol)UDP (User Datagram Protocol)

Page 4: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Quiz of 25 Feb 2008• Name:________________• Student #______________• Date: 25 Feb 2008• 212F TCP/IP Self Assessment question Quiz• (1) Identify and describe two methods to handle the rapid depletion

of IP addresses. [2 marks]• (2) Your boss asks you to create 124 sub-networks with the

assigned Class B address 150.150.0.0. Answer the following questions:– What is the subnet mask? [1/2]– List the first three valid network numbers. [1/2]– List the range of host IP addresses on those three networks. [2]– List the last valid network and range of IP addresses. [2]– How many subnets does this solution allow? [2]– How many host addresses can be on each subnet? [1]

Page 5: 212 F  Network  Programming and Design TCP/IP  5 March 2009

• Answer• (a)To have 124 sub-networks, we need to borrow 7 bits from the

host field. So the subnet mask is 255.255.254.0.1• (b) 150.150.2.0, 150.150.4.0, • (c) 150.150.2.1.– 150.150.3.254; 150.150.4.1-

150.150.5.254;150.150.6.1-150.150.7.254

• [See following IP Subset Calculator screen capture]2

• d) Network: 150.150.252.0• Range of IP addresses: 150.150.252.1-150.150.253.2542

• (e)In part • the 2^N-2 equation where N =10, 2^7-2=126. 126 subnets can be

allowed.2• (f) The subnet mask 255.255.254.0 has 9 bits set to 0.• Use the 2^N-2 equation where N =6, 2^9-2=512512 host addresses

can be on each subnet.

Page 6: 212 F  Network  Programming and Design TCP/IP  5 March 2009

• (1)• The first method is using the private IP for

the local network. Since private IP can use in different local network repeatedly. So the hosts no need to assign the public IP.

• • The second method is using more bits to

represent the IP address. So the number of IP address will increase.

• (2)• a) The subnet mask used to determine what

subnet an IP address belongs to.• • b) The first three valid network numbers are

150.150.1.0, 150.150.2.0, 150.150.3.0• • c) On 150.150.1.0, the range of host IP

addresses is 150.150.1.1 - 150.150.1.254• On 150.150.2.0, the range of host IP

addresses is 150.150.2.1 - 150.150.2.254• On 150.150.3.0, the range of host IP

addresses is 150.150.3.1 - 150.150.3.254• • d) The last valid network is 150.150.254.0• Its range of host IP addresses is

150.150.254.1 – 150.150.254.254• • e) This solution allows 128 subnets.• • f) Addresses can be on each subnet are

510.

Page 7: 212 F  Network  Programming and Design TCP/IP  5 March 2009

• Subnet mask consist the number between 0~255,it allows you to identify which part of an IP address is reserved for the network, and which part is available for host use. Also it required setting in the TCP/IP control panel.

Page 8: 212 F  Network  Programming and Design TCP/IP  5 March 2009

List the first three valid network numbers. [1/2]

• Class A : Network 8 bits; (0=Reserved ;127=Loopback address) (e.g. US government , IBM)

• Class B : Network 16 bits; Range=128-191• Class C : Network 24 bits. Range=192-223

Page 9: 212 F  Network  Programming and Design TCP/IP  5 March 2009

c. List the range of host IP addresses on those three

networks. [2]• Class A: Host address is denoted by last

24 bits or last 3 octets.

• Host IP configuration for class A can be shown as -

• network.host.host.host

Page 10: 212 F  Network  Programming and Design TCP/IP  5 March 2009

• Class B: Host address is denoted by last 16 bits or last 2 octets.

• Host IP configuration for class B can be shown as -

• network.network.host.host

Page 11: 212 F  Network  Programming and Design TCP/IP  5 March 2009

• Class C : Host address address is denoted by last 8 bits or last octet.

• Host IP configuration for class C can be shown as -

• network.network.network.host

Page 12: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Quiz of 27 Feb 2008

• Repeat your answers of Quiz of 25 Feb through a multimedia presentation

Page 13: 212 F  Network  Programming and Design TCP/IP  5 March 2009
Page 14: 212 F  Network  Programming and Design TCP/IP  5 March 2009

But How?

Page 15: 212 F  Network  Programming and Design TCP/IP  5 March 2009

• Describe the concept of the Internet architecture• Identify the different roles of IP, TCP and UDP in

Internet communication• Describe the features of the IP• Discuss IP naming and address translation

mechanisms• Demonstrate an understanding of IP routing• Compare connection-oriented and connectionless

communication• Discuss TCP protocol mechanism• Discuss UDP protocol mechanism• Analyze the benefits & limitations of TCP and UDP

Objective of Unit 3

Page 16: 212 F  Network  Programming and Design TCP/IP  5 March 2009

5 questions for communication between 2 s/w (eg. Client & server) on 2 different machines

1. How to identify a machine ??2. How to identity a s/w running in a machine ??3. How to transfer data from one machine to

another ??4. How to ensure all data are correctly received

by destination s/w ??5. What to do if there is network congestion due

to :• Transmitting host sends out data too fast??• receiving capability of destination machine is not enough

(eg. Less processing power, memory, NIC speed, etc) ??

Page 17: 212 F  Network  Programming and Design TCP/IP  5 March 2009

~~Architecture & basic concept~~

Page 18: 212 F  Network  Programming and Design TCP/IP  5 March 2009

INTERNET (TCP/IP) architecture

Page 19: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Basic concepts (1)• Encapsulation & de-capsulation • De-multiplexing• IP address

uniquely identify a interface/host/subnet/network in INTERNET (more discussion in later section)

• TCP port & UDP port are differentEach application within a host is uniquely identified by a

port number; 16 bit; reserved well-known port : 1 ~ 1023

• Socket one end of each communication connectionUniquely identify which host, which application IP address + port number + protocol type (tcp or udp)

Page 20: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Basic concepts (2)

• Connectionidentified by a pair of sockets on both ends

of connection. (protocol type, source IP address, source port number, destination IP address, destination port number)

• MTU (Maximum Transmission Unit)Upper limit on size of data portion of

network access layer, excluding header and trailer

Different Network access (link) layer technologies have different MTU.

Page 21: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Encapsulation & De-capsulation

Page 22: 212 F  Network  Programming and Design TCP/IP  5 March 2009

De-multiplexing

Page 23: 212 F  Network  Programming and Design TCP/IP  5 March 2009

5 questions for communication between 2 s/w (eg. client & server) on 2 different machines

1. How to identify a machine IP address

2. How to identity a s/w running in a machine (IP address + transport layer protocol type + port number )

3. How to transfer data from one machine to another??

4. How to ensure all data are correctly received by destination s/w ??

5. What to do if there is network congestion due to : Transmitting host sends out data too fast; receiving capability of destination machine is not

enough (eg. Less processing power, memory, NIC speed, etc) ??

Page 24: 212 F  Network  Programming and Design TCP/IP  5 March 2009

~~~~~~~~ Internet Layer ~~~~~~~~~•IP address•IP subnet & CIDR•IP routing•IP fragmentation & reassembly•ICMP & ARP

Page 25: 212 F  Network  Programming and Design TCP/IP  5 March 2009

IP packet format

Page 26: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Functions of internet layer• Functions of internet layer :

1. Provide unique identification (i.e. logical IP address) to an interface of INTERNET node (eg. Host, router) or network/subnet Classful Classless (CIDR)

2. Packet forwarding (or routing)

3. Fragmentation

4. Reassembly

Page 27: 212 F  Network  Programming and Design TCP/IP  5 March 2009

IP Address (1)• IP-v4 classful address :

32 bit long, four 8-bit fields, separated by period2 formats :

Decimal : 192.123.44.23 (note each field between period is 0 ~ 255), more convenient

Binary : 11000000.01111011.00110100.00010111Composed of 2 portions – network portion, host

portionAll hosts of same network must have same value on

network portionBased on first 4 bits (MSB) of IP address, it is

classified into 5 classes : class A through E. Only Class A through C can be assigned to a machine/host.

Page 28: 212 F  Network  Programming and Design TCP/IP  5 March 2009

IP Address (2)

Page 29: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Class attendance quiz

• Illustrate with steps to identify/show the max value of the first decimal number of Class C address is 223.

•Send email to: [email protected]

• Subject: 212f Quiz> Your name + Your SN

Page 30: 212 F  Network  Programming and Design TCP/IP  5 March 2009

IP Address (3)

• Network mask is used to specify which part of an IP address is network-portion.

• Consecutive bit of value “1” specify corresponding bit as network portion

Class (Default) network mask

A 255.0.0.0

B 255.255.0.0

C 255.255.255.0

Page 31: 212 F  Network  Programming and Design TCP/IP  5 March 2009

IP Address (4)Class Network

portionHost

portionnumber of

usable

network

number of usable hosts per

network

A 0~127 0.0.1~

255.255.254

126 224-2

B 128.0~

191.255

0.1~

255.254

214 216-2

C 192.0.0~

223.255.255

1~254 221 28-2

D 224.0.0.0~239.255.255.25

5

N/A multicast N/A

E 240.0.0.0~255.255.255.25

5

N/A experiment N/A

Page 32: 212 F  Network  Programming and Design TCP/IP  5 March 2009

IP Subnet (1)• What is sub-netting?

Divide a large network into a number of smaller subnets Each subnet is also a network, a broadcast domain. Subnets are

connected by router (not hub/LAN switch)

• How to sub-net? Network mask changes extends continuous ones towards right to host portion. The

extension part is called subnet portion. The network mask is now called subnet mask.

• Each IP address now consists of 3 portions Network, subnet, host portion

• Why sub-netting : Better utilization of IP addressing space Reduces network congestion by reducing broadcast domain Reduce number of hosts per subnet => better performance in

Ethernet

Page 33: 212 F  Network  Programming and Design TCP/IP  5 March 2009

IP Subnet (2)

Page 34: 212 F  Network  Programming and Design TCP/IP  5 March 2009

IP Subnet (3)

Page 35: 212 F  Network  Programming and Design TCP/IP  5 March 2009

IP Subnet (4)

• Scenario 1 :• Class A network : 12.0.0.0

default network mask = 255.0.0.0Number of networks = 1Number of hosts per network = 2 24 -2

subnet mask=255.255.0.0Number of subnets = 256 –2 = 254Number of hosts per network = 2 16 –2

Effect : one class A network increases from 1 to 254 subnets but the number of hosts per subnet decreases from (2 24 –2) to (2 16 –2)

Page 36: 212 F  Network  Programming and Design TCP/IP  5 March 2009

IP Subnet (5)

• Scenario 2 :• Class B network : 158.182.0.0

default network mask = 255.255.0.0Number of network = 1Number of hosts per network = 2 16 -2

subnet mask=255.255.240.0Number of network = 24 – 2 = 14Number of hosts per network = 2 12 – 2 = 4094

Effect : one class B network increases from 1 to 14 subnets but the number of hosts per subnet decreases from (2 16 –2) to 4094

Page 37: 212 F  Network  Programming and Design TCP/IP  5 March 2009

IP Subnet (5)

• Scenario 3 :• Class C network : 192.182.0.0(?)

default network mask = 255.255.255.0Number of network = 1Number of hosts per network = 2 8 -2

subnet mask=255.255.255.240Number of network = 24 – 2 = 14Number of hosts per network = 2 12 – 2 = 4094

Effect : one class B network increases from 1 to 14 subnets but the number of hosts per subnet decreases from (2 16 –2) to 4094

Page 38: 212 F  Network  Programming and Design TCP/IP  5 March 2009

IP Subnet Quiz

• Scenario 3 :• Class C network :

default network mask = XXXNumber of network = XNumber of hosts per network = XXX

subnet mask=XXXXXXNumber of network = XXXXNumber of hosts per network = XXX

Effect : one class B network increases from M to n subnets but the number of hosts per subnet decreases from N

Send answer to: [email protected] 212F Subnet Quiz>Your Name +SN

Page 39: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Online IP Calculatorhttp://www.subnet-calculator.com/

Page 40: 212 F  Network  Programming and Design TCP/IP  5 March 2009

IP Routing principle• Unreliable; hop-by-hop routing

Each hop (router) only provide IP address of next best hop (router) to destination network/subnet

• Routing decision is made based on destination IP address of incoming packet to be routed and metrics for the path

• Each router/host has a routing table, from which routing decision is made

• Contents of routing table can be : Dynamic, learned from routing protocols (eg. RIP, OSPF, IGRP,

EIGRP) Default route by hard-coded Static by hard-coded Directly connected network/subnet

Page 41: 212 F  Network  Programming and Design TCP/IP  5 March 2009

IP Routing table• Each entry of routing table must contain at least

following columns : IP address of destination network/subnet IP address of next hop (router) to destination

Note next hop is sometimes called gatewaymetric for that routeOutgoing interface to next hop

• Default gateway :Definition : When there is no entry in routing table matching

destination IP address, entry of default gateway will be used. Destination is represented by 0.0.0.0/0.0.0.0

Host with single interface : simplify by using default gateway entry

One more usage of default gateway. What is it?

Page 42: 212 F  Network  Programming and Design TCP/IP  5 March 2009

IP default gateway

Page 43: 212 F  Network  Programming and Design TCP/IP  5 March 2009

ARP (Address Resolution Protocol)

• Function : Get MAC address of other TCP/IP host, on the same segment, from IP address of that host(i.e. IP address -> MAC address)

Page 44: 212 F  Network  Programming and Design TCP/IP  5 March 2009

IP V6• Previous discussion is for IPv4.• IPv6 is compatible with IPv4 with following

enhancement:1. IP address : 32 bits -> 128 bits => more IP available2. Support QoS for timing critical application such as Voice

over IP, video conference, etc.3. Support security extension for authenticating source,

providing integrity & encryption.4. Header processing is speed up to increase performance5. Addition of anycast address, which allows messages to be

sent to any of several possible service providers or to any of a related group of remote destinations, with the idea that any one of them can manage the forwarding of the packet to others.

• More : http://www.ipv6.org

Page 45: 212 F  Network  Programming and Design TCP/IP  5 March 2009

IP - CIDR (1)

• Classless InterDomain Routing (CIDR)

• No more class boundary or subnet on IP address

• Combine multiple continuous smaller subnets (eg. Class C) into a larger supernet or CIDR block

• Representation of CIDR network : prefix + length

Eg. 158.182.4.40 /12

Page 46: 212 F  Network  Programming and Design TCP/IP  5 March 2009

ICMP• ICMP (Internet Control Message Protocol) :• Network control and error reporting

test node reachability across an internetwork with echo and reply messages (eg. Ping)

stimulate more efficient routing with redirect messages (eg. ICMP redirect)

“time exceeded” messages to inform sources that a datagram has exceeded its allocated time to exist within the internetwork (eg. Traceroute, tracert)

Router advertisement and router solicitation messages

Page 47: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Labs and discussion of Quizzes

• Remember to submit your Quiz to [email protected]

•So far only half of your have submitted it.

Page 48: 212 F  Network  Programming and Design TCP/IP  5 March 2009

5 questions for communication between 2 s/w (eg. Client & server) on 2 different machines

1.How to identify a machine IP address2.How to identity a s/w running in a machine IP address + transport layer protocol type + port number3.How to transfer data from one machine to another routing table + IP layer4.How to ensure all data are correctly received by destination s/w ??5.What to do if there is network congestion due to :

Transmitting host sends out data too fast; receiving capability of destination machine is not enough

(eg. Less processing power, memory, NIC speed, etc) ??

Page 49: 212 F  Network  Programming and Design TCP/IP  5 March 2009

212 F Network

Programming and Design TCP/IP of 5-10 March 08

Philip Tsang

Page 50: 212 F  Network  Programming and Design TCP/IP  5 March 2009

~~~~~ Transport Layer ~~~~~

TCP : connection-oriented

UDP : connectionless

Page 51: 212 F  Network  Programming and Design TCP/IP  5 March 2009

TCP segment format

Page 52: 212 F  Network  Programming and Design TCP/IP  5 March 2009

TCP – basics (1)• Connection-oriented; full-duplex • stream data transfer - segmenting data from

application• Data transfer involves 3 steps :

Connection establishment (3-way handshake) Actual data transfer Connection termination (4-way)

• Some terms:sequence number – identify each byte of segment dataacknowledge number – sequence number of next byte of

data sender expects to receive delayed acknowledge – acknowledgement does not

respond immediately for each received segment, wait until multiple segments are received => save bandwidth and processing power

Page 53: 212 F  Network  Programming and Design TCP/IP  5 March 2009

TCP – basics (2)• Reliability is provided by :

Sequence number, Ack. number, delayed ack., Timeout & Retransmission, discard duplicate

Sender starts a timer for sending each packet.Receiver sends +ve acknowledge (ACK) some delays

after receiving packet If sender does not receive ACK for a packet when timer

expires (i.e. timeout), sender retransmits that packet– Retransmission may lead to duplicate packet. Duplicated

packet is discarded by receiver.

• Flow Control – sliding windows buffering. Buffer size is integral multiple of segment size. Sender transmits data continuously until number of segments sent without acknowledge equals length of window.

Page 54: 212 F  Network  Programming and Design TCP/IP  5 March 2009

UDP datagram format

Page 55: 212 F  Network  Programming and Design TCP/IP  5 March 2009

UDP

• User Data Protocol (UDP)ConnectionlessDiscrete datagramsUnreliable (NO Sequence & Ack.,

retransmission)High-speed, minimal overheadEg. Multimedia application, TFTP, SNMP

Page 56: 212 F  Network  Programming and Design TCP/IP  5 March 2009

5 questions for communication between 2 s/w (eg. Client & server) on 2 different machines

1.How to identify a machine IP address2.How to identity a s/w running in a machine IP address + transport layer protocol type + port number3.How to transfer data from one machine to another routing table + IP layer4.How to ensure all data are correctly received by destination s/w -> TCP reliability5.What to do if there is network congestion due to :

Transmitting host sends out data too fast; receiving capability of destination machine is not

enough (eg. Less processing power, memory, NIC speed, etc) -> TCP flow control

Page 57: 212 F  Network  Programming and Design TCP/IP  5 March 2009

~~~~ Application Layer ~~~~~•DNS•Trouble-shooting commands•BOOTP & DHCP•Telnet •FTP•SMTP

Page 58: 212 F  Network  Programming and Design TCP/IP  5 March 2009

DNS (1)• Function of DNS

Resolve domain name (eg. www.ouhk.edu.hk) into IP address

• Why DNS ? For human being, it is easier to remember domain

name of a host rather than its IP address• Distributed and hierarchical domain name server &

database• Delegation of domain authority• Each DNS server can server its own domain or lower-

level domains• Each domain must be served by at least 2 DNS servers.

Why?

Page 59: 212 F  Network  Programming and Design TCP/IP  5 March 2009

DNS (2)• Structure of domain spacing:

Hierarchical tree structureMultiple levels :

Root domain : . Top level domains :

o By functional : com, net, org, mil, edu, gov, …o By geographical : hk, ca, jp, ….

Lower levels : eg. ibm.com, novell.com, cisco.com,…

Page 60: 212 F  Network  Programming and Design TCP/IP  5 March 2009

DNS (3)

Page 61: 212 F  Network  Programming and Design TCP/IP  5 March 2009

BOOTP (BOOTstrap protocol)

• BOOTP server maintains and provides auto-configuration of clientsClient’s IP address, IP address of default

router; server’s IP address, server’s host name

Page 62: 212 F  Network  Programming and Design TCP/IP  5 March 2009

DHCP (Dynamic Host Configuration Protocol)

• Similar to BOOTP with 2 major differences:More configuration options

Client’s IP address, subnet mask, IP address of default router, IP address of DNS server, other options

Lease time for IP assignment can be finite. Allows release and reallocation of configuration information => dynamic

Note that permanent allocation is still possible

Page 63: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Telnet•  What is a Telnet Client? In client/server computing, you need to be able to

send data to and receive data from a host. To do this, a general terminal or a terminal emulator is normally used. Naturally, the terminal or the PC that is running a terminal emulator must be connected to the server or mainframe computer, because otherwise data transmission would not be possible. To connect the terminal to the server, there are various different methods that can be used. Among these methods are serial cables, modems and existing networks. For a long time, the most common network connection method was Telnet, a standard protocol for connecting two different computers. With Telnet, you can connect to the server, input commands, and run programs. Nevertheless, Telnet has one major drawback: all data is exchanged without any form of encryption.

• In a corporate environment where LAN cables and infrastructure are controlled by the owners, Telnet may well be sufficient. However, it can present a serious security risk when you re transmitting over a wide area �or using public lines. When you're using Telnet, anybody with some network knowledge and tools can monitor the data exchange, even gaining access to details like usernames and passwords. For this reason, SSH (Secure Shell) is usually a better alternative nowadays.

• http://www.emtec.com/zoc/telnet-client.html

Page 64: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Telnet

• - What is Telnet?Telnet is a user command and an underlying TCP/IP protocol for accessing remote computers. Through Telnet, an administrator or another user can access someone else's computer remotely. On the Web, HTTP and FTP protocols allow you to request specific files from remote computers, but not to actually be logged on as a user of that computer. With Telnet, you log on as a regular user with whatever privileges you may have been granted to the specific application and data on that computer.

Page 65: 212 F  Network  Programming and Design TCP/IP  5 March 2009
Page 66: 212 F  Network  Programming and Design TCP/IP  5 March 2009

telnet

• RFC 854 (RFC854)

• http://www.faqs.org/rfcs/rfc854.html

Page 67: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Additional information

• IANA - oversee allocation of port number of Internet Protocol;

• ICANN (APNIC, RIPE, ARIN) – allocation of IP address in Asia Pacific, Europe & America respectively

• INTERNIC – domain name registration

Page 68: 212 F  Network  Programming and Design TCP/IP  5 March 2009

exercises

• Select five .hk site and ‘telnet them’ to see the results.

• Write a C/shell/script to automatically telnet hosts on a file.

Page 69: 212 F  Network  Programming and Design TCP/IP  5 March 2009

FTP• File FTP is an acronym for File Transfer Protocol.

• - File Transfer Protocol (FTP), a standard Internet protocol, is the simplest way to exchange files between computers on the Internet. Like the Hypertext Transfer Protocol (HTTP), which transfers displayable Web pages and related files, and the Simple Mail Transfer Protocol (SMTP), which transfers e-mail, FTP is an application protocol that uses the Internet's TCP/IP protocols. FTP is commonly used to transfer Web page files from their creator to the computer that acts as their server for everyone on the Internet. It's also commonly used to download programs and other files to your computer from other servers.

• As a user, you can use FTP with a simple command line interface (for example, from the Windows MS-DOS Prompt window) or with a commercial program that offers a graphical user interface. Your Web browser can also make FTP requests to download programs you select from a Web page. Using FTP, you can also update (delete, rename, move, and copy) files at a server. You need to logon to an FTP server. However, publicly available files are easily accessed using anonymous FTP.

• Basic FTP support is usually provided as part of a suite of programs that come with TCP/IP. However, any FTP client program with a graphical user interface usually must be downloaded

• from the company that makes it. • http://www.kb.indiana.edu/data/aerg.html

Page 70: 212 F  Network  Programming and Design TCP/IP  5 March 2009

http://www.w3.org/Protocols/rfc959/

Page 71: 212 F  Network  Programming and Design TCP/IP  5 March 2009

commands

• Get• Put• Del• Binary• Cd• Lcd• Dir• quite

Page 72: 212 F  Network  Programming and Design TCP/IP  5 March 2009

SMTP• Simple mail Transfer Protocol• pronounced as separate letters) Short for Simple Mail Transfer Protocol, a

protocol for sending e-mail messages between servers. Most e-mail systems that send mail over the Internet use SMTP to send messages from one server to another; the messages can then be retrieved with an e-mail client using either POP or IMAP. In addition, SMTP is generally used to send messages from a mail client to a mail server. This is why you need to specify both the POP or IMAP server and the SMTP server when you configure your e-mail application.

• [pronounced as separate letters) Short for Simple Mail Transfer Protocol, a protocol for sending e-mail messages between servers. Most e-mail systems that send mail over the Internet use SMTP to send messages from one server to another; the messages can then be retrieved with an e-mail client using either POP or IMAP. In addition, SMTP is generally used to send messages from a mail client to a mail server. This is why you need to specify both the POP or IMAP server and the SMTP server when you configure your e-mail application. ]

• [See also http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol]

Page 73: 212 F  Network  Programming and Design TCP/IP  5 March 2009

SMTP exercises

• Select five com.hk sites and five .com and ‘telnet to their SMTP port’ to see the results.

• Write a C/shell/script to automatically telnet hosts on a file.

Page 74: 212 F  Network  Programming and Design TCP/IP  5 March 2009

TCP/IP Administration Tools

• Netstat

• Ifconfig

• Ping

Page 75: 212 F  Network  Programming and Design TCP/IP  5 March 2009

212 (April 2007) Network

Programming and DesignTutorial 4 for group 1

(Internet application development on the Unix Platform)

Course Coordinator : Dr. Philip TsangTutor : Mr. Joe Lee

URL of my teaching website : http://ucourse.ouhk.edu.hk/~t441051/ct212

Page 76: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Agenda for Tutorial 4• Review for Unit 3• Objectives of Unit 4• Your Linux/UNIX accounts provided by OUHK• Linux/UNIX architecture• About shell• Linux/UNIX file and directory• Linux/UNIX permission right• Hint for Linux/UNIX• Linux/UNIX editor • File transfer (ftp, wsftp)• Shell programming• Compiling C program in Linux/UNIX

Page 77: 212 F  Network  Programming and Design TCP/IP  5 March 2009

• Identify the key features of the UNIX programming environment

• Discuss the concept of UNIX shells• Discuss syntax of UNIX commands• Describe the UNIX file and directory systems• Introduce a pico text editor in UNIX• Introduce some basic concepts in Shell

programming• Describe the compilation tool available in

UNIX

Objective of Unit 4

Page 78: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Linux/UNIX architecture

kernel

shell

application

Page 79: 212 F  Network  Programming and Design TCP/IP  5 March 2009

• 3 parts :

1. Utility, command or script : eg. ls, mkdir, sort, named, ..

2. Shell (command interpreter) • Interpret command or script for kernel execution

• Provide working environment for your login session

• Provides script language

3. Kernel Driver code Actual Interface with memory, I/O Execution of command

Linux/UNIX architecture

Page 80: 212 F  Network  Programming and Design TCP/IP  5 March 2009

• In using Linux/UNIX, you are always facing shell:

1. When you login, you will be provided with a default shell (working shell), which provides a working environment for you.

2. When you run a shell script or command, a shell will be invoked to interpret your shell script or command.

– Shell type of this invoked shell can be of same or different from your working environment shell.

– This is controlled by first line of your shell script (eg. #!/bin/sh)

About Shell (1)

Page 81: 212 F  Network  Programming and Design TCP/IP  5 March 2009

• Different shell type provides: Different working environment, which can be

customized by your startup script Different startup script Different syntax of shell script statement

• Default shell of Red Hat Linux is k-shell (ksh)• How to know my current working shell

– Echo $SHELL

• How to change your default shell1. login to the system2. Type “chsh”, then input pathname of your desired shell

Pathname of all shells are /bin/*sh

About Shell (2)

Page 82: 212 F  Network  Programming and Design TCP/IP  5 March 2009

About Shell (3) – startup script

• Function : setup initial environment variables when you login

shell System-wide User-wide

Bourne (sh) /etc/profile ~/.profile

C (csh) /etc/csh.login ~/.cshrc , then ~/.login

Korn (ksh) /etc/profile ~/.profile

Bash (bash) ~/.bash_profile

Tcsh (tcsh)

Page 83: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Directory & file (1) – file typeFirst column represents file type :

normal file (-)directory (d)device file (b or c)link file (l)

Page 84: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Directory & file (2) - hierarchy

Page 85: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Directory & file (3) – file reference You can reference file/directory by relative or absolute pathname

Current directory (shown by pwd)1. Absolute pathname (begins with “/”)

2. Relative pathname (relative to current directory)Begins with either “.” (current directory) or “..” (parent directory)

Page 86: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Directory & file (4) - wildcard

• To reference a group of files/directories with common in filename, we use “wildcards”:– * matches any (zero or more) characters except

that first character cannot be “.”– ? matches any single character except “.”– [a-z,A-Z,0-9] matches any single character which

is alphanumeric– [!a-z,A-Z,0-9] matches any single character which

is not alphanumeric

Page 87: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Directory & file (5) - Command• To display current directory : pwd• Change current directory : cd new_dir_pathname• Create a directory : mkdir dir_name• Delete a directory : rmdir dir_name (dir. must be

empty) or rm -R dir_name (even dir. is not empty)• List content of a directory : ls –l dir_pathname• Delete file(s) : rm filename• Display content of a file :

cat filename or more filename

• Copy or overwrite file : cp src_filename dest_filename

• Rename file : mv old_filename new_filename• Move file : mv src_filepathname new_filepathname

Page 88: 212 F  Network  Programming and Design TCP/IP  5 March 2009

File permission right (1)

File owner

These 9 columns represents the permission rights for 3 types of users – 3 columns for file owner, 3 columns for group owner, 3 columns for others.

Only file owner and root can change the permission right.

To display permission right of file

Group owner Filename

Page 89: 212 F  Network  Programming and Design TCP/IP  5 March 2009

File permission right (2) –File ownerTo display permission right of file

These 3 columns represents the permission rights for file owner. File owner has read ( r ) , write (w) , execute (x) right for file AA2. Numerically, his right is 700 (111 000 000)

Page 90: 212 F  Network  Programming and Design TCP/IP  5 March 2009

File permission right (3) – group ownerTo display permission right of file

These 3 columns represents the permission rights for group owner. Group owner has read ( r ) , no write (-) , execute (x) right for file BB. Numerically, his right is 750 (111 101 000)

Page 91: 212 F  Network  Programming and Design TCP/IP  5 March 2009

File permission right (4) – othersTo display permission right of file

These 3 columns represents the permission rights for others. Others has read ( r ) , no write (-) , execute (x) right for file CC. Numerically, his right is 705 (111 000 101)

Page 92: 212 F  Network  Programming and Design TCP/IP  5 March 2009

• How to change permission right

File permission right (5) – command

chmod is the command

700 is New permission rightAA is the filename

Page 93: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Directory permission right (1)To display permission right of directory

Still classify users into 3 types – file owner, group owner, othersStill classify users into 3 types – file owner, group owner, others

Meaning of r, w, x is different from that for file. Meaning of r, w, x is different from that for file.

r : allow listing content of directory (i.e. ls /dir1/*)r : allow listing content of directory (i.e. ls /dir1/*)

x : allows enter (i.e. cd ) into the directoryx : allows enter (i.e. cd ) into the directory

w + x : allow creating, deleting, renaming files within the w + x : allow creating, deleting, renaming files within the directorydirectory

Numeric permission is similar to that for file – 700 (Numeric permission is similar to that for file – 700 (111111 000000 000000))

Page 94: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Directory permission right (2)To display permission right of directory

Setting “dir2 with this permission right (xx1)” + “read right (xx4) Setting “dir2 with this permission right (xx1)” + “read right (xx4) for html files under dir2” for html files under dir2”

html files under dir2html files under dir2 can be read by everyone ( can be read by everyone (othersothers) via ) via browserbrowser

Numeric permission is 701 (Numeric permission is 701 (111111 000000 001001))

Page 95: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Directory permission right (3)To display permission right of directory

Setting “dir3 with this permission right (xx5)” + “read right (xx4) Setting “dir3 with this permission right (xx5)” + “read right (xx4) for html files under dir3” for html files under dir3”

html files under dir3html files under dir3 can be “ can be “readread by everyone ( by everyone (othersothers) via ) via browser” + “everyone (browser” + “everyone (othersothers) can ) can list filenameslist filenames of files under dir3 of files under dir3 via browser”via browser”

Numeric permission is 705 (Numeric permission is 705 (111111 000000 101101))

Page 96: 212 F  Network  Programming and Design TCP/IP  5 March 2009

• How to change permission right for a directory

Directory permission right (4)

chmod is the command

704 is New permission right dir1 is directory name

To change permission right of “dir2, all files and subdirectory under dir2” recursively, use option “-R”

Page 97: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Useful hints for Linux/UNIX• all input to UNIX is case-sensitive• command syntax :

command_name [-options] [arguments]• You can get online help of a command by “man

command_name”• types of variables – environment, shell variable• check your environment variable : env • default path : echo $PATH• your home directory : $HOME (or ~)• goto your home directory : cd• How to change your environment variable (eg. $TERM) : export TERM=vt100• change your password : passwd

(follow the prompt message to change your password)

Page 98: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Fast Linux shell commands intro

• http://www.youtube.com/watch?v=LssPpKtb2oQ&feature=related

Page 99: 212 F  Network  Programming and Design TCP/IP  5 March 2009

UNIX editors (1)• 2 most common editors :

– pico • more user-friendly,

• Easy to learn,

• more suitable for normal user

– vi • more powerful,

• hard to learn,

• more suitable for administrator

• learn by yourself through Joe’s Website -> reference page and news page

Page 100: 212 F  Network  Programming and Design TCP/IP  5 March 2009

File transfer• 4 tools : ftp or wsftp or winscp, filezilla

– ftp• already exist on every unix machine or

win9x/NT/2000• Command mode• Commands : open, quit, help, ascii, binary, put,

mput, get, mget, prompt

– wsftp • Freeware, downloadable from Joe’s Website ->

News page• GUI mode, user-friendly• Installed by yourself• run on win9x/XP

Page 101: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Shell programming (1) – script basics

• How does UNIX know this is shell script?– It check the first line of script file for #!/bin/ksh (or #!/bin/sh or #!/bin/csh)

• How to make shell script executable by yourself?– In unix prompt, chmod 700 script_filename

• How to run shell script?– For ksh or sh script,

• Pathname_of_script (eg. ./test.ksh or /data1/home1/tutors/t441051/test.ksh)

– For csh script,• source Pathname_of_script (eg. source ~/.test.csh)

Page 102: 212 F  Network  Programming and Design TCP/IP  5 March 2009

ASCII Animation

• http://www.youtube.com/watch?v=jnlpbQ5IxHk&feature=related

Page 103: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Shell programming (2) – script basics

• Contents of a script can be:– Comment prefixed by #– Shell built-in commands (mkdir, cd, echo, eval, etc.)– UNIX external utilities (cp, tar, grep, awk, etc.)– Shell variable assignment statement (Var_name=“test”;

variable is declared and assigned at the same time)– Arithmetic or relational statement– Control flow statement :

• Conditional expression (if, then, else, elseif)• Looping expression• Case statement• Test statement

– File I/O statement (read for input or echo for output)

Page 104: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Shell programming (3) – shell variable• Declaration and assignment within one statement• Content is manipulated as string• Content can be

– single, double quoted for string– backslash quoted for single character with special meaning , eg. $ ? & ‘ ’ “

” ( ) [ ] | < > `

• Accessed by ${VAR_NAME} or $VAR_NAME• Eg. VAR1=abc• Eg. VAR2=‘this is a variable’• Eg. VAR3=“today is `date` and VAR1 is $VAR1”• Eg. VAR4=“\$VAR1”• Read and run file var.ksh (downloadable from Joe’s web site ->

tutorial page)

Page 105: 212 F  Network  Programming and Design TCP/IP  5 March 2009

var.ksh4 “echo VARn” statements not displayed

Page 106: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Shell programming (4) – arithmetic

• Two methods : make use of either utility expr or bc

• expr – support arithmetic of 2 integers only– + - * / %

• bc– Support floating point arithmetic of expression– Not include %

• Read and run file arith.ksh (downloadable from Joe’s web site ->tutorial page)

Page 107: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Arithmetic of integers only

Arithmetic of real numbers

Echo is used to print “…..” to monitor

Page 108: 212 F  Network  Programming and Design TCP/IP  5 March 2009
Page 109: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Shell programming (5) – relational• [ expr1 -eq expr2 ]

– Is equal to

• [ expr1 -ne expr2 ]– Is not equal to

• [ expr1 -lt expr2 ]– Is less than

• [ expr1 -le expr2 ]– Is less than or equal to

• [ expr1 -gt expr2 ]– Is greater than

• [ expr1 -ge expr2 ]– Is greater than or equal to

At least one or At least one or more spacemore space

Page 110: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Shell programming (6) – conditional

if condition1

then

command_block1

elif condition2

command_block2

else

command_block3

fi

Page 111: 212 F  Network  Programming and Design TCP/IP  5 March 2009

These are all logical conditions for flow control statements

Read is used to input from keyboard and assigned value to variable “INPUT_VAR”

Page 112: 212 F  Network  Programming and Design TCP/IP  5 March 2009
Page 113: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Shell programming (7) –looping

• syntax :

while condition

do

command_block

done• Read and run file loop.ksh (downloadable from

Joe’s web site -> tutorial page)

Page 114: 212 F  Network  Programming and Design TCP/IP  5 March 2009

This is logical condition for while loop

Read input from keyboard and assigned value to variable “VAR”

Echo is used to print “…..” to monitor

Page 115: 212 F  Network  Programming and Design TCP/IP  5 March 2009
Page 116: 212 F  Network  Programming and Design TCP/IP  5 March 2009

Shell programming (8) – case

• syntax :

case string in

pattern1) command_block1 ;;

pattern2) command_block2 ;;

patterni) command_blocki ;;

*) command_block ;;

esac• Read and run file case.ksh (downloadable from

Joe’s web site ->tutorial page)

Page 117: 212 F  Network  Programming and Design TCP/IP  5 March 2009

If value $VAR is one of these values, corresponding command(s) will be executed

If value $VAR is NOT one of those listed values, command(s) in *) will be executed

Page 118: 212 F  Network  Programming and Design TCP/IP  5 March 2009
Page 119: 212 F  Network  Programming and Design TCP/IP  5 March 2009

C programming (1) – compilation

• Function of c compilation : – check for syntax error in source code– source code (*.c) -> object code (*.o) ->

executable machine code

• C compilation model :– 4 components – preprocessor, compiler,

assembler, linker (for detail, refer to http://www.cs.cf.ac.uk/Dave/C/section2_4_4.html)

• Available c compiler : – gcc (better, more ansi-compatible)

Page 120: 212 F  Network  Programming and Design TCP/IP  5 March 2009

C programming (2) – compilation

• How to compile :• 1. For single source file :

– compile to executable code :•gcc *.c (a.out is default filename for

executable code)

– compile up to object code file but no linking :•gcc –c *.c (*.o will be output)

– compile to executable code but specify filename for executable code :•gcc –o out_exe *.c (out_exe will be

executable code)

Page 121: 212 F  Network  Programming and Design TCP/IP  5 March 2009

C programming (2) – compilation

• 2. For multiple source files :– make utility + makefile (or Makefile) file– makefile file syntax :

•Maintarget: requirements1 instructions1 target1: requirements2 instructions2 target2: requirements3 instructions3

• Try make with makefile and c programs from the hand out of today’s lecture

Page 122: 212 F  Network  Programming and Design TCP/IP  5 March 2009
Page 123: 212 F  Network  Programming and Design TCP/IP  5 March 2009
Page 124: 212 F  Network  Programming and Design TCP/IP  5 March 2009

References

• Ken SteubeUNIX Bourne Shell Scripting

• http://www.esscc.uq.edu.au/~ksteube/Bshell/

• Norman Matloff ,• Unix Shell Scripts http://heather.cs.ucdavis.edu/~matloff/UnixAndC/Unix/CShellII.html

• Mendel Cooper – Advanced Bash-Scripting Guide – http://tldp.org/LDP/abs/html/

• Phil– Internet Radio to Podcast with Shell Tools" in the September, 2005 issue of

LINUX JOURNAL, http://www.linuxjournal.com/article/8171