Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

14.5 Socket Multiplexing

If an application tries to read from a socket whose buffers are empty or write to a socket whose buffers are full, the socket will normally block; pause the application until it can complete the operation. This is a problem if the application works with more than one socket at a time. For example, consider a program with two open sockets, s1 and s2, that print any input that arrives on a socket to the screen. No data is currently available on either socket. If the program reads from s1, execution blocks until data is available on that socket. In the meantime, data could arrive on s2, but the program never receives it. A better solution is to block on both sockets simultaneously.

14.5.1 RWSocketAttribute

A socket attribute is used to indicate that a socket is ready to do something. For example, a socket attribute might indicate that a socket is ready for reading, or for writing, or that a socket is successfully connected. When a socket attribute is true, it means that the corresponding operation on the socket can be executed without blocking.

In the net library, a socket attribute is represented by an instance of the RWSocketAttribute class. A socket attribute has two parts: a socket, and an attribute, where the attribute is a combination of any of the following.

RWSocketAttribute::CANREAD
Data is available for reading.
RWSocketAttribute::CANWRITE
Data can be written on the socket.
RWSocketAttribute::EXCEPTION
An exceptional condition, such as the arrival of out-of-band data, has occurred.
RWSocketAttribute::ISCONNECTED
The socket is connected.
RWSocketAttribute::ISCLOSED
The socket has been closed.
RWSocketAttribute::CANACCEPT
A connection has arrived on this socket, and can be received using RWSocket::accept().

Applications can set multiple attributes by OR'ing them together using |, the bitwise OR operator.

14.5.2 rwSocketSelect

The global function rwSocketSelect() is used to test for attributes and wait for attributes to become true. Here is an expanded version of the two-socket example; this version waits on both sockets at the same time.

//1Build a vector of socket attributes to be waited for. A Tools.h++ ordered vector class is used to represent the list of attributes.
//2Add the conditions to the list. In this case, the application wants to know as soon as either s1 or s2 is ready for reading.
//3Wait for at least one condition to come 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, then rwSocketSelect() returns immediately. An optional second argument could have been used to provide a timeout in seconds or fractions thereof. The function returns a list of conditions that are true.

The Tools.h++ ProfessionalClass Reference describes the rwSocketSelect() global function under the "Global Functions" heading in class RWSocketAttribute.



Previous fileTop of DocumentContentsIndexNext file

©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.