Вы находитесь на странице: 1из 24

NETWORK PROGRAMMING

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.

y The : 1024 through 49151. These are not controlled by


the
h IANA,
IANA b but the
h IANA registers
i and
d lists
li the
h uses off these
h ports as a
convenience to the community. For example, ports 6000 through 6063 are
assigned for an X Window server for both protocols (TCP/ UDP), even
though all implementations currently use only TCP.

y The , 49152 through 65535. The IANA says


nothing about these ports. These are what we call ephemeral ports. (The
magic
i number
b 49152 iis th
three-fourths
f th off 65536.)
65536 ) 3
PORT NUMBERS

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

| A uniquely identifies every TCP


connection on a network.

| 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

| When we specify the local IP address as an asterisk, it is called


the wildcard character.
| The wildcard local address is the "any" choice. The wildcard
address is specified by setting the IP address in the socket
address structure to INADDR_ANY
INADDR ANY before calling bind bind. 6
TCP PORT NUMBERS
AND CONCURRENT SERVERS
206.62.226.35
206.62.226.66 198.69.10.2

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}

Figure 2.8 Connection request from client to server

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}

Figure 2.10 Second client connection with same server


8
2.9: BUFFER SIZES AND LIMITATIONS
| Maximum size of IPv4 => 65535 byte
| Maximum size of IPv6 => 65575 byte
| MTU(maximum
MTU( i ttransmit
it unit)
it) =>
>
fragmentation
| DF (don’t fragment)
| IPv4 and IPv6 define a minimum
reassembly buffer size.
| TCP has MSS(maximum
( segment
g
size)
9
STANDARD INTERNET SERVICE

Notice: TCP and UDP port number is same.

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

User Application protocol


Web Web A li ti layer
Application l
process Client server

TCP protocol
TCP TCP transport layer

Protocol stack
within kernel
IP IP protocol IP network layer

Ethernet Ethernet protocol Ethernet datalink layer


driver driver
Actual flow between client and server
14

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

Resulting TCP connection identified by


I will talk to
(my-machine:a,
(my machine:a, mail.yahoo.com:b)
my-machine, 21

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

| Socketsis just a convenient interface for the


processes on different hosts to communicate.

| Justlike if you want to write to the hard disk,


you can operate
y p the hard disk directly,
y, but it is
much more convenient to open a file and write
to the file using the interface provided by the
system. 23
SOCKETS: CONCEPTUAL VIEW

24

Вам также может понравиться