| BOOL SetClientData( |
| |
SOCKET hClient, |
|
| |
LPVOID lpvData |
|
| ); |
| BOOL SetClientData( |
| |
LPVOID lpvData |
|
| ); |
Associate application defined data with the specified client session.
Parameters
- hClient
- An optional parameter that specifies the handle to the client socket.
If this parameter is omitted, the socket handle for the active
client session will be used. If this method is called outside of a
server event handler, the socket handle must be specified.
- lpvData
- Pointer to the application defined data associated with the
specified client session.
Return Value
If the method succeeds, the return value is non-zero. A return
value of zero indicates that application defined data for the client
session could not be modified. To get extended error information,
call the GetLastError method.
Remarks
The SetClientData method is used to associate
application defined data with a specific client session. This is
typically used to associate a pointer to a data structure or a class
instance with the client socket. The data can be
retrieved using the GetClientData method.
You should never specify a pointer to a local variable or data
structure that will go out of scope when the calling method exits.
If you do this, the pointer will no longer be valid after the
method exits and attempting to dereference that pointer at some
later time can cause an exception to be thrown and terminate the
program. You should always allocate a block of memory for the data
using a method such as HeapAlloc or LocalAlloc. If
you specify the address of a static or global data structure, you
must use thread synchronization methods when dereferencing and
modifying that structure.
Example
UINT *pnValue1 = new UINT;
UINT *pnValue2 = NULL;
*pnValue1 = 1234;
if (!pServer->SetClientData(hClient, pnValue1))
{
// Unable to associate the data with this session
return;
}
if (!pServer->GetClientData(hClient, (LPVOID *)&pnValue2))
{
// Unable to retrieve the data associated with this session
return;
}
// pnValue2 == pnValue1
printf("The value of the user defined data is %u\n", *pnValue2);
Requirements
Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cswsock12.h
Import Library: cswskv12.lib
See Also
|