Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
IEG 3310 B
Chapter #2 - Internet and TCP
Page 1
Outline
TCP/IP Architecture
Access Network
IP, TCP and UDP
Socket API
Page 2
TCP/IP architecture
OSI
Application
TCP/IP
Application
Presentation
Session
Transport
Transport
Network
Internet
Data Link
Network Interface
(Ethernet, ADSL, ATM, etc.)
Physical
Page 3
TCP/IP protocol graph
Ping
Telnet
&
Rlogin
Transmission
Control
Procedure
FTP
SMTP
Traceroute
DNS
TFTP
UDP
TCP
ICMP
IP
ARP
Data
Link
Physical Media
BOOTP
&
DHCP
SNMP
User
Datagram
protocol
IGMP
RARP
Page 4
Outline
TCP/IP Architecture
Access Network
IP, TCP and UDP
Socket API
Page 5
Network Cabling
Cable – allows you to connect devices
Connector – standard interface
for making connections.
Note: We need different cables & connectors
for different links.
Page 6
Repeater and Hub
<100m
1.
Repeater
2.
Hub – multiple port
repeater
Signal regeneration
<100m
<100m
uplink
<100m
<100m
The signal is regenerated on all segments
Page 7
Switches and Hub
Number of ports: 8/16/24/…
Nature: Hub or Switch (access/core)
Speed: 10/100/1Gbps
Interfaces: Fiber/Copper
Backplane bus: stackability
Uplink: connecting to core switch
Management: SNMP and web
Others:
redundant supply
control console
…
Page 8
Using a Hub or Switch
Straight Through Cable
Other lines: unused.
Computer
Hub
1 Tx+
1 Rx+
2 Tx-
2 Rx-
3 Rx+
3 Tx+
6 Rx-
6 TxPage 9
Direct Connection
We need a cross over cable – one side uses
568A and the other side uses 568B.
WOGBN – Write: Orange Green, bluish brown
Page 10
How to wire many
computers?
Structured Cabling and Patch Panels
Page 11
Ethernet Transmissions
• The address of a node as defined by its LAN.
• The address in included in an Ethernet frame of data.
•Ethernet uses a six-byte physical address which is
imprinted on the network interface card (NIC)
Page 12
Ethernet Frames
Data is divided into frames.
64-1518 bytes
Src address: 6 bytes
Dst address: 6 bytes
Page 13
LAN addressing
Every Ethernet node has a built-in, unique MAC (medium
access control) address. It represents the address of
the node on a shared Ethernet network.
Finding your MAC address: winipcfg or ipconfig /all
Page 14
Outline
TCP/IP Architecture
Access Network
IP, TCP and UDP
Socket API
Page 15
IP Addressing
The Internet (IP) addressing. Each address is represented by
4 bytes, denoting
(1) Network address
(2) Host address
202.45.183.1
11001010
00101101
Network-ID
10110111
00000001
Host-ID
Host address=0 the network, host address=1s broadcast.
Page 16
IP Addresses
Class
A
7 bits
24 bits (16777214)
netid
0
hostid
14 bits
B
16 bits (65534)
netid
1 0
hostid
21 bits
8 bits (254)
hostid
C
1 1 0
netid
D
1 1 1 0
Multicast address
E
1 1 1 1 0
Reserved for future use
CUHK : 137.189.X.X (dotted decimal notation) -> a Class B network
A
B
C
1st Octet
1 - 126
128 - 192
192 - 223
netid
p
p.q
p.q.r
hostid
q.r.s
r.s
s
Page 17
Internet Domains
magnet3.sth.hk
137.150.96.48
137.150.96.252
IGS2.STH.HK
magnet2.pwh.hk
137.154.96.251
137.154.96.250
AGS.PWH.HK
137.148.96.47
137.148.96.252
adnet1
137.152.96.254
adnetpc0
T-1
137.152.96.253
IGS1.IE.CUHK.HK
137.189.96.252
137.189.96.131 137.189.96.37
Page 18
IP Packet Format
0
8
VERS HLEN
31
16
TOTAL LENGTH
SERVICE TYPE
IDENTIFICATION
FLAGS
FRAGMENT OFFSET
HEADER CHECKSUM
TIME TO LIVE PROTOCOL
Packet
Header
SOURCE IP ADDRESS
DESTINATION IP ADDRESS
IP OPTIONS (IF ANY)
PADDING
DATA
...
Packet
Data
Page 19
How are IP packets routed?
magnet3.sth.hk
137.150.96.48
(1)
137.150.96.252
IGS2.STH.HK
magnet2.pwh.hk
137.154.96.251
(2)
137.154.96.250
AGS.PWH.HK
137.148.96.47
137.148.96.252
137.152.96.254
(3)
T-1
adnetpc0
137.152.96.253
(4)
137.189.96.252
IGS1.IE.CUHK.HK
137.189.97.37
IP address
with net_id
137.152.0.0
137.154.0.0
137.148.0.0
137.150.0.0
137.189.0.0
routing
directly
directly
directly
137.154.96.250
137.152.96.254
Page 20
Routing table for AGS.PWH.HK
IP Routing Algorithm
Route_IP_Datagram(datagram, routing_table)
Extract destination address, I-d, from datagram
Compute IP address of destination network, I-n
IP net_id
routing
137.152.0.0 directly
137.154.0.0 directly
137.148.0.0 directly
137.150.0.0
137.154.96.250
137.189.0.0
137.152.96.254
If I-n matches any directly connected network address
send datagram to destination over that network;
else if I-d appears as a host-specific route
route datagram as specified in the list;
else if I-n appears in routing table
route datagram as specified in the table;
else if a default route has been specified
route datagram to the default gateway;
else declare a routing error;
Page 21
TCP and UDP
End-to-End Transport Protocol
a. Connectionless packet delivery service (UDP)
b. Reliable Stream Transport service (TCP)
It features
a. Network technology independence
b. Universal interconnection
c. End-to-end acknowledgments
d. Application protocol standards
router or
gateway
physical
network
Its major applications
a. e-mail (SMTP)
b. file transfer (FTP)
c. remote login (TELNET)
d. web (HTTP)
Page 22
Relationship of layers and addresses in
TCP/IP
Page 23
End-to-end Transport
Header
Application
Application
Packet Header
Frame Header
Packet Data Area
User
Frame Data Area
Transport
Transport
Kernal
datagram or transport segment
Internet
Internet
Internet
packet
H/W
Interface
Interface
Interface
frame
Physical
network 1
Gateway G
Physical
network 1
Page 24
Internet Connection via TCP
Page 25
TCP Segment Format
0
10
4
24
16
31
DESTINATION PORT
SOURCE PORT
SEQUENCE NUMBER
ACKNOWLEDGEMENT NUMBER
HLEN
RESVD CODE BITS
WINDOW
URGENT POINTER
CHECKUM
OPTIONS (IF ANY)
PADDING
DATA
...
Segments for connection management, data transfer, and
reception acknowledgements.
Page 26
UDP Datagram Format
0
16
31
UDP SOURCE PORT
UDP DESTINATION PORT
UDP MESSAGE LENGTH
UDP CHECKSUM
DATA
...
UDP Message length in bytes (Minimum: 8)
UDP Checksum is optional (16-bit 1-complement of 1-complement sum)
To be transferred independently by IP packets.
Page 27
How can a server
serves multiple clients?
TCP assigns some well-known ports for communications
- server port defines the application.
- client port defines the client.
TELNET Client
TELNET Server
7
23
21
9
TELNET client
204 208
1048
magnet1
iestp10
137.189.96.31 binding: 23-204 137.189.96.210
Solution: Connection is identified by a pair of end-points
E.g., (137.189.96.31, 23) and (137.189.96,210, 204) Page 28
TCP Port numbers
Decimal
0
1
5
7
9
11
13
20
21
23
25
37
42
53
79
80
103
104
113
117
119
139
Keyword
-TCPMUX
RJE
ECHO
DISCARD
USERS
DAYTIME
FTP-DATA
FTP
TELNET
SMTP
TIME
NAMESERVER
DOMAIN
FINGER
HTTP
X400
X400SND
AUTH
UUCP-PATH
NNTP
NETBIOS-SSN
Description
Reserved
TCP multiplexer
Remote job entry
Echo
Discard
Active users
Daytime
File transfer protocol-data
File transfer protocol
Terminal connection
Simple mail transfer protocol
Time
Host name server
Domain name server
Finger
Web server
X.400 Mail Service
X.400 Mail Sending
Authentication Service
UUCP Path Service
USENET news transfer protocol
NETBIOS session service
Page 29
UDP Port numbers
Decimal
0
7
9
11
13
15
17
19
37
42
43
53
67
68
69
111
123
161
162
513
514
Keyword
-ECHO
DISCARD
USERS
DAYTIME
-QUOTE
CHARGEN
TIME
NAMESERVER
NICNAME
DOMAIN
BOOTPS
BOOTPC
TFTP
SUNRPC
NTP
-----
Description
Reserved
Echo
Discard
Active Users
Daytime
Who is up or NETSTAT
Quote of the Day
Character Generator
Time
Host Name Server
Who is
Domain Name Server
Bootstrap Protocol Server
Bootstrap Protocol Client
Trivial File Transfer
Sun Microsystems RPC
Network Time Protocol
SNMP net monitor
SNMP traps
Unix rwho daemon
System log
Page 30
FTP: File Transfer Protocol
user at a
terminal
user
interface
user
protocol
interpreter
file system
user
data transfer
function
Client
Commands
3 or 4 bytes NVT ASCII w/wo arguments
Replies
3-digit numbers, optional messages ASCII
server
control 21 protocol
connection interpreter
server
data 20
data transfer
connection function
file system
Server
Page 31
FTP commands (NVT ASCII)
Name
ABOR
Meaning
abort previous FTP command and any data transfer
LIST filelist
list files or directory
PASS password
password on server
PORT n1,n2,n3,...,n6 client IP address (n1..n4) and port (n5x256+n6)
QUIT
logoff from server
RETR filename
retrieve (get) a file
STOR filename
store (put) a file
SYST
server returns system type
TYPE type
specify file type:A (for ascii) I (for image)
USER username
username on server
Page 32
FTP commands and replies
/data/staff/pcwong> ftp -d magnet1
Connected to magnet1.
220 magnet1 FTP server (SunOS 4.1) ready.
Name (magnet1:pcwong): pcwong
---> USER pcwong
331 Password required for pcwong.
Password:
---> PASS rachel41
230 User pcwong logged in.
ftp> dir cantoni.1
---> PORT 137,189,96,21,11,33
200 PORT command successful.
---> LIST cantoni.1
150 ASCII data connection for /bin/ls (137.189.96.21,2849) (0 bytes).
-rw-r--r-- 1 pcwong staff
1953 Nov 10 18:16 cantoni.1
226 ASCII Transfer complete.•
remote: cantoni.1
65 bytes received in 0.11 seconds (0.56 Kbytes/s)
ftp> get cantoni.1
---> PORT 137,189,96,21,11,34
200 PORT command successful.
---> RETR cantoni.1
150 ASCII data connection for cantoni.1 (137.189.96.21,2850) (1953 bytes).
226 ASCII Transfer complete.
local: cantoni.1 remote: cantoni.1
2009 bytes received in 0.051 seconds (39 Kbytes/s)
Page 33
How do the layers work together?
Internet
User clicks on http://www.singtao.com/
Ethereal network analyzer captures all frames observed by its
Ethernet NIC
Sequence of frames and contents of frame can be examined in
detail down to individual bytes
Page 34
Top Pane
shows
frame/packet
sequence
Middle Pane
shows
encapsulation for
a given frame
Bottom Pane shows hex & Page
text35
TCP Segment
GET HTTP
Request
Page 36
Outline
TCP/IP Architecture
IP
TCP and UDP
Socket API
Page 37
Socket API
API (Application Programming Interface)
Berkeley UNIX Sockets API
Provides a standard set of functions that can be called by
applications
Abstraction for applications to send & receive data
Applications create sockets that “plug into” network
Applications write/read to/from sockets
Implemented in the kernel
Facilitates development of network applications
Hides details of underlying protocols & mechanisms
Also in Windows, Linux, and other OS’s
Page 38
Communications through
Socket Interface
Client
Socket
interface
Server
Application 1
Application 2
User
descriptor
Kernel
User
descriptor
Kernel
Socket
port number
Socket
interface
Socket
• Application references a
socket through a descriptor
• Socket bound to a port number
Underlying
communication
protocols
port number
Underlying
communication
protocols
Communications
network
Page 39
Socket Streams - mode of
service
Connection-oriented
First, setup connection
between two peer
application processes
Then, reliable bidirectional
in-sequence transfer of byte
stream (boundaries not
preserved in transfer)
Multiple write/read between
peer processes
Finally, connection release
Uses TCP
Connectionless
Immediate transfer of one
block of information
(boundaries preserved)
No setup overhead & delay
Destination address with
each block
Send/receive to/from
multiple peer processes
Best-effort service only
Possible out-of-order
Possible loss
Uses UDP
Page 40
Client & Server Differences
Server
Specifies well-known port # when creating socket
May have multiple IP addresses (net interfaces)
Waits passively for client requests
Client
Assigned ephemeral port #
Initiates communications with server
Needs to know server’s IP address & port #
DNS for URL & server well-known port #
Server learns client’s address & port #
Page 41
Connection Stream
Server
socket()
bind()
listen()
Client
accept()
blocks until server receives
a connect request from client
read()
write()
close()
socket()
connect negotiation
connect()
data
write()
data
read()
close()
Page 42
Figure 2.17
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()));
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = "Welcome "+ clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}
Java TCP Server
Page 43
Java TCP Client
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
String hostName= “localhost"; // or the server hostname
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket(hostName, 6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
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();
}
}
Page 44
Datagram Stream
Server
socket()
Client
socket()
bind()
bind()
recvfrom()
blocks until server
receives data from client
sendto()
data
sendto()
data
recvfrom()
close()
close()
Page 45
Figure 2.18
Java UDP Server
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);
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);
}
}
}
Page 46
Java UDP Client
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(localhost");
byte[] sendData = new byte[512];
byte[] receiveData = new byte[512];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
System.out.println("Length " + receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence =
new String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
Page 47
}
Thank You