Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Essential Networking Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

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

7.6.1 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 ORing them together.

7.6.2 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

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


The Essential Networking Module page of the SourcePro C++ API Reference Guide describes rwSocketSelect() in the Global Reference section.



Previous fileTop of DocumentContentsIndex pageNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.