Review How to create a TCP end point

















- Slides: 17
• Review: • How to create a TCP end point? • What is the right format for sin_port and sin_addr in the sockaddr_in data structure? • How many different ways we can bind a socket?
• How to specify the maximum number of connections for a socket? • How to find out the remote machine information?
• Today’s topic: • Introduction to UDP • Some server design alternatives • Select
• TCP: Reliable byte stream service. – Different ways to build client/servers –How to get around blocking I/O –Assumption: whatever sent will eventually be received!! • UDP: Unreliable datagram service. • Data may get lost – application may need to deal with more details in the communication.
• Why UDP: – Applications that do not need 100% reliability communication. E. g Vo. IP, video stream, DNS servers. – Applications care a lot about performance: high performance computing (TCP cares too much about fairness). – Applications that need multicast or broadcast (TCP only supports point to point communication).
• Basic UDP service interface: – Socket, bind, sendto, recvfrom, close UDP server TCP client server socket Bind bind sendto Listen recvfrom … Sendto close client socket connect … close
#include <sys/socket. h> ssize_t recvfrom(int sockfd, void *buff, size_t nbytes, int flags, struct sockaddr *from, socklen_t *addrlen); ssize_t sendto(int sockfd, void *buff, size_t nbytes, int flags, const struct sockaddr *to, socklen_t addrlen); See udpsender. c/udprecv. c for communication using UDP.
Server design alternatives: concurrent and multiplexed server. • Concurrent server (see lect 3/example 5. c): • Use a new child process to handle a new connection requests.
• Multiplexed Server: The use of select. • I/O multiplexing – check the file descriptor before performing a blocking operation (what happen to the client when a concurrent server clushs? ).
– The select function that allows: • To detect any of the descriptors in the read set are ready for reading. • To detect any of the descriptors in the write set are ready for writing • To detect any of the descriptors in the error set have exception conditions pending. • To wait for a period for something to happen.
#include <sys/select. h> #include <sys/time. h> int select (int maxfdp 1, fd_set *readset, fd_set *writeset, fd_set *exceptset, struct timeval *timeout)
– Set the timeout value: Struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ } • Wait forever (blocking select): timeout = NULL • Non blocking select (return right away: tv_sec = 0; tv_usec = 0; • Wait for a certain amount of time: tv_sec and tv_usec
– Set the set of file descriptors value: void FD_ZERO(fd_set *fdset) void FD_SET(int fd, fd_set *fdset) void FD_CLR(int fd, fd_set *fdset) void FD_ISSET(int fd, fd_set *fdset)
–Maxfdp 1: the maximum file descriptor number plus 1. (can just specify a big number (64) if unknown). –Select clears the uninteresting file descriptors in the fd_sets – always reset the fd_sets before calling select.
• When is a socket ready for read? • The number of bytes in the socket is more than the low-water mark (can be set, default 1 for TCP/UDP socket) • Half of the connection is closed • Listening socket with nonzero of completed connections • Socket error.
• When is a socket ready for write? • The available buffer space is larger than the low-water mark • Half the connection is closed • Error pending • Exception? • Out of band data exists.
• A multiplexed server (multiserv. c) – A single process to handle everything including connection request and data processing. – Using select the check on all descriptors that might need communication. • Response to whatever from the clients.