Lab 4 sockets_lab
Lab 4 sockets_lab
• Create TCP and UDP sockets using the POSIX Socket API
1
Introduction
Protocol Families - TCP/IP
2
TCP/IP
3
Internet Protocol (IP)
4
TCP vs UDP
6
The POSIX Socket API
The POSIX Socket API
What is POSIX?
Portable Operating System Interface, is a family of standards
specified by the IEEE for maintaining compatibility between
operating systems.
7
Creating a Socket
Prototype
8
Creating a Socket
Prototype
• Possible types:
• SOCK STREAM provides reliable two way
connection-oriented byte streams (TCP)
• SOCK DGRAM provides connection-less, unreliable messages
of fixed size (UDP)
• protocol depends on the domain and type parameters. In
most cases 0 can be passed
9
Creating a Socket
SOCK STREAM
Sockets of this type are full-dublex data streams that do not rely
on a known data length. Before sending or receiving the socket
must be in a connected state. To send and receive data, send()
and recv() system calls may be used. By default, socket of this
type are blocking, meaning that a call of recv() may block until
data arrive from the other side. At the end, close() should be used
to properly indicate the end of the communication session.
SOCK DGRAM
This kind of sockets allowing to send messages of a specific size
without the guarantee that they will be received from the other
side. To send and receive messages sendto() and recvfrom() calls
may be used.
10
TCP Sockets
TCP: Creating the socket
11
Bind a Socket
Prototype
12
TCP: Bind the socket
13
Listening for incoming connections
Prototype
Hint!
For debugging you can use the netstat utility!
or
14
Trivia
Think!
Which of the calls of the previous slides cause data to be
transmitted or received over the network?
15
Trivia
Think!
Which of the calls of the previous slides cause data to be
transmitted or received over the network? NONE!
16
TCP: Accepting connections
Prototype
Prototype
18
TCP: Sending Data
Prototype
Question!
Does this call block?
19
TCP: Sending Data
Prototype
Question!
Does this call block? YES!
19
TCP: Receiving Data
Prototype
20
TCP Overview 1/3
21
TCP Overview 2/3
22
TCP Overview 3/3
• TCP Server:
• 1. using create(), Create TCP socket.
• 2. using bind(), Bind the socket to server address.
• 3. using listen(), put the server socket in a passive mode,
where it waits for the client to approach the server to make a
connection
• 4. using accept(), At this point, connection is established
between client and server, and they are ready to transfer data.
• 5. Go back to Step 3.
• TCP Client:
• 1. Create TCP socket.
• 2. Connect newly created client socket to server.
23
Client - Server Communication
24
UDP Sockets
UDP: Creating the socket
25
UDP: Connection-less
UDP is connection-less!!!
No need to call accept() or connect()!!!
26
UDP: Receiving data
Prototype
27
UDP: Problems at receiving
28
UDP: Problems at receiving
28
UDP: Problems at receiving
28
UDP: Problems at receiving
28
UDP: Sending data
Prototype
29
UDP: Sending data
Prototype
Trivia!
Does sendto() block?
29
UDP: Sending data
Prototype
Trivia!
Does sendto() block? NO!
29
Endianness
Endianness
30
Endianness
Trivia!
When sending large strings do we have to convert
in Network Byte Order?
30
Endianness
Trivia!
When sending large strings do we have to convert
in Network Byte Order? NO!
30
Customize sockets
Prototype
31
Accurate time measurements
• Solution:
• clock gettime()
• Use the CLOCK MONOTONIC RAW option
32
Useful man pages
• socket(7)
• ip(7)
• setsockopt(3p)
• tcp(7)
• udp(7)
33
Questions??
34