Essential Networking Module User’s Guide : PART II The Networking Package : Chapter 7 The Berkeley Sockets Adapter : Socket Multiplexing
Socket Multiplexing
If an application tries to read from a socket whose buffers are empty or tries to write to a socket whose buffers are full, the socket will normally block or stop the application until it can complete the operation. This is a problem if your application uses more than one socket at a time.
For example, an application could create two open sockets, s1 and s2, that display any data that arrives on a socket. Initially, data is not available on either socket. If the application reads from s1, execution blocks until data is available on that socket. In the meantime, data could arrive on s2, but the program would never receive it because the program is waiting for s1.
The solution is to block on both sockets simultaneously. The following sections explain how.
Working with Socket Attributes
Socket attributes indicate conditions on a socket. For example, a socket attribute might indicate that a socket is ready for reading or writing, or that it is successfully connected. When a socket attribute is true, it means that the corresponding operation on the socket can be executed without blocking.
A socket attribute is represented by an instance of the RWSocketAttribute class. An RWSocketAttribute has two parts: a socket and an attribute, where the attribute is a combination of any of the following:
RWSocketAttribute::sock_attr_canread
Data is available for reading.
RWSocketAttribute::sock_attr_canwrite
Data can be written on the socket.
RWSocketAttribute::sock_attr_exception
An exceptional condition, such as the arrival of out-of-band data, has occurred.
RWSocketAttribute::sock_attr_isconnected
The socket is connected.
RWSocketAttribute::sock_attr_canaccept
A connection has arrived on this socket and can be received using RWSocket::accept().
Applications can set multiple attributes by bitwise OR-ing them together.
Using rwSocketSelect
You can use the global function rwSocketSelect() to test attributes and wait for them to become true. Example 15 shows how to wait on two sockets at the same time.
Example 15 – Using the global function rwSocketSelect
// Somehow establish two connected sockets, s1 and s2
 
RWTValOrderedVector<RWSocketAttribute> waiton; //1
waiton.append(RWSocketAttribute(
s1,RWSocketAttribute::CANREAD)); //2
waiton.append(RWSocketAttribute(
s2,RWSocketAttribute::CANREAD));
RWTValOrderedVector<RWSocketAttribute> ready =
rwSocketSelect(waiton); //3
 
// Do something with the sockets that are ready
 
//1 Builds a vector of socket attributes to wait for. An Essential Tools Module ordered vector class represents the list of attributes.
//2 Adds the conditions to the list. In this case, the application is waiting for either s1 or s2 to be ready for reading.
//3 Waits for at least one condition to be true. The conditions are passed in as an RWTValOrderedVector<RWSocketAttribute>, a vector of socket attributes. If one of the conditions in waiton is already true, rwSocketSelect() returns immediately. You can pass an optional second argument to set a timeout in seconds. The function returns a list of conditions that are true.
NOTE >> The Essential Networking Module page of the SourcePro API Reference Guide describes rwSocketSelect() in the Function Documentation section of that page.