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?

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

Embed Size (px)

Citation preview

Page 1: 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

• 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?

Page 2: 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

•How to specify the maximum number of connections for a socket?

•How to find out the remote machine information?

Page 3: 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

• Today’s topic:

•Introduction to UDP

•Some server design alternatives

•Select

Page 4: 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

• 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.

Page 5: 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

• Why UDP:– Applications that do not need 100%

reliability communication. E.g VoIP, 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).

Page 6: 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

• Basic UDP service interface:– Socket, bind, sendto, recvfrom, close

UDP server client

socket socketbind sendtorecvfrom recvfromSendto close

TCP server clientsocket socketBind connectListen …… closeclose

Page 7: 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

#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.

Page 8: 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

Server design alternatives: concurrent and multiplexed server.

• Concurrent server (see lect3/example5.c):

•Use a new child process to handle a new connection requests.

Page 9: 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

• 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?).

Page 10: 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

– 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.

Page 11: 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

#include <sys/select.h>

#include <sys/time.h>

int select (int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, struct timeval *timeout)

Page 12: 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

– 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

Page 13: 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

–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)

Page 14: 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

–Maxfdp1: 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.

Page 15: 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

• 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.

Page 16: 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

• 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.

Page 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

• 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.