N P CSC 341 CSC-341: Etwork Rogramming
N P CSC 341 CSC-341: Etwork Rogramming
CSC 341
CSC-
1
Instructor:
Junaid Tariq,
Lecturer,
Department of Computer Science
L
Lecture 8
2
PORT NUMBERS
| TCP / UDP use 16bit port number.
y 65536 (1-65535)
| Clients use ephemeral ports, that is, short lived ports.
| The port numbers are divided into three ranges:
y The : 0 through 1023. These port numbers are
g
controlled and assigned by
y the IANA ((Internet Assigned
g Numbers
Authority). For example, port 80 is assigned for a Web server.
4
SOCKET PAIR
| The socket p
pair for a TCP connection is the four-tuple
p
that defines the two endpoints of the connection:
y The local IP address, local port, foreign IP address, and
foreign port.
port
| The two
Th t values
l that
th t identify
id tif eachh endpoint,
d i t an IP
address and a port number, are often called a .
5
TCP PORT NUMBERS
AND CONCURRENT SERVERS
connection request to
server client
206 62 226 35 port 21
206.62.226.35,
listening socket (* .21, * .* ) { 198.69.10.2.1500,
206.62.226.35.21}
206.62.226.35
206.62.226.66 198.69.10.2
server client
listening socket (* .21, * .* ) { 198.69.10.2.1500,
on 206.62.226.35.21}
e c ti
nn
fork co
server
(child)
connected
socket { 206.62.226.35.21,
198.69.10.2.1500}
7
Figure 2.9 Concurrent server has child handle client
TCP PORT NUMBERS
AND CONCURRENT SERVERS
206.62.226.35
206.62.226.66 198.69.10.2
server client1
listening socket (* .21, * .* ) { 198.69.10.2.1500,
io n
ec t 206.62.226.35.21}}
nn
fork co
server
client2
(child1)
connected
socket { 206.62.226.35.21, { 198.69.10.2.1500,
io n
198.69.10.2.1500} ec t 206.62.226.35.21}
nn
co
server
(child2)
connected
socket { 206.62.226.35.21,
198.69.10.2.1501}
10
PROTOCOL USAGE BY COMMON INTERNET
APPLICATION
11
CHAPTER 1:
Introductio
n
12
INTRODUCTION
| Client / Server
Communication link
Client Server
Figure 1.1
1 1 Network
Net ork application : client and server
ser er
Client
...
Client Server
...
Client
Figure 1.2 Server handling multiple clients at the same time. 13
INTRODUCTION (CONTINUED)
| Example : Client and Server on the same Ethernet
communication using TCP
TCP protocol
TCP TCP transport layer
Protocol stack
within kernel
IP IP protocol IP network layer
Ethernet
Figure 1.3 Client and server on the same Ethernet communicating using TCP
INTRODUCTION (CONTINUED)
| Example : Client and Server on different LANs
connected through WAN.
client server
application application
Host Host
with with
TCP/IP TCP/IP
LAN LAN
router router
WAN
router router router router
15
Figure 1.4 Client and server on different LANs connected through a WAN
TCP CONNECTION
socket
bind
listen
socket
SYN J
connect connection added to
SYN K incomplete queue
client
ACK J+1 server
connect
completes
connection moved
ACK K+1
to complete queue
16
accept
TCP CONNECTION socket
bind
socket
k
listen
connect
server
client
accept
write
read
write
read
close close
l 17
SOCKET FUNCTIONS
TCP Server
socket()
Well known port bind()
TCP Client
listen()
socket() accept()
3WHS
connect() Blocks until connection from client
read()
write() Data
write()
read() Data
read()
()
close() E d off Data
End D t Indication
I di ti 18
close()
UDP CONNECTION
socket
k
socket bind
server
sendto
recvfrom
client
sendto
recvfrom
close
l 19
20
CLIENT-SERVER CONNECTION
I am
Talk to
mail.yahoo.com,
mail.yahoo.com, mail.yahoo.com
my-machine port b
port b
I accept
connections
client server
port a
CLIENT-SERVER CONNECTION
I am
Talk to
mail.yahoo.com,
mail.yahoo.com, mail.yahoo.com
my-machine port b
port b
11. socket()
k t()
4. socket() 2. bind()
5. connect()
I accept
connections
client server 3.
3 listen()
7. write() / sendto()
8. read() / recvfrom() I will talk to
6. accept()
9 close()
9. l () / shutdown()
h td () my-machine, 22
port a
BSD SOCKET API
| Introduced in 1981 BSD 4.1 UNIX
24