UNP Chapter 4: Elementary TCP Sockets
CMPS 105: Systems Programming
- Prof. Scott Brandt
UNP Chapter 4: Elementary TCP Sockets CMPS 105: Systems Programming - - PowerPoint PPT Presentation
UNP Chapter 4: Elementary TCP Sockets CMPS 105: Systems Programming Prof. Scott Brandt T Th 2-3:45 Soc Sci 2, Rm. 167 Introduction First: Elementary socket functions Next: TCP client-server example Concurrent services Common
First: Elementary socket functions Next: TCP client-server example Concurrent services
Common Unix server technique Allows server to simultaneously serve
Server forks a separate process to serve
Figure 4.1 (p. 96) 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
To perform network I/O, a process first
Called by both client and server
# include < sys/socket.h> int socket(int family, int type, int
returns a socket descriptor
family specifies 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
type specifies type of communication
SOCK_STREAM: stream socket SOCK_DGRAM: datagram socket SOCK_SEQPACKET: sequenced packet
SOCK_RAW: raw socket
TCP supports only SOCK_STREAM UDP supports SOCK_DGRAM
protocol specifies transport protocol
IPPROTO_TCP: TCP transport protocol IPPROTO_UDP: UDP transport protocol IPPROTO_SCTP: SCTP transport protocol
TCP provides reliable, in-order streaming
Resends lost packets, guarantees order
UDP provides possibly unreliable, possibly
Doesn’t guarantee anything, but usually works
Use by a TCP client to establish a
# include < sys/socket.h> int connect(int sockfd, const struct
Returns when the connection has been
Possible errors: timeout, host not
sockfd is the socket descriptor returned
servaddr is the socket address
Contains the IP address and port number
addrlen is the size of the address
Used by server to bind a socket to an IP
# include < sys/socket.h> int bind(int sockfd, const struct
Common error: EADDRINUSE
sockfd is the socket descriptor returned
myaddr is the address of this server
Note: addresses are protocol-specific Usually well-known addresses so that
addrlen is the length of the address
Called by a TCP server to:
Set the socket up to receive connections Specify the maximum number of connections the
# include < sys/socket.h> int listen(int sockfd, int backlog); Normally called after socket() and bind() and
sockfd is the socket descriptor from socket() backlog should be non-zero
Called by a TCP server to return the next completed
Blocks if no connection is available
# include < sys/socket.h> int accept(int sockfd, struct sockaddr * cliaddr,
sockfd is the socket descriptor from socket() cliaddr is the address of the client that connected to
Returns a new socket descriptor for the connection to
int main(int argc, char **argv) { int listenfd, connfd; struct sockaddr_in servaddr; char buff[maxline]; time_t ticks; listenfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(13); // daytime sever bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); listen(listenfd, LISTENQ); while(1) { connfd = accept(listenfd, (SA *)NULL, NULL); ticks = time(NULL); snprintf(buff, sizeof(buff), “%.24s\r\n”, ctime(&ticks)); write(connfd, buff, strlen(buff)); close(connfd); } }
The previous example is an iterative
It sequentially receives connections and
A concurrent server forks a child
Multiple requests can be handled in parallel
pid_t pid; int listenfd, connfd; listenfd = socket(…); bind(listenfd, …); listen(listenfd, LISTENQ); while(1) { connfd = accept(listenfd, …); if( (pid = fork()) == 0) { close(listenfd); doit(connfd); // service the request close(connfd); exit(0); } close(connfd); }
Normal close() is used to close sockets # include < unistd.h> int close(int sockfd);
getsockname() returns the local
Used to get socket address when
getpeername() returns the foreign
Used to get client address in an execed