Networking
based on slides by Prof. Sirer, Bracy, Van Renesse, Ross, Kurose
1
Networking based on slides by Prof. Sirer, Bracy, Van Renesse, Ross, - - PowerPoint PPT Presentation
Networking based on slides by Prof. Sirer, Bracy, Van Renesse, Ross, Kurose 1 Basic Network Abstraction A process can create endpoints Each endpoint has a unique address Processes can receive messages on endpoints Processes can send
1
2
■ Can multiple endpoints share the same address?
■ be arbitrarily large? ■ be lost or garbled? ■ be re-ordered?
3
■ Syntax: Layout of bits, bytes, fields, etc.
⬥message format
■ Semantics: What they mean
■ HTTP “get” requests and responses
⬥HTML is part of the format
■ Excuse me, please, thank you, etc. in real life
4
■ Each layer provides a service to layers above; relies on services from
layers below
Application Layer HTTP/FTP/DNS; exchanges messages Transport Layer Transports messages; TCP (connection oriented)/ UDP; exchanges segments Network Layer Transports segments; IP; exchanges datagrams Link Layer Transports datagrams; Ethernet/WiFi; exchanges frames Physical Layer Trasports frames;wires, signal encoding, wireless; exchanges bits
5
■ Allows to identify relationship between distinct
■ Eases maintenance and updating of system
6
destination
application transport network link physical
Ht Hn Hl M Ht Hn M Ht M M
network link physical
Ht Hn Hl M Ht Hn M Ht Hn M
link physical
7
application transport network link physical
Ht Hn M
segment
Ht Ht Hn Hl M
message
M Ht M Hn
switch
Ht Hn Hl M Ht Hn Hl M
router
datagram frame
8
Application Layer Transport Layer Network Layer Link Layer Physical Layer
■ Network Interface Cards
⬥ Ethernet, 802.11, etc.
9
controller physical transmission cpu memory host bus (e.g., PCI) network adapter card application transport network link link physical
■ Media Access Control address ■ Unique! ■ 6 bytes long ■ Ethernet example: b8:e3:56:15:6a:72 ■ Address space managed by IEEE; first 24 bits identify
■ Does not change if the NIC moves
⬥ Not true of IP address!
10
▪ collision if node receives two or more signals at the
▪
no out-of-band channel for coordination
11
12
■ divide channel into smaller “pieces” (time slots, frequency, code) ■ allocate piece to node for exclusive use
■ channel not divided, allow collisions ■ “recover” from collisions
■ nodes take turns, but nodes with more to send can take longer turns
13
1 3 4 1 3 4 6-slot frame 6-slot frame
14
15
■ share channel efficiently and fairly at high load ■ inefficient at low load: delay in channel access, 1/N
■ low load: single node can fully utilize channel ■ high load: collision overhead
16
❖ control token passed
❖ token message ❖ concerns:
17
■ transmit at full channel data rate R. ■ no a priori coordination among nodes
■ how to detect collisions ■ how to recover from collisions (e.g., via delayed
■ slotted ALOHA, ALOHA ■ CSMA, CSMA/CD, CSMA/CA
18
⬥ Later at 3COM
⬥ many orders of magnitude increase in bandwidth since early versions
19
■ Listen before you speak
■ Multiple hosts can access the network
■ Detect and respond to cases where two hosts
20
■ distance & propagation
delay play role in in determining collision probability
spatial layout of nodes
21
▪ collisions detected within short time ▪ colliding transmissions aborted, reducing channel
▪ easy in wired LANs: measure signal strengths,
▪ difficult in wireless LANs: received signal strength
22
spatial layout of nodes
23
■ after mth collision, choose
■ longer backoff interval
24
■ network-layer address for interface ■ used for layer 3 (network layer) forwarding (coming up)
■ function: used ‘locally” to get frame from one interface to another
■ 48 bit MAC address (for most LANs) burned in NIC ROM,
■ e.g.: 1A-2F-BB-76-09-AD 25
1A-2F-BB-76-09-AD 58-23-D7-FA-20-B0 0C-C4-11-6F-E3-98 71-65-F7-2B-08-53
26
▪ IP/MAC address
< IP address; MAC address; TTL>
▪ TTL (Time To Live): time
1A-2F-BB-76-09-AD 58-23-D7-FA-20-B0 0C-C4-11-6F-E3-98 71-65-F7-2B-08-53
137.196.7.23 137.196.7.78 137.196.7.14 137.196.7.88 27
■ B’s MAC address not in A’s ARP
table.
28
■ B’s MAC address not in A’s ARP
table.
■ dest MAC address = FF-FF-FF-FF-
FF-FF
■ all nodes on LAN receive ARP
query
■ frame sent to A’s MAC address
(unicast)
■ soft state: goes away unless
refreshed
■ nodes create their ARP tables
without intervention from net administrator
29
walkthrough: send datagram from A to B via R
■ focus on addressing – at IP (datagram) and MAC layer (frame) ■ assume A knows B’s IP address ■ assume A knows IP address of first hop router, R ■ assume A knows R’s MAC address (how?)
1A-23-F9-CD-06-9B 222.222.222.220 111.111.111.110 E6-E9-00-17-BB-4B CC-49-DE-D0-AB-7D 111.111.111.112 111.111.111.111 74-29-9C-E8-FF-55
222.222.222.222 49-BD-D2-C7-56-2A 222.222.222.221 88-B2-2F-54-1A-0F
30
1A-23-F9-CD-06-9B 222.222.222.220 111.111.111.110 E6-E9-00-17-BB-4B CC-49-DE-D0-AB-7D 111.111.111.112 111.111.111.111 74-29-9C-E8-FF-55
222.222.222.222 49-BD-D2-C7-56-2A 222.222.222.221 88-B2-2F-54-1A-0F
IP Eth Phy
IP src: 111.111.111.111 IP dest: 222.222.222.222
❖
A creates IP datagram with IP source A, destination B
❖
A creates link-layer frame with R's MAC address as dest, frame contains A-to-B IP datagram
MAC src: 74-29-9C-E8-FF-55 MAC dest: E6-E9-00-17-BB-4B
31
1A-23-F9-CD-06-9B 222.222.222.220 111.111.111.110 E6-E9-00-17-BB-4B CC-49-DE-D0-AB-7D 111.111.111.112 111.111.111.111 74-29-9C-E8-FF-55
222.222.222.222 49-BD-D2-C7-56-2A 222.222.222.221 88-B2-2F-54-1A-0F
IP Eth Phy
❖
frame sent from A to R
IP Eth Phy
❖
frame received at R, datagram removed, passed up to IP
MAC src: 74-29-9C-E8-FF-55 MAC dest: E6-E9-00-17-BB-4B IP src: 111.111.111.111 IP dest: 222.222.222.222 IP src: 111.111.111.111 IP dest: 222.222.222.222
32
1A-23-F9-CD-06-9B 222.222.222.220 111.111.111.110 E6-E9-00-17-BB-4B CC-49-DE-D0-AB-7D 111.111.111.112 111.111.111.111 74-29-9C-E8-FF-55
222.222.222.222 49-BD-D2-C7-56-2A 222.222.222.221 88-B2-2F-54-1A-0F
IP src: 111.111.111.111 IP dest: 222.222.222.222
❖
R forwards datagram with IP source A, destination B
❖
R creates link-layer frame with B's MAC address as dest, frame contains A-to-B IP datagram
MAC src: 1A-23-F9-CD-06-9B MAC dest: 49-BD-D2-C7-56-2A
IP Eth Phy IP Eth Phy
33
1A-23-F9-CD-06-9B 222.222.222.220 111.111.111.110 E6-E9-00-17-BB-4B CC-49-DE-D0-AB-7D 111.111.111.112 111.111.111.111 74-29-9C-E8-FF-55
222.222.222.222 49-BD-D2-C7-56-2A 222.222.222.221 88-B2-2F-54-1A-0F
❖
R forwards datagram with IP source A, destination B
❖
R creates link-layer frame with B's MAC address as dest, frame contains A-to-B IP datagram
IP src: 111.111.111.111 IP dest: 222.222.222.222 MAC src: 1A-23-F9-CD-06-9B MAC dest: 49-BD-D2-C7-56-2A
IP Eth Phy IP Eth Phy
34
1A-23-F9-CD-06-9B 222.222.222.220 111.111.111.110 E6-E9-00-17-BB-4B CC-49-DE-D0-AB-7D 111.111.111.112 111.111.111.111 74-29-9C-E8-FF-55
222.222.222.222 49-BD-D2-C7-56-2A 222.222.222.221 88-B2-2F-54-1A-0F
❖
R forwards datagram with IP source A, destination B
❖
R creates link-layer frame with B's MAC address as dest, frame contains A-to-B IP datagram
IP src: 111.111.111.111 IP dest: 222.222.222.222 MAC src: 1A-23-F9-CD-06-9B MAC dest: 49-BD-D2-C7-56-2A
IP Eth Phy
35
Metcalfe’s Ethernet sketch
36
■ all nodes in same collision domain
■ active switch in center ■ each “spoke” runs a (separate) Ethernet protocol (nodes
switch
37
❖ 7 bytes with pattern 10101010 followed by one
❖ used to synchronize receiver, sender clock rates
dest. address source address
data (payload) preamble type CRC
38
❖ addresses: 6 byte source, destination MAC addresses ▪ if adapter receives frame with matching destination
▪ otherwise, adapter discards frame ❖ type: indicates higher layer protocol (mostly IP but
❖ CRC: cyclic redundancy check at receiver (basically, a
▪ error detected: frame is dropped
dest. address source address
data (payload) CRC preamble type
46 to 1500 bytes
39
Link Layer
40
■ data in dropped frames recovered only if higher
⬥ Certification process tries to assure compliance ⬥ Not everyone always backs off exponentially
⬥ But the data is available for all to see
■ All packets are broadcast on the wire ■ Can place Ethernet card in promiscuous mode and liste
41
■ store, forward Ethernet frames ■ examine incoming frame’s MAC address,
■ hosts are unaware of presence of switches
■ switches do not need to be configured
42
■ each link is its own collision
A’
B’
C’
43
A’
B’
C’
❖ A: each switch has a switch
44
A’
B’
C’
■ when frame received,
■ records sender/
A A’
Source: A Dest: A’
MAC addr interface TTL
45
A’
B’
C’ 1 2 3 4 5 6
A A’
Source: A Dest: A’
MAC addr interface TTL
A 1 60 A A’ A A’ A A’ A A’ A A’
A’ A ❖ destination A location
A’ 4 60
46
47
Link Layer
48
❖ A: self learning! (works exactly the same as in
49
Application Layer Transport Layer Network Layer Link Layer Physical Layer
50
❖ each with their own
❖ address format and allocation scheme ❖ packet format ❖ LAN-level protocols, reliability guarantees
❖ Wouldn’t it be nice to tie them all together?
❖ Nodes with multiple NICs can provide the glue! ❖ Standardize address and packet formats
❖ This gives rise to an “Internetwork”
❖ aka WAN (wide-area network)
51
52
⬥ How do you coordinate a nuclear attack ?
⬥ Along with a lot of other properties
53
■ the term packet is probably more common though…
54
Presentation Transport Network Data Link Physical Application Presentation Transport Network Data Link Physical Application Network Data Link Physical Network Data Link Physical
55
Session Session
Presentation Transport Network Data Link Physical Application Presentation Transport Network Data Link Physical Application Network Data Link Physical Network Data Link Physical
56
Session Session
Connection management (RPC) Format translation, serialization, encryption
(EBCDIC/ASCI)
■ Headers often contain a “demultiplexer” like a port or
57
⬥ Guaranteed, or ordered, packet delivery, duplicate suppression,
security, etc.
⬥ Packets are sent on a best-effort basis ⬥ Higher-level applications do the rest
58
1
2 3
0111
value in arriving packet’s header
routing algorithm
local forwarding table header value
0100 0101 0111 1001 3 2 2 1
■ routing algorithms
determines end-end path through network determines local forwarding at router
59
❖ guaranteed delivery ❖ guaranteed delivery with
60
Network Architecture Internet ATM ATM ATM ATM Service Model best effort CBR VBR ABR UBR Bandwidth none constant rate guaranteed rate guaranteed minimum none Loss no yes yes no no Order no yes yes yes yes Timing no yes yes no no Congestion feedback no (inferred via loss) no congestion no congestion yes no Guarantees ?
61
❖ Much like a phone network
❖ Coming up…
62
application transport network data link physical application transport network data link physical
63
1
2 3
IP destination address in arriving packet’s header
routing algorithm local forwarding table dest address output link
address-range 1 address-range 2 address-range 3 address-range 4 3 2 2 1
4 billion IP addresses, so rather than list individual destination address list range of addresses (aggregate table entries)
64
Destination Address Range 11001000 00010111 00010000 00000000 through 11001000 00010111 00010111 11111111 11001000 00010111 00011000 00000000 through 11001000 00010111 00011000 11111111 11001000 00010111 00011001 00000000 through 11001000 00010111 00011111 11111111
Link Interface 1 2 3
65
Destination Address Range 11001000 00010111 00010*** ********* 11001000 00010111 00011000 ********* 11001000 00010111 00011*** *********
DA: 11001000 00010111 00011000 10101010
examples:
DA: 11001000 00010111 00010110 10100001
which interface? which interface?
Link interface 1 2 3
66
ver length 32 bits
data (variable length, typically a TCP
16-bit identifier header checksum time to live 32 bit source IP address head. len type of service flgs fragment
upper layer 32 bit destination IP address
IP protocol version number header length (bytes) upper layer protocol to deliver payload to total datagram length (bytes) “type” of data for fragmentation/ reassembly max number remaining hops (decremented at each router) e.g. timestamp, record route taken, specify list of routers to visit.
how much overhead?
❖
20 bytes of TCP
❖
20 bytes of IP
❖
= 40 bytes + app layer overhead
67
■ different link types,
■ one datagram becomes
■ “reassembled” only at final
■ IP header bits used to
fragmentation: in: one large datagram
reassembly
68
ID =x
=0 fragflag =0 length =4000 ID =x
=0 fragflag =1 length =1500 ID =x
=185 fragflag =1 length =1500 ID =x
=370 fragflag =0 length =1040
several smaller datagrams
❖
4000 byte datagram
❖
MTU = 1500 bytes
1480 bytes in data field
1480/8
69
⬥ IPv4: 32-bit descriptor, e.g. 128.84.12.43 ⬥ IPv6: 128-bit descriptor (but only 64 bits “functional”) ⬥ Will use IPv4 unless specified otherwise…
⬥ IP addresses can be re-used ⬥ Same NIC may have different IP addresses over time
70
network consisting of 3 subnets
223.1.1.1 223.1.1.3 223.1.1.4 223.1.2.9 223.1.3.2 223.1.3.1
subnet
223.1.1.2 223.1.3.27 223.1.2.2 223.1.2.1
71
223.1.1.1 223.1.1.3 223.1.1.4 223.1.2.2 223.1.2.1 223.1.2.6 223.1.3.2 223.1.3.1 223.1.3.27 223.1.1.2 223.1.7.0 223.1.7.1 223.1.8.0 223.1.8.1 223.1.9.1 223.1.9.2
72
▪ subnet portion of address of arbitrary length ▪ address format: a.b.c.d/x, where x is # bits in
subnet part host part
73
■ Windows: control-panel->network-
■ UNIX: /etc/rc.config
■ “plug-and-play”
74
DHCP is used to discover IP addresses (and more)
DHCP = Dynamic Host Configuration Protocol
“I just got here. My physical address is 1a:34:2c:9a:de:cc. What’s my IP?”
“Your IP is 128.84.96.89 for the next 24 hours”
75
10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4 138.76.29.7
local network (e.g., home network) 10.0.0/24 rest of Internet
76
■ range of addresses not needed from ISP: just one IP
■ can change addresses of devices in local network
■ can change ISP without changing addresses of devices
■ devices inside local net not explicitly addressable,
77
■ outgoing datagrams: replace (source IP address, port #) of
■ remember (in NAT translation table) every (source IP address,
■ incoming datagrams: replace (NAT IP address, new port #) in
78
Network Layer 79
10.0.0.1 10.0.0.2 10.0.0.3
S: 10.0.0.1, 3345 D: 128.119.40.186, 80
1
10.0.0.4 138.76.29.7
1: host 10.0.0.1 sends datagram to 128.119.40.186, 80 NAT translation table WAN side addr LAN side addr 138.76.29.7, 5001 10.0.0.1, 3345 …… ……
S: 128.119.40.186, 80 D: 10.0.0.1, 3345
4
S: 138.76.29.7, 5001 D: 128.119.40.186, 80
2 2: NAT router changes datagram source addr from 10.0.0.1, 3345 to 138.76.29.7, 5001, updates table
S: 128.119.40.186, 80 D: 138.76.29.7, 5001
3 3: reply arrives
138.76.29.7, 5001 4: NAT router changes datagram dest addr from 138.76.29.7, 5001 to 10.0.0.1, 3345
■ routers should only process up to layer 3 ■ violates end-to-end argument
■ address shortage should instead be solved by IPv6
80
81
82
83
84
■ has a private IP network ■ runs its own routing protocols ■ may have multiple IP subnets
■ has a unique “AS number”
85
86
■ Edge or Border router: Resides at the edge of an
■ Interior router:
87
■ Transit AS: forwards packets coming from one AS
■ Stub AS: has only links to ASs higher in the
88
89
(intradomain)
90
■ no “upstream peers” ■ instead, peers with every other Tier-1 AS ■ “default-free” routing ■ “settlement-free connections”
■ a stub, connecting to one or more upstream ISPs ■ connects consumers to the Internet
■ everything in between, i.e., transit ASs that have upstream ASs,
91
92 IXP: Internet Exchange Point
1
2 3 IP destination address in arriving packet’s header
routing algorithm local forwarding table dest address output link
address-range 1 address-range 2 address-range 3 address-range 4 3 2 2 1
routing algorithm determines end-end-path through network forwarding table determines local forwarding at this router
93
■ For now, assume a unity weight associated with each link
■ e.g., Dijkstra’s shortest path algorithm
■ Distance vector algorithm 94
Network Layer 95
■ x maintains distance vector
■ knows cost to each neighbor v: ■ maintains its neighbors’ distance vectors. For each
Network Layer 96
❖ under minor, natural conditions, the estimate Dx(y)
Network Layer 97
■ neighbors then notify their
neighbors if necessary
cost or msg from neighbor)
if DV to any dest has changed, notify neighbors
98
■ but instead of shortest path, uses various other
99
■ business relationships ■ traffic engineering ■ scalability (improving stability, aggregation, etc.) ■ security
100
■ import policy: which routes (advertised by peers) are
■ decision process: which routes are used ■ export policy: which routes are advertised to peers
■ BGP allows advertised routes to be tagged with
101
102
Application Layer Transport Layer Network Layer Link Layer Physical Layer
Presentation Transport Network Data Link Physical Application Presentation Transport Network Data Link Physical Application Network Data Link Physical
104
Session Session
datagrams frames bits
application transport network data link physical
logical end-end transport
application transport network data link physical
105
a p p l i c a t i
s a p p l i c a t i
s t r a n s p
t l a y e r t r a n s p
t l a y e r
process socket
transport application physical link network
transport application physical link network
transport application physical link network
107
process socket
transport application physical link network
transport application physical link network
transport application physical link network
108
109
ver length 32 bits
data (variable length, typically a TCP
16-bit identifier header checksum time to live head. len type of service flgs fragment
upper layer
32 bit source IP address
32 bit destination IP address
source port # dest port #
application data (payload)
TCP/UDP segment format
Dear James,
Services not available:
■ delay guarantees ■ bandwidth guarantees
application transport network data link physical 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 data link physical network data link physical
logical end-end transport
110
111
112
source port # dest port #
application data (payload) length UDP segment format
Dear James,
32 bits
checksum
113
transport application physical link network
transport application physical link network
transport application physical link network
DatagramSocket mySocket1 = new DatagramSocket (5775);
source port: 9157 dest port: 6428 source port: 6428 dest port: 9157 source port: ? dest port: ? source port: ? dest port: ?
DatagramSocket mySocket2 = new DatagramSocket (9157); DatagramSocket sererSocket = new DatagramSocket (6428);
114
▪ each socket identified by
▪ non-persistent HTTP will
115
transport application physical link network
transport application physical link
transport application physical link network
source IP,port: A,9157 dest IP, port: B,80 source IP,port: B,80 dest IP,port: A,9157
host: IP address A host: IP address C
network
source IP,port: C,5775 dest IP,port: B,80 source IP,port: C,9157 dest IP,port: B,80
three segments:
server: IP address B
116
117
source port # dest port #
32 bits
application data (variable length) sequence number acknowledgement number
receive window Urg data pointer checksum
F S R P A U
head len not used
URG: urgent data (generally not used) ACK: ACK # valid PSH: push data now (generally not used) RST, SYN, FIN: connection estab (setup, teardown commands) # bytes rcvr willing to accept counting by bytes
(not segments!) Internet checksum (as in UDP)
118
⬥Initial number chosen randomly ⬥Number incremented by the data length
⬥ Can acknowledge a sequence of bytes by ack’ing latest
119
120
I would tell you a joke about TCP... If only to be acknowledged 😣
■ Both sides will have seen one packet from the
■ SYN-ACK also typically carries a new port for the
■ Both sides will know that the other side is ready to
121
⬥ Need to wait after a FIN for
straggling packets
122
123
Here's a joke about TCP. Did you get it? Did you get it? Did you get it? Did you get it?
■ Want improved throughput w/o unnecessary transmissions
AverageRTT := (1 - α) AverageRTT + α LatestRTT AverageVar := (1 - β) AverageVar + β LatestVar where LatestRTT = (ack_receive_time – send_time), LatestVar = |LatestRTT – AverageRTT|, α = 1/8, β = 1/4 typically. Timeout := AverageRTT + 4*AverageVar
124
125
126
127
128
129
130
131
132
133
Application Layer Transport Layer Network Layer Link Layer Physical Layer
134
■ note: core Internet function,
■ complexity at network’s “edge”
135
■
canonical, alias names
■ replicated Web servers:
136
Root DNS Servers com DNS servers
edu DNS servers cornell.edu DNS servers utexas.edu DNS servers yahoo.com DNS servers amazon.com DNS servers pbs.org DNS servers
… …
137
■ contacts authoritative name server if name mapping not known ■ gets mapping ■ returns mapping to local name server
13 root name “servers” worldwide
(5 other sites)
(41 other sites)
Palo Alto, CA (and 48 other sites)
(5 other sites)
OH (5 other sites)
138
■ responsible for com, org, net, edu, aero, jobs, museums,
■ Network Solutions maintains servers for .com TLD ■ Educause for .edu TLD
■ organization’s own DNS server(s), providing authoritative
■ can be maintained by organization or service provider
139
■ also called “default name server”
■ has local cache of recent name-to-address
■ acts as proxy, forwards query into hierarchy
140
requesting host
cs.utexas.edu irnerio.cs.cornell.edu
root DNS server local DNS server
dns.utexas.edu
1 2 3 4 5 6
authoritative DNS server dns.cs.cornell.edu
7 8 TLD DNS server
❖ contacted server replies
❖ “I don’t know this name,
141
4 5 6 3
❖ puts burden of name
❖ heavy load at upper
requesting host
cs.utexas.edu irnerio.cs.cornell.edu
root DNS server local DNS server
dns.utexas.edu
1 2 7
authoritative DNS server dns.cs.cornell.edu
8
TLD DNS server
142
⬥ thus root name servers not often visited
❖ cached entries may be out-of-date (best effort name-
143
■ Not successful to date ■ Traffic Filtering ■ Local DNS servers cache IPs
■ Potentially more dangerous
▪ Intercept queries
▪ Send bogus replies to DNS
144
145
Internet controlled by OS controlled by app developer
transport application physical link network
process
transport application physical link network
process
socket
146
147
close clientSocket read datagram from clientSocket create socket: clientSocket = socket(AF_INET,SOCK_DGRAM) Create datagram with server IP and port=x; send datagram via clientSocket create socket, port= x: serverSocket = socket(AF_INET,SOCK_DGRAM) read datagram from serverSocket write reply to serverSocket specifying client address, port number
148
149
from socket import * serverName = ‘hostname’ serverPort = 12000 clientSocket = socket(AF_INET, SOCK_DGRAM) message = raw_input(’Input lowercase sentence:’) clientSocket.sendto(message.encode(),
(serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048) print modifiedMessage.decode() clientSocket.close()
include Python’s socket library create UDP socket for server get user keyboard input Attach server name, port to message; send into socket print out received string and close socket read reply characters from socket into string
150
from socket import * serverPort = 12000 serverSocket = socket(AF_INET, SOCK_DGRAM) serverSocket.bind(('', serverPort)) print (“The server is ready to receive”) while True:
message, clientAddress = serverSocket.recvfrom(2048) modifiedMessage = message.decode().upper() serverSocket.sendto(modifiedMessage.encode(), clientAddress)
create UDP socket bind socket to local port number 12000 loop forever Read from UDP socket into message, getting client’s address (client IP and port) send upper case string back to this client
151
152
wait for incoming connection request connectionSocket = serverSocket.accept() create socket, port=x, for incoming request: serverSocket = socket() create socket, connect to hostid, port=x clientSocket = socket()
send request using clientSocket read request from connectionSocket write reply to connectionSocket
TCP connection setup
close connectionSocket read reply from clientSocket close clientSocket
153
from socket import * serverName = ’servername’ serverPort = 12000 clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket.connect((serverName,serverPort)) sentence = raw_input(‘Input lowercase sentence:’) clientSocket.send(sentence.encode()) modifiedSentence = clientSocket.recv(1024) print (‘From Server:’, modifiedSentence.decode()) clientSocket.close()
create TCP socket for server, remote port 12000 No need to attach server name, port
154
from socket import * serverPort = 12000 serverSocket = socket(AF_INET,SOCK_STREAM) serverSocket.bind((‘’,serverPort)) serverSocket.listen(1) print ‘The server is ready to receive’ while True: connectionSocket, addr = serverSocket.accept() sentence = connectionSocket.recv(1024).decode() capitalizedSentence = sentence.upper() connectionSocket.send(capitalizedSentence. encode()) connectionSocket.close()
create TCP welcoming socket server begins listening for incoming TCP requests loop forever server waits on accept() for incoming requests, new socket created on return read bytes from socket (but not address as in UDP) close connection to this client (but not welcoming socket)