















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An in-depth exploration of sockets, their role as end-points for ip network connections, and the differences between tcp and udp protocols. Topics include socket basics, socket details, socket options, and the use of sockets in client-server applications. Port numbers, socket addresses, and socket functions such as bind(), listen(), accept(), and close() are also covered.
Typology: Slides
1 / 23
This page cannot be seen from the preview
Don't miss anything!
-^ setsockopt(), getsockopt()
Docsity.com
(Block until connection
)^
“Handshake”
recv()send()
Data (request)Data (reply)
close()
End-of-File
recv()close()
“well-known”
port
socket()
int socket(int
family
, int
type
, int
protocol
-^ AF_INET (IPv4), AF_INET6 (IPv6), AF_LOCAL (local Unix),–^ AF_ROUTE (access to routing tables), AF_KEY (new, for encryption)
-^ SOCK_STREAM (TCP), SOCK_DGRAM (UDP)–^ SOCK_RAW (for special IP packets, PING, etc. Must be root)
If (( sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)err_sys (“socket call error”);
Docsity.com
listen()
Announce willingness to accept connections, givequeue size, change socket state for TCP server.
accept()
)^ sockfd
is socket descriptor from
socket()
)^ cliaddr
and
addrlen
return protocol address from client
)^ returns brand new descriptor, created by OS )^ if used with
fork()
,^ can create concurrent server (more
later) ) Example:sfd = accept (s, NULL, NULL);if (sfd == -1) err_sys (“accept error”); int accept(int
sockfd
, struct sockaddr
cliaddr
**socklen_t ***
addrlen
Return next completed connection.
Docsity.com
connect()
port number
and
IP address
bind()
if ( connect (sockfd, (struct sockaddr *) &servaddr, sizeof (servaddr)) != 0)err_sys(“connect call error”); int connect(int
sockfd
, const struct sockaddr
***** servaddr
, socklen_t
addrlen
Connect to server.
-^ MSG_DONTWAIT (this send non-blocking)–^ MSG_OOB (out of band data, 1 byte sent ahead)–^ MSG_WAITALL (don’t give me less than max)–^ MSG_DONTROUTE (bypass routing table)
Text segment
sock = socket()/ setup socket /while (1) {
newsock = accept(sock)fork()if child
read(newsock)until exit
Parent int sock;int newsock;
Child int sock;int newsock;
UDP Client-Server
Server socket()bind()recvfrom()
Client socket()sendto()recvfrom()
(Block until receive datagram)^ sendto()
Data (request) Data (reply)
close()
“well-known”port
close()
fork()
for concurrent servers!
Called
iterative
server