One listener plus many clients
Keep a listener and all accepted client connections in one set. When the
listener is read-ready, call AcceptNext. When a connected socket
is read-ready, call a receive method.
A socket set lets one Chilkat.Socket object coordinate multiple
listener and connected sockets. Instead of blocking on one connection at a time,
the application waits for whichever sockets are ready, selects one, and performs
the appropriate operation through the set object.
A socket set is a Socket object that owns multiple member sockets.
It can wait for several sockets at once, report which members are ready, and route
ordinary socket operations to the member selected by
SelectorIndex, SelectorReadIndex, or
SelectorWriteIndex.
AcceptNext, ReceiveString,
SendBytes, and Close operate on that selected
socket.
A set may contain listener sockets and established connections at the same time. The application must remember which members are listeners because the set does not label the ready socket's role.
Socket sets are most helpful when several independent sockets may need attention, but the application wants a single coordinating loop rather than a separate blocking wait for every connection.
Keep a listener and all accepted client connections in one set. When the
listener is read-ready, call AcceptNext. When a connected socket
is read-ready, call a receive method.
A monitoring tool, gateway, or test harness can connect to many remote systems and wait until any one of them returns data.
Pass 0 to SelectForReading or
SelectForWriting to poll immediately, allowing the application to
perform other work between checks.
SelectForWriting identifies sockets that can currently accept data
into the operating-system send path without blocking. This does not mean the
remote application has received or processed the data.
TakeSocket
A normal Chilkat.Socket object becomes a socket set after one or more
successful calls to TakeSocket. Each call moves the source
connection into a newly created member of the set.
Connect outbound sockets, create a listener, or accept connections using ordinary socket objects.
Call socketSet.TakeSocket(sourceSocket) for each connection or listener to be managed.
After selection, call ordinary methods on socketSet; Chilkat routes the call to the selected member.
TakeSocket moves ownership into the setTakeSocket with TakeConnection.
TakeSocket adds a new member to a socket set. TakeConnection
moves a connection into the receiving object itself and is not a set-building method.
A selector tells the set which member should receive ordinary method calls and property access. The three selectors are mutually exclusive: assigning any one clears the other two.
| Selector | What the number means | When to use it |
|---|---|---|
SelectorIndex |
A direct position in the contained set: 0 through NumSocketsInSet - 1. |
When the application already knows which set member it wants. |
SelectorReadIndex |
A position in the read-ready result returned by the most recent SelectForReading. |
While iterating sockets that are ready for accept or receive activity. |
SelectorWriteIndex |
A position in the write-ready result returned by the most recent SelectForWriting. |
While iterating sockets that can currently accept a write without blocking. |
SelectorReadIndex or SelectorWriteIndex. Do not translate
it into SelectorIndex. Setting SelectorIndex would clear
the ready selection and reinterpret the number as a direct set position.
SelectForReading returns the number of ready sockets, 0
for no readiness before the timeout, or -1 for an error. When it
returns a positive value, iterate the ready positions and route operations through
the set object.
n = socketSet.SelectForReading(timeoutMs)
if n == -1
// Handle select error.
else
for i = 0 to n - 1
socketSet.SelectorReadIndex = i
if selected member is a listener
socketSet.AcceptNext(destinationSocket, acceptTimeoutMs)
else
socketSet.ReceiveXxx(...)
end if
next
end if
AcceptNext or a receive method.
| Socket type | Read-ready means | Typical next call |
|---|---|---|
| Listener | An incoming connection is pending. | AcceptNext |
| Connected socket | Data is available, or a close/error condition can now be detected. | A receive method, followed by failure-state handling if needed. |
The ready-set order is stable while you iterate one select result, but the order is arbitrary. It does not have to match the order in which sockets were added, and it may change on the next select call.
SelectorIndex refers to the current contained-set position. If a
member is removed, later positions shift down, so a saved direct index may
identify a different member or become invalid.
SelectorReadIndex and SelectorWriteIndex are temporary
positions in the current ready result. The next call to either select method
invalidates all prior ready indexes.
There is no separate “remove without closing” operation for a contained member.
Select the member and call Close; the selected socket is closed and
removed from the set immediately.
Set a selector, call Close on the set object, and Chilkat routes
the close to the selected member. Direct set positions after that member shift
down.
A connection that has died may remain counted until the next
SelectForReading or SelectForWriting. Each select
rebuilds the descriptor set and removes members that no longer have a usable
descriptor.
// Remove direct member 3.
socketSet.SelectorIndex = 3
socketSet.Close(maxWaitMs)
// Do not reuse old direct indexes without reconsidering the new set order.
The following design keeps one listener and several accepted client connections in the same set. The application maintains a small side table that records whether each member is a listener or a client connection.
TakeSocket.SelectForReading.SelectorReadIndex.Close to remove it.// Conceptual pseudocode. Exact syntax varies by language.
listener.BindAndListen(port, backlog)
socketSet.TakeSocket(listener)
mark member as LISTENER
while running
n = socketSet.SelectForReading(1000)
if n < 0
handle select error
break
end if
for i = 0 to n - 1
socketSet.SelectorReadIndex = i
if selected member is LISTENER
accepted = new Socket
if socketSet.AcceptNext(accepted, 0)
socketSet.TakeSocket(accepted)
mark new member as CLIENT
end if
else
if not socketSet.ReceiveXxx(...)
// Handle close/error, then remove the selected member.
socketSet.Close(0)
end if
end if
next
end while
SelectorIndex.SelectorReadIndex or SelectorWriteIndex directly.TakeConnection to build the set.TakeSocket adds set members; TakeConnection does not.TakeSocket to move sockets into the set.NumSocketsInSet to inspect the current contained count.SelectorIndex only for a direct current set position.SelectForReading, set SelectorReadIndex directly.SelectForWriting, set SelectorWriteIndex directly.Close on a selected member to close and remove it.