Introduction First: Elementarysocket functions Next: TCP client-server example Concurrent services Common Unix server technique Allows server to simultaneously serve multiple clients Server forks a separate process to serve each client
3.
Typical TCP Client- ServerScenario Server starts up Client starts up and connects to server Client contacts server Server processes client request Server responds to client Client closes connection Server goes back to waiting for clients to connect
5.
socket() To performnetwork I/O, a process first calls socket() Called by both client and server #include <sys/socket.h> int socket(int family, int type, int protocol); returns a socket descriptor
6.
socket() parameters familyspecifies the protocol family or domain AF_INET: IPv4 protocols AF_INET6: IPv6 protocols AF_LOCAL: Unix domain protocols AF_ROUTE: Routing sockets AF_KEY: Key socket Usually: AF_INET AF_LOCAL might work on CATS machines
7.
socket() parameters typespecifies type of communication SOCK_STREAM: stream socket (Reliable, connection-oriented (TCP)) SOCK_DGRAM: datagram socket (Unreliable, connectionless (UDP)) SOCK_SEQPACKET: sequenced packet socket (Reliable, Connection-Oriented, Preserves Message Boundaries (SCTP)) SOCK_RAW: raw socket (for direct IP packet access)
8.
socket() parameters protocolspecifies transport protocol IPPROTO_TCP: TCP transport protocol IPPROTO_UDP: UDP transport protocol IPPROTO_SCTP: SCTP transport protocol TCP provides reliable, in-order streaming communication Resends lost packets, guarantees order UDP provides possibly unreliable, possibly out-of-order message delivery Doesn’t guarantee anything, but usually works fine
9.
socket() parameters ReturnValue: On success Returns a socket → descriptor (a non-negative integer, similar to a file descriptor). On failure Returns -1 (error can be → checked using errno).
10.
socket() parameters The socket()function creates a communication endpoint but does not assign an address (IP + port) yet. Example Usage: Creating a TCP Socket (IPv4) This creates a TCP socket (since SOCK_STREAM with 0 defaults to TCP). Creating a UDP Socket (IPv6) This creates a UDP socket (since SOCK_DGRAM with 0 defaults to UDP).
11.
connect() Use bya TCP client to establish a connection with a TCP server #include <sys/socket.h> int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); Returns when the connection has been established
connect() parameters sockfdis the socket descriptor returned by socket() servaddr is the socket address Contains the IP address and port number of the server Typically, struct sockaddr_in (IPv4) or struct sockaddr_in6 (IPv6) is used. addrlen is the size of the address structure
14.
bind() Used byserver to bind a socket to an IP address/port pair. This allows clients to know where to connect. #include <sys/socket.h> int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); Common error: EADDRINUSE (The requested port is already in use by
15.
bind() parameters sockfdis the socket descriptor returned by socket() myaddr is the address of this server Containing the local IP address and port. Note: addresses are protocol-specific Usually well-known addresses so that clients can find the server addrlen is the length of the address
listen() Called bya TCP server to: Set the socket up to receive connections Specify the maximum number of connections the kernel should queue up for this socket #include <sys/socket.h> int listen(int sockfd, int backlog); Normally called after socket() and bind() and before accept() sockfd is the socket descriptor from socket() Backlog: The maximum number of pending connections the kernel should
18.
accept() Called bya TCP server to return the next completed connection from a client Blocks if no connection is available #include <sys/socket.h> int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); sockfd is the socket descriptor from socket() cliaddr is the address of the client that connected to the server Returns a new socket descriptor for the connection to the client
Concurrent Servers Theprevious example is an iterative server It sequentially receives connections and processes each request in turn A concurrent server forks a child process to handle each request Multiple requests can be handled in parallel by multiple concurrently executing child processes
getsockname() and getpeername() getsockname()returns the local protocol address (IP+Port) associate with a socket Used to get socket address when responding to a wildcard connection (INADDR_ANY) getpeername() returns the foreign protocol address associated with a socket Used to get client address in an execed process