Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Sockets: A Deep Dive into TCP and UDP for Network Connections, Slides of Social Work

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

2012/2013

Uploaded on 04/29/2013

awais
awais 🇮🇳

4.3

(15)

155 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to LAN/WAN
Sockets
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Understanding Sockets: A Deep Dive into TCP and UDP for Network Connections and more Slides Social Work in PDF only on Docsity!

Introduction to LAN/WAN

Sockets

Outline

)^ Socket basics )^ TCP sockets )^ Socket details )^ Socket options )^ Final notes

Socket Basics

)^ An

end-point

for a IP network connection

  • what the application layer “plugs into”– programmer cares about ApplicationProgramming Interface (API)

)^ End point determined by two things:

  • Host address: IP address is

Network Layer

  • Port number: is

Transport Layer

)^ Two end-points determine a connection: socketpair

  • ex: 206.62.226.35,p21 + 198.69.10.2,p1500– ex: 206.62.226.35,p21 + 198.69.10.2,p

Ports

)^ Numbers (vary in BSD, Solaris):

  • 0-1023 “reserved”, must be root– 1024 – 49151 (registered with IANA)– 49152 – 65535 “ephemeral”

)^ /etc/services:

ftp 21/tcptelnet 23/tcpfinger 79/tcpsnmp 161/udp

Transport Layer

)^ UDP: User Datagram Protocol

  • no acknowledgements– no retransmissions– out of order, duplicate possible– connectionless

)^ TCP: Transmission Control Protocol

  • reliable (in order, all arrive, no duplicates)– flow control– connection– duplex

Socket Details

TCP/IP Sockets in C: Practical Guide for Programmers

M J Donahoo and K Calvert

©2001 Morgan Kaufmann

Sections 6.1.3-6.1.

of text

)^ Socket address structure )^ TCP client-server )^ UDP client server )^ Misc stuff

-^ setsockopt(), getsockopt()

Docsity.com

TCP Client-Server

Server socket()bind()listen()accept()

Client socket()connect()send() recv()

(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

  • Create a socket, giving access to transport layer service. )^ family

is one of

-^ AF_INET (IPv4), AF_INET6 (IPv6), AF_LOCAL (local Unix),–^ AF_ROUTE (access to routing tables), AF_KEY (new, for encryption)

)^ type

is one of

-^ SOCK_STREAM (TCP), SOCK_DGRAM (UDP)–^ SOCK_RAW (for special IP packets, PING, etc. Must be root)

)^ protocol

is 0 (used for some raw socket options)

)^ upon success returns socket descriptor

  • like file descriptor => -1 if failure )^ Example:

If (( sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)err_sys (“socket call error”);

Docsity.com

listen()

)^ sockfd

is socket descriptor from

socket()

)^ backlog

is maximum number of

incomplete

int^ connections– historically 5– rarely above 15 on a even moderate web server! )^ Sockets default to active (for client) )^ Example:If (listen (sd, 2) != 0)errsys (“listen call error”);

listen(int

sockfd

,^ int

backlog

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()

)^ sockfd

is socket descriptor from

socket()

)^ servaddr

is a pointer to a structure with:

  • Server

port number

and

IP address

  • must be specified (unlike

bind()

)^ addrlen

is length of structure

)^ client doesn’t need

bind()

  • OS will pick ephemeral port

)^ returns socket descriptor if ok, -1 on error )^ Example:

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.

Sending and Receiving

int recv(int

sockfd

, void *

buff

size_t

mbytes

, int

flags

int send(int

sockfd

, void *

buff

size_t

mbytes

, int

flags

)^ Same as

read()

and

write()

but for

flags

)^ flags

examples (see man pages)

-^ 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)

Concurrent TCP Server

)^ Close

sock

in child,

newsock

in parent

)^ Reference count for socket descriptor

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

  • No “handshake”- No simultaneous

close()

  • Note: usually

fork()

for concurrent servers!

Called

iterative

server