Trọn bộ Sline Introduction to Sockets Programming in C using TCP/IP - Đồ họa máy tính | Đại học Bách Khoa, Đại học Đà Nẵng

Trọn bộ Sline Introduction to Sockets Programming in C using TCP/IP - Đồ họa máy tính | Đại học Bách Khoa, Đại học Đà Nẵng giúp sinh viên tham khảo, ôn luyện và phục vụ nhu cầu học tập của mình cụ thể là có định hướng, ôn tập, nắm vững kiến thức môn học và làm bài tốt trong những bài kiểm tra, bài tiểu luận, bài tập kết thúc học phần, từ đó học tập tốt và có kết quả cao cũng như có thể vận dụng tốt những kiến thức mình đã học

Introduction to
Sockets Programming in C
using TCP/IP
Professor: Panagiota Fatourou
TA: Eleftherios Kosmas
CSD - May 2012
Introduction
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 2
Computer Network
hosts, routers,
communication channels
Hosts
Hosts
Hosts
HostsHosts run applications
Routers
Routers
Routers
RoutersRouters forward information
Packets
Packets
Packets
PacketsPackets: sequence of bytes
contain control information
e.g. destination host
Protocol
Protocol
Protocol
ProtocolProtocol is an agreement
meaning of packets
structure and size of packets
e.g. Hypertext Transfer Protocol
(HTTP)
Host
Router
Communication
channel
Protocol Families - TCP/IP
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 3
Several protocols for different problems
)
Protocol Su
Protocol Su
Protocol Su
Protocol SuProtocol Su
ites
ites
ites
ites ites or
Protocol Fa
Protocol Fa
Protocol Fa
Protocol FaProtocol Fa
milies:
milies:
milies:
milies: milies: TCP/IP
TCP/IP provides
end-to-end
end-to-end
end-to-end
end-to-endend-to-end connectivity specifying how data
should be
formatted,
addressed,
transmitted,
routed, and
received at the destination
can be used in the internet and in stand-alone private networks
it is organized into
la
la
la
lala
yers
yers
yers
yersyers
TCP/IP
* image is taken from “http://en.wikipedia.org/wiki/TCP/IP_model”
*
Communication
Communication
Communication
Communication Communication
Channels
Channels
Channels
ChannelsChannels
Network Layer
Network Layer
Network Layer
Network Layer Network Layer
IP
IP
IP
IPIP
Transport
Transport
Transport
TransportTransport
a
a
a
aa
L
L
L
L L
yer
yer
yer
yer yer
TCP o
TCP o
TCP o
TCP oTCP o
r U
r U
r U
r Ur U
DP
DP
DP
DPDP
FTP, SMTP, …
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 4
Internet Protocol (IP)
provides a
datagram
datagram
datagram
datagramdatagram service
packets are handled and delivered
independently
best-effort
best-effort
best-effort
best-effortbest-effort protocol
may loose, reorder or duplicate
packets
each packet must contain an
IP
IP
IP
IP IP
address
address
address
address address of its destination
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 5
Addresses - IPv4
7 24
Class A: 0 Network ID Host ID
14 16
Class B: 1 0 Network ID Host ID
21 8
Class C: 1 1 0 Network ID Host ID
28
Class D (multicast): 1 1 1 0 Multicast address
27
Class E (reserved): 1 1 1 1 unused0
1.0.0.0 to
127.255.255.255
128.0.0.0 to
191.255.255.255
192.0.0.0 to
223.255.255.255
224.0.0.0 to
239.255.255.255
240.0.0.0 to
255.255.255.255
Range of addresses
The bits of an IPv4 address are broken into
32
32
32
3232
4 octets
4 octets
4 octets
4 octets4 octets, or 8 bit fields
(0-255 value in decimal notation).
For networks of different size,
the first one (for large networks) to three (for small networks) octets can
be used to identify the
network
network
network
networknetwork, while
the rest of the octets can be used to identify the
node
node
node
nodenode on the network.
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 6
Local Area Network Addresses - IPv4
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 7
TCP vs UDP
Both use
port
port
port
portport
numbers
numbers
numbers
numbers numbers
application-specific construct serving as a communication endpoint
16-bit unsigned integer, thus ranging from 0 to 65535
) to provide
end-to-
end-to-
end-to-
end-to-end-to-
end
end
end
end end transport
UDP: User Datagram Protocol
no acknowledgements
no retransmissions
out of order, duplicates possible
connectionless, i.e., app indicates destination for each packet
TCP: Transmission Control Protocol
reliable
byte-stream channel
byte-stream channel
byte-stream channel
byte-stream channelbyte-stream channel (in order, all arrive, no duplicates)
similar to file I/O
flow control
connection-oriented
bidirectional
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 8
TCP vs UDP
TCP is used for services with a large data capacity, and a persistent
connection
UDP is more commonly used for quick lookups, and single use
query-reply actions.
Some common examples of TCP and UDP with their default ports:
DNS lookup UDP 53
FTP TCP 21
HTTP TCP 80
POP3 TCP 110
Telnet TCP 23
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 9
Berkley Sockets
Universally known as
Sockets
Sockets
Sockets
SocketsSockets
It is an abstraction through which an
application may send and receive data
Provide
generic access
generic access
generic access
generic accessgeneric access to interprocess
communication services
e.g. IPX/SPX, Appletalk, TCP/IP
Standard API for networking
Host
Host
Host
HostHost
Applic
Applic
Applic
ApplicApplic
ation
ation
ation
ationation
Socket
Socket
Socket
SocketSocket
TCP
TCP
TCP
TCPTCP
IP
IP
IP
IPIP
Host
Host
Host
HostHost
Applic
Applic
Applic
ApplicApplic
ation
ation
ation
ationation
Socket
Socket
Socket
SocketSocket
TCP
TCP
TCP
TCPTCP
IP
IP
IP
IPIP
Router
Router
Router
RouterRouter
IP
IP
IP
IPIP
Channel
Channel
Channel
ChannelChannel
Channel
Channel
Channel
ChannelChannel
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 10
Sockets
Uniquely identified by
an internet address
an end-to-end protocol (e.g. TCP or UDP)
a port number
Two types of (TCP/IP) sockets
Stream
Stream
Stream
StreamStream sockets (e.g. uses TCP)
provide reliable byte-stream service
Datagram
Datagram
Datagram
DatagramDatagram sockets (e.g. uses UDP)
provide best-effort datagram service
messages up to 65.500 bytes
Socket extend the convectional UNIX I/O facilities
file descriptors for network communication
extended the read and write system calls
0
1
2
internal data
structure for file 1
Family: PF_INET
Service: SOCK_STREAM
Local_IP:
Remote_IP:
Remote_Port:
Local_Port:
Descriptor Table
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 11
Sockets
TCP
UDP
IP
1
2
65535
1 2
65535
Applications
TCP sockets
UDP sockets
TCP ports UDP ports
Descriptor
references
Sockets bound to ports
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 12
Socket Programming
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 13
Client-Server communication
Server
Server
Server
Server Server
passively waits for and responds to clients
passive
passive
passive
passivepassive socket
Client
Client
Client
ClientClient
initiates the communication
must know the address and the port of the server
active
active
active
activeactive socket
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 14
Sockets - Procedures
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas 15
| 1/200

Preview text:

Introduction to
Sockets Programming in C using TCP/IP Professor: Panagiota Fatourou TA: Eleftherios Kosmas CSD - May 2012 Introduction  Computer Network Host  hosts, routers, communication channels Router  Ho H s o t s s t s run applications  Ro R ut o e ut r e s r s forward information Communication  Pac Pa k c e k t e s t : sequence of bytes channel  contain control information  e.g. destination host  Pro Pr t o o t c o o c l o l is an agreement  meaning of packets  structure and size of packets
e.g. Hypertext Transfer Protocol (HTTP) CS556 - Distributed Systems
Tutorial by Eleftherios Kosmas 2 Protocol Families - TCP/IP 
Several protocols for different problems ) Pro Pr t o o t c o o c l o l S u S it i e t s e s or Pro Pr t o o t c o o c l o l F a F mi m li i e li s e : s :TCP/IP  TCP/IP provides en e d n - d t - o t - o e - n e d n
d connectivity specifying how data should be  formatted,  addressed,  transmitted,  routed, and  received at the destination 
can be used in the internet and in stand-alone private networks  it is organized into layer ye s r CS556 - Distributed Systems
Tutorial by Eleftherios Kosmas 3 TCP/IP * FTP, SMTP, … Tr T a r n a s n p s o p r o t r L a L ye y r e r TCP T CP o r r U DP Ne N t e wo t r wo k r k L a L y a e y r e r IP I Co C m o m m u m n u i n c i a c t a i t o i n o n Cha Ch n a n n e n l e s l
* image is taken from “http://en.wikipedia.org/wiki/TCP/IP_model” CS556 - Distributed Systems
Tutorial by Eleftherios Kosmas 4 Internet Protocol (IP)  provides a da d t a a t gr a a gr m a m service 
packets are handled and delivered independently  bes be t s - t e - f e f f o f r o t r t protocol 
may loose, reorder or duplicate packets 
each packet must contain an IP I ad a d d r d e r s e s s s of its destination CS556 - Distributed Systems
Tutorial by Eleftherios Kosmas 5 Addresses - IPv4  The 32 3
2 bits of an IPv4 address are broken into 4 4 o c o t c e t t e s t , or 8 bit fields
(0-255 value in decimal notation). 
For networks of different size, 
the first one (for large networks) to three (for small networks) octets can be used to identify the ne n t e w t o w r o k r , while 
the rest of the octets can be used to identify the nod no e d e on the network. Range of addresses 7 24 1.0.0.0 to Class A: 0 Network ID Host ID 127.255.255.255 14 16 128.0.0.0 to Class B: 1 0 Network ID Host ID 191.255.255.255 21 8 192.0.0.0 to Class C: 1 1 0 Network ID Host ID 223.255.255.255 28 224.0.0.0 to Class D (multicast): 1 1 1 0 Multicast address 239.255.255.255 27 240.0.0.0 to Class E (reserved): 1 1 1 1 0 unused 255.255.255.255 CS556 - Distributed Systems
Tutorial by Eleftherios Kosmas 6
Local Area Network Addresses - IPv4 CS556 - Distributed Systems
Tutorial by Eleftherios Kosmas 7 TCP vs UDP  Both use por po t r n um n be um r be s r 
application-specific construct serving as a communication endpoint 
16-bit unsigned integer, thus ranging from 0 to 65535 ) to provide end e - nd t - o t - o end e nd transport  UDP: User Datagram Protocol  no acknowledgements  no retransmissions 
out of order, duplicates possible 
connectionless, i.e., app indicates destination for each packet 
TCP: Transmission Control Protocol  reliable by b t y e t - e s - t s r t e r a e m a m c h c a h n a n n e n l e
l (in order, all arrive, no duplicates)  similar to file I/O  flow control  connection-oriented  bidirectional CS556 - Distributed Systems
Tutorial by Eleftherios Kosmas 8 TCP vs UDP 
TCP is used for services with a large data capacity, and a persistent connection 
UDP is more commonly used for quick lookups, and single use query-reply actions. 
Some common examples of TCP and UDP with their default ports: DNS lookup UDP 53 FTP TCP 21 HTTP TCP 80 POP3 TCP 110 Telnet TCP 23 CS556 - Distributed Systems
Tutorial by Eleftherios Kosmas 9 Berkley Sockets  Universally known as So S c o k c e k t e s t 
It is an abstraction through which an
application may send and receive data  Provide generic a generic c a c c e c s e s s s to interprocess communication services 
e.g. IPX/SPX, Appletalk, TCP/IP  Standard API for networking App Ap l p i l c i at a i t o i n o App Ap l p i l c i at a i t o i n o So S c o k c e k t e So S c o k c e k t e TC T P C TC T P C IP I Ch C a h n a n n e n l I el l P I Ch C a h n a n n e n l e IP I Ho H s o t s Ro R u o t u e t r e Ho H s o t s CS556 - Distributed Systems
Tutorial by Eleftherios Kosmas 10 Sockets  Uniquely identified by  an internet address 
an end-to-end protocol (e.g. TCP or UDP)  a port number  Two types of (TCP/IP) sockets Descriptor Table internal data  St S r t e r a e m a m sockets (e.g. uses TCP) 0 structure for file 1 1 
provide reliable byte-stream service 2  Da D t a a t g a r g a r m a m sockets (e.g. uses UDP) Family: PF_INET Service: SOCK_STREAM 
provide best-effort datagram service Local_IP:  messages up to 65.500 bytes Remote_IP: Local_Port: 
Socket extend the convectional UNIX I/O facilities Remote_Port: … 
file descriptors for network communication 
extended the read and write system calls CS556 - Distributed Systems
Tutorial by Eleftherios Kosmas 11 Sockets Descriptor references Applications TCP sockets UDP sockets Sockets bound to ports TCP ports 1 2 65535 1 2 65535 UDP ports TCP UDP IP CS556 - Distributed Systems
Tutorial by Eleftherios Kosmas 12 Socket Programming CS556 - Distributed Systems
Tutorial by Eleftherios Kosmas 13 Client-Server communication  Se S r e v r e v r e r 
passively waits for and responds to clients  pa p s a s s i s v i e v e socket  Cli C e li n e t n  initiates the communication 
must know the address and the port of the server  ac a t c i t v i e v e socket CS556 - Distributed Systems
Tutorial by Eleftherios Kosmas 14 Sockets - Procedures CS556 - Distributed Systems
Tutorial by Eleftherios Kosmas 15