TCP Server
socket()
bind()
listen()
accept()
TCP Client
socket()
connect()
Connection Established
Block until connection from client
write()
read()
read()
Process Request
write()
close() read()
close()
Data Request
Data (reply)
end-of-file notification
Socket Function
• To perform network I/O .
• Family specifies the protocol family.
 The connect function is used by a TCP client to
establish a connection with a TCP server:
 Sockfd is a socket descriptor returned by the socket
function
 Second & Third arguments are a pointer to the socket
address structure and its size.
The socket address structure must contain –
 IP address
 Port number
The client does not have to call bind.
The kernel chooses both an temporary port
and the source IP address if necessary.
Connection Error
ClientTCP
No Response
ETIMEDOUT
ECONNREFUSED Server’s response to client’s SYN is a reset
EHOSTUNREACH Host is down.
• Assigns a local protocol address to a socket.
• With IP, the protocol address is the combination of 32-bit IPv4 or
128-bit IPv6 address, along with a 16-bit TCP or UDP port
number.
#include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *myaddr,
socklen_t addrlen);
•Servers bind to their well-known port when they
start.
•A process can bind a specific IP address to its socket.
•A client does not bind an IP address to its socket .
• EACCES The address is protected.
• EADDRINUSE The given address is already in use.
• EBADF The sockfd is not a valid descriptor.
• EINVAL The socket is already bound to an address.
 The listen function is called only by a TCP server and it performs two
actions:
• Converts an unconnected (active) socket into a passive socket.
• Specifies the maximum number of connections .
#include <sys/socket.h>
int listen(int sockfd, int
backlog);
• Called after both the socket and bind function .
• Backlog - for a given listening socket, the kernel maintains 2
queues:
• An incomplete connection queue - Contains an entry for each
SYN that has arrived from a client .
• A completed connection queue, entry for each client with whom
3-way handshake has completed.
• Accept is called by a TCP server
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr
*cliaddr, socklen_t *addrlen);
• The cliaddr and addrlen args are used to return
the protocol address .
• Fork() function is the only way in Unix to create a new process
#include <unistd.h>
pid_t fork(void);
• Called once but returnsTWICE
• Once in the parent process (returns child process id),
• and once in the child process (return of 0)
• Used for multitasking .
• The process want to handle another program .
• Used when multiple clients are accessing a single
server .
• fork() is used in such cases to handle each client.
• Used to close a socket and terminate a TCP
connection
#include <unistd.h>
int close(int sockfd);
• Socket descriptor is no longer usable to the app
process.
Elementary TCP Sockets

Elementary TCP Sockets

  • 2.
  • 3.
  • 4.
    Socket Function • Toperform network I/O . • Family specifies the protocol family.
  • 5.
     The connectfunction is used by a TCP client to establish a connection with a TCP server:  Sockfd is a socket descriptor returned by the socket function  Second & Third arguments are a pointer to the socket address structure and its size.
  • 6.
    The socket addressstructure must contain –  IP address  Port number The client does not have to call bind. The kernel chooses both an temporary port and the source IP address if necessary.
  • 7.
    Connection Error ClientTCP No Response ETIMEDOUT ECONNREFUSEDServer’s response to client’s SYN is a reset EHOSTUNREACH Host is down.
  • 8.
    • Assigns alocal protocol address to a socket. • With IP, the protocol address is the combination of 32-bit IPv4 or 128-bit IPv6 address, along with a 16-bit TCP or UDP port number. #include <sys/socket.h> int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen);
  • 9.
    •Servers bind totheir well-known port when they start. •A process can bind a specific IP address to its socket. •A client does not bind an IP address to its socket .
  • 10.
    • EACCES Theaddress is protected. • EADDRINUSE The given address is already in use. • EBADF The sockfd is not a valid descriptor. • EINVAL The socket is already bound to an address.
  • 11.
     The listenfunction is called only by a TCP server and it performs two actions: • Converts an unconnected (active) socket into a passive socket. • Specifies the maximum number of connections . #include <sys/socket.h> int listen(int sockfd, int backlog);
  • 12.
    • Called afterboth the socket and bind function . • Backlog - for a given listening socket, the kernel maintains 2 queues: • An incomplete connection queue - Contains an entry for each SYN that has arrived from a client . • A completed connection queue, entry for each client with whom 3-way handshake has completed.
  • 14.
    • Accept iscalled by a TCP server #include <sys/socket.h> int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); • The cliaddr and addrlen args are used to return the protocol address .
  • 15.
    • Fork() functionis the only way in Unix to create a new process #include <unistd.h> pid_t fork(void); • Called once but returnsTWICE • Once in the parent process (returns child process id), • and once in the child process (return of 0)
  • 16.
    • Used formultitasking . • The process want to handle another program .
  • 18.
    • Used whenmultiple clients are accessing a single server . • fork() is used in such cases to handle each client.
  • 19.
    • Used toclose a socket and terminate a TCP connection #include <unistd.h> int close(int sockfd); • Socket descriptor is no longer usable to the app process.