Presentation is loading. Please wait.

Presentation is loading. Please wait.

Select The select function determines the status of one or more sockets, waiting if necessary, to perform synchronous I/O. int select( int nfds, fd_set*

Similar presentations


Presentation on theme: "Select The select function determines the status of one or more sockets, waiting if necessary, to perform synchronous I/O. int select( int nfds, fd_set*"— Presentation transcript:

1 Select The select function determines the status of one or more sockets, waiting if necessary, to perform synchronous I/O. int select( int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, const struct timeval* timeout ); Return value: The select function returns the total number of socket handles that are ready and contained in the fd_set structures, zero if the time limit expired, or SOCKET_ERROR if an error occurred. If the return value is SOCKET_ERROR, WSAGetLastError can be used to retrieve a specific error code.

2 Parameters nfds [in] Ignored. The nfds parameter is included only for compatibility with Berkeley sockets. readfds [in, out] Optional pointer to a set of sockets to be checked for readability. writefds [in, out] Optional pointer to a set of sockets to be checked for writability. exceptfds [in, out] Optional pointer to a set of sockets to be checked for errors. timeout [in] Maximum time for select to wait, provided in the form of a TIMEVAL structure. Set the timeout parameter to null for blocking operations.

3 FD_SET The fd_set structure is used by various Windows Sockets functions and service providers, such as the select function, to place sockets into a "set" for various purposes, such as testing a given socket for readability using the readfds parameter of the select function. typedef struct fd_set { u_int fd_count; SOCKET fd_array[FD_SETSIZE]; } fd_set; Members fd_count Number of sockets in the set. fd_array Array of sockets that are in the set.

4 FD-SET Macros The macros to manipulate and check fd_set contents are: FD_CLR(s, *set) Removes the descriptor s from set. FD_ISSET(s, *set) Nonzero if s is a member of the set. Otherwise, zero. FD_SET(s, *set) Adds descriptor s to set. FD_ZERO(*set) Initializes the set to the null set. The variable FD_SETSIZE determines the maximum number of descriptors in a set (default 64).

5 Remarks The select function is used to determine the status of one or more sockets. For each socket, the caller can request information on read, write, or error status. The set of sockets for which a given status is requested is indicated by an fd_set structure. The sockets contained within the fd_set structures must be associated with a single service provider. Any two of the parameters, readfds, writefds, or exceptfds, can be given as null. At least one must be non-null, and any non-null descriptor set must contain at least one handle to a socket.

6 Remarks- cont’d A socket will be identified in a particular set when select returns if: readfds: If listen has been called and a connection is pending, accept will succeed. Data is available for reading. Connection has been closed/reset/terminated. writefds: If processing a connect call (nonblocking), connection has succeeded. Data can be sent. exceptfds: If processing a connect call (nonblocking), connection attempt failed.

7 GetHostByName() Translates DNS names (a.k.a www.eng.tau.ac.il) to IP addresses. Returns the address in a pointer to a hostent struct – no need to allocate space for hostent as it’s done automatically in GetHostByName() (each call to the function uses the same space) hostent struct contains a NULL terminated list of IP addresses, we use the first one : remoteHost- >h_addr_list[0];


Download ppt "Select The select function determines the status of one or more sockets, waiting if necessary, to perform synchronous I/O. int select( int nfds, fd_set*"

Similar presentations


Ads by Google