Socket Programming - TCP
Socket Programming - TCP
TCP/IP
OSI & Internet protocol suite
3
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP/IP
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP or UDP
• At the internet layer, a destination address
identifies a host computer; no further distinction
is made regarding which process will receive the
datagram
• TCP or UDP add a mechanism that
distinguishes among destinations within a given
host, allowing multiple processes to send and
receive datagrams independently
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Why process is not the destination for a
message?
• Processes are created and destroyed dynamically
• A process can be repalced with a new process
without informing all senders
• Identify a destination by the function rather than
the process which implements it
• A process can handle multiple functions, so there
should a way to specify which one sender desires
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Protocol Ports
• Instead of thinking process as ultimate
destination, imagine that each machine contains
a set of abstract destination points called
protocol ports
• Each protocol port is identified by a positive
integer
• Operating systems provide some mechanism
that processes use, to specify a port.
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Port Numbers
• The port numbers are divided into three ranges by
Internet Assigned Numbers Authority
• The well-known ports: 0 through 1023. These port
numbers are controlled and assigned by the IANA.
• The registered ports: 1024 through 49151. These are not
controlled by the IANA, but the IANA registers and lists
the uses of these ports as a convenience to the
community.
• The dynamic or private ports, 49152 through 65535. The
IANA says nothing about these ports. These are what we
call ephemeral ports. (49152 is three-fourths of 65536.)
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Ports
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
UDP (User Datagram Protocol)
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
UDP header
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Some standard UDP based services and their
ports
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP
Transmission Control Protocol
• TCP provides connections between clients and
servers.
• TCP uses the connection, not the protocol port,
as its fundamental abstraction.
• Connections are identified by a pair of endpoints.
– Endpoint means (ip, port)
• TCP provides:
– Connection-oriented
– Reliable
– Full-duplex
– Byte-Stream
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Connection-Oriented
• Connection oriented means that a virtual connection is
established before any user data is transferred.
• A TCP client establishes a connection with a given
server, exchanges data with that server across the
connection, and then terminates the connection.
• If the connection cannot be established - the user
program is notified.
• If the connection is ever interrupted - the user
program(s) is notified.
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP Ports
• Interprocess communication via TCP is
achieved with the use of ports (just like
UDP).
• UDP ports have no relation to TCP ports
(different name spaces).
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP Segments
• TCP views the data stream as a sequence of
bytes that it divides into segments for
transmission. Segments carry varying sizes of
data.
• The chunk of data that TCP asks IP to deliver is
called a TCP segment.
• Each segment contains:
– data bytes from the byte stream
– control information that identifies the data bytes
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP Segment Format
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP Segments
• Segments are exchanged to establish connections,
transfer data, send acknowledgements, advertise
window sizes, and close connections.
• Because TCP uses piggybacking, acknowledgement can
be sent along with data
• TCP advertises how much data it is willing to accept
every time it sends segment by specifying its buffer size
in the WINDOW field
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP Connection Establishment
• Three-way handshake
• It accomplishes two important functions.
– It guarantees that both sides are ready to transfer data (and
that they know they are both ready)
– it allows both sides to agree on initial sequence numbers.
• Sequence numbers are sent and acknowledged during
the handshake. Each machine must choose an initial
sequence number at random that it will use to identify
bytes in the stream it is sending.
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP Connection Establishment
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP Connection Establishment
• A server accepts a connection.
Must be looking for new connections!
• A client requests a connection.
Must know where the server is!
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Connection Termination
• The TCP layer can send a RST segment that terminates
a connection if something is wrong.
• Usually the application tells TCP to terminate the
connection gracefully with a FIN segment.
• Either end of the connection can initiate termination.
• A FIN is sent, which means the application is done
sending data.
• The FIN is ACK’d.
• The other end must now send a FIN.
• That FIN must be ACK’d.
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Connection Termination
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP Connection State Diagram
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
What is the purpose of TIME_WAIT?
• Once a TCP connection has been terminated
(the last ACK sent) there is some unfinished
business:
– What if the ACK is lost? The last FIN will be resent
and it must be ACK’d.
– What if there are lost or duplicated segments that
finally reach the incarnation of the previous
connection after a long delay?
• The MSL is the maximum amount of time that
any given IP datagram can live in a network
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Outline
• Sockets
• TCP Client Server
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Sockets
OSI & Internet protocol suite
31
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP/IP & Sockets API
• TCP/IP does not include an API definition.
• There are a variety of APIs for use with TCP/IP:
o Sockets
o TLI, XTI
o Winsock
o MacTCP
• API should have the following functionalities
o Specify local and remote communication endpoints
o Initiate a connection
o Wait for incoming connection
o Send and receive data
o Terminate a connection gracefully
o Error handling
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Berkeley Sockets API
• First appeared in 42. BSD in 1983.
o Supported on every UNIX variant.
o WinSock API also follows socket API.
• Generic API:
o support for multiple communication domains which differ in
addressing methods.
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Socket Types
• Two types of sockets
o Stream Sockets
Reliable
Bidirectional
Byte-stream
Connection-oriented
Stream sockets operate in connected pairs. (local end point, remote
end point)
Internet Domain: TCP Socket
o Datagram Sockets
Message boundaries are preserved.
No reliability support: out of order, duplicates, or lost datagrams
Connectionless socket: no need to be connected to another socket.
Internet Domain: UDP Socket
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Socket Descriptor Data Structure
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Internet Domain & Sockets
• Endpoint is identified by ip address and port number.
o A socket needs to be bound to endpoints local and remote.
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Socket Pair
• The socket pair for a TCP connection is the four-tuple that
defines the two endpoints of the connection:
o the local IP address, local port, foreign IP address, and foreign port.
• A socket pair uniquely identifies every TCP connection on a
network.
• We can extend the concept of a socket pair to UDP, even
though UDP is connectionless.
• TCP connection for a ftp server:
o Server has two IP interfaces. * indicates any ip address/any port.
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP Connection
• A client 206.168.112.219 connects to 12.106.32.254 at port
21.
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP Connection
• Client opens one more connection to the same server.
What makes it a different TCP connection?
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Writing to TCP Socket
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Writing to UDP Socket
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Creating a Socket
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Specifying an Endpoint Address
• Remember that the sockets API is generic.
• There must be a generic way to specify endpoint
addresses.
• Internet Domain requires an IP address and a port number
for each endpoint address.
• Other domains (families) may use other schemes.
• Data types for specifying address structure
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Generic Socket Address Structure
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Specifying End Point Address
o Bind call binds an address to a socket
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Network Byte Order Functions
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
bind() Example
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
IPv4 Address Conversion
• The inet_pton() and inet_ntop() functions allow conversion
of both IPv4 and IPv6 addresses between binary form and
dotted-decimal or hex-string notation.
• The p in the names of these functions stands for
“presentation,” and the n stands for “network.” The
presentation form is a human-readable string.
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
bind() “well-known”
port
listen()
Client
accept()
socket()
(Block until connection) “Handshake”
connect()
Data (request)
write()
read()
Data (reply)
write()
read()
End-of-File close()
read()
• close()
Diagram Source: sjsu.edu Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP Client
PF_INET
PF_INET6 STREAM 0, used by
PF_UNIX DGRAM RAW socket
PF_X25 RAW
port
ephemeral port three way
addr ip addr
(routing)
sd = connect (sd, server_addr, addr_len); handshaking
Server
CONNECT actions
write (sd, *buff, mbytes); PORT#
IP-ADDR
1. socket is valid
2. fill remote endpoint addr/port
3. choose local endpoint add/port read (sd, *buff, mbytes);
4. initiate 3-way handshaking
disconnect
close (sd); sequence
1. Turn sd from
listen (sd, backlog); active to passive
2. Queue length
family
port
CONNECT
SOCKET ssd = accept (sd, *cliaddr, *len); three way
handshaking
addr
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
connect() - connect to server
• Errors
o If the server’s TCP response to client TCP’s SYN segment is RST,
then there is no process is waiting for incoming connections.
Hard error
o Three conditions that generate RST are
when a SYN arrives for a port that has no listening server
when TCP wants to abort an existing connection
when TCP receives a segment for a connection that does not exist
o If client’s SYN request elicits ICMP “destination unreachable”
message, kernal saves the message but keeps on sending the SYN
segment.
Soft error
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
listen() - change socket state to passive
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
listen()
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
accept() - return next completed connection
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
close() - close socket fd
• sockfd is socket descriptor from socket()
• closes socket for reading/writing
o returns (doesn’t block)
o attempts to send any unsent data
o socket option SO_LINGER
block until data sent
or discard any remaining data
o Returns -1 if error
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Descriptor Reference Counts
• For every socket a reference count is maintained, as to how
many processes are accessing that socket
• When close() is called on socket descriptor reference count
is decreased by 1
• When close() is called on socket descriptor, TCP 4 packet
termination sequence will be initiated only if the reference
count goes to zero.
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
getsockname() and getpeername() Functions
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP Echo Client
• TCP client and server using echo protocol
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP Concurrent Server
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP Concurrent Server
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
str_echo function
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
TCP Concurrent Server
• Handling zombies
o while ( (pid = waitpid(-1, &stat, WNOHANG)) > 0) in SIGCHLD
signal handler
• Handling interrupted system calls
o when writing network programs that catch signals, we must be
cognizant of interrupted system calls, and we must handle them
o Slow system call is any system call that can block forever
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Handling interrupted system calls
for ( ; ; ) {
clilen = sizeof (cliaddr);
if ( (connfd = accept (listenfd, (SA *)
&cliaddr, &clilen)) < 0) {
if (errno == EINTR)
continue; /* back to for () */
else
err_sys ("accept error");
}
• Another option:
o Use sigaction() with SA_RESTART flag.
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Termination of Server Process
• FIN is sent to client
• Client tcp sends ACK to server
• What if client application doesn’t take not of it, and sends
data to server?
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
SIGPIPE Signal
• When a process writes to a socket that has received an
RST, the SIGPIPE signal is sent to the process.
• The default action of this signal is to terminate the process,
so the process must catch the signal to avoid being
involuntarily terminated.
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Crashing of Server Host
• Nothing is sent to client
• Client will try to reach the host, but will get errors such as
ETIMEDOUT, EHOSTUNREACH, ENETWORKUNREACH
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Crashing and Rebooting of Server Host
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Shutdown of Server Host
• Init sends SIGTERM to all processes
• Then sends SIG KILL to all processes
• Fin is sent to the client
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Exercise
• Write a TCP client and server that fulfills the following requirements.
• Server.c:
server should take port number on command-line and listen on that port.
server should create a child to handle a new client.
When a client sends a command such as 'ps', server should execute the
command and send the output to the client.
it should take care of zombies.
• Client.c:
o client takes ip address and port number of the server on command-line.
o client sets up a connection to the server.
o client takes a command from the user and sends it to the server.
o client waits for the reply and prints the reply on the standard output.
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Acknowledgements
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
Q&A
Network Programming by Dr. K Hari Babu, CSIS Dept. BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Thank You