|
BOOL Start( |
|
LPCTSTR lpszLocalHost, |
|
|
UINT nLocalPort, |
|
|
UINT nMaxClients, |
|
|
DWORD dwOptions |
|
); |
BOOL Start( |
|
LPCTSTR lpszLocalHost, |
|
|
UINT nLocalPort, |
|
|
DWORD dwOptions |
|
); |
BOOL Start( |
|
UINT nLocalPort, |
|
|
DWORD dwOptions |
|
); |
BOOL Start( |
|
UINT nLocalPort |
|
); |
The Start method begins listening for client
connections on the specified local address and port number. The server
is started in its own thread and manages the client sessions
independently of the calling thread. All interaction with the server
and its client sessions takes place inside the class event handlers.
Parameters
- lpszLocalHost
- A pointer to a string which specifies the local
hostname or IP address address that the server should be bound to.
If this parameter is omitted or specifies a NULL pointer an appropriate
address will automatically be used. If a specific address is used,
the server will only accept client connections on the network
interface that is bound to that address.
- nLocalPort
- The port number the server should use to listen for client
connections. The port
number used by the application must be unique and multiple instances of
a server cannot use the same port number. It is recommended that a
port number greater than 5000 be used for private,
application-specific implementations.
- nMaxClients
- The maximum number of client connections that can be
established with the server. A value of zero specifies that there
should not be any fixed limit on the number of active client
connections. This value can be adjusted after the server has been
created by calling the Throttle method.
- dwOptions
- An unsigned integer value that specifies one or more options to
be used when creating an
instance of the server. For a list of the available options, see
Server Option Constants.
If this parameter is omitted, the default options for the server
instance will be used.
Return Value
If the method succeeds, the return value is non-zero. If the method fails, the return value is
zero.
To get extended error information, call GetLastError.
Remarks
In most cases, the lpszLocalHost parameter should be omitted
or a NULL
pointer. On a multihomed system, this will enable
the server to accept connections on any appropriately configured
network adapter. Specifying a hostname or IP address will limit client
connections to that particular address. Note that the hostname or
address must be one that is assigned to the local system, otherwise an
error will occur.
If an IPv6 address is specified as the local address, the system
must have an IPv6 stack installed and configured, otherwise the
method will fail.
To listen for connections on any suitable IPv4 interface, specify
the special dotted-quad address "0.0.0.0". You can accept connections
from clients using either IPv4 or IPv6 on the same socket by
specifying the special IPv6 address "::0", however this is only
supported on Windows 7 and Windows Server 2008 R2 or later platforms. If
no local address is specified, then the server will only listen for
connections from clients using IPv4. This behavior is by design for
backwards compatibility with systems that do not have an IPv6 TCP/IP
stack installed.
The
server instance is managed in another thread, and all interaction with the
server and active client connections are performed inside the event
handlers. To disconnect all active connections, close the listening
socket and terminate the server thread, call the Stop
method.
Example
// EchoServer implementation
class CEchoServer : public CInternetServer
{
void OnRead(SOCKET hSocket)
{
// Read data sent by the client to the server
BYTE ioBuffer[1024];
INT nBytesRead = Read(hSocket, ioBuffer, sizeof(ioBuffer));
// Send a copy of the data back to the client
if (nBytesRead > 0)
Write(hSocket, ioBuffer, nBytesRead);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CEchoServer myServer;
// Start the server listening for connections on port 7000
if (myServer.Start(7000))
{
TCHAR szCommand[128], *pszCommand;
// Process commands entered by the user at the console
while (TRUE)
{
if ((pszCommand = _fgetts(szCommand, 128, stdin)) == NULL)
break;
if (_tcsicmp(pszCommand, _T("quit")) == 0)
break;
}
// Stop the server and terminate all clients
myServer.Stop();
}
return 0;
}
Requirements
Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cswsock11.h
Import Library: cswskv11.lib
Unicode: Implemented as Unicode and ANSI versions
See Also
EnumClients,
Restart,
Stop
|
|