classPiiTcpServer
#include <PiiTcpServer.h>
A threaded TCP server.
Inherits PiiNetworkServer
Description
This class can be used to run an application layer protocol over TCP. Use PiiLocalServer to run the protocols over local sockets/pipes. To create a minimalistic HTTP server, do this:
#include <QCoreApplication> #include <PiiHttpProtocol.h> #include <PiiTcpServer.h> int main(int argc, char* argv[]) { QCoreApplication app(argc, argv); PiiHttpProtocol p; PiiTcpServer server(&p); server.setPort(8080); // unprivileged port if (!server.start()) return 1; return app.exec(); }
The server will run eternally and answer "404 Not Found" to all requests. Look at the documentation of PiiHttpProtocol on how to add URI handlers to the server.
Properties
|
The IP address of the network interface this server binds to. |
|
|
The encryption mode. |
|
|
int
|
The TCP port number to bind to. |
|
int
|
The number of milliseconds the server will wait for client input. |
Public types
|
enum
|
{ NoEncryption = 0, SslEncryption }
Supported encryption modes. |
Constructors and destructor
|
Create a new TCP server that communicates with its clients with the given application layer protocol and encryption mode. |
|
|
( )
|
Public member functions
|
( )
|
|
|
virtual QIODevice *
|
Creates a new QTcpSocket or QSslSocket and assigns
|
|
( )
|
|
|
int
|
( )
|
|
int
|
( )
|
|
virtual QString
|
( )
|
|
void
|
|
|
void
|
|
|
void
|
(
|
|
void
|
(
|
|
virtual bool
|
Set the server's bind address. |
|
virtual bool
|
( )
Start the server. |
|
virtual void
|
(
Sends a stop signal to the server. |
Property details
-
QString bindAddress
[read, write]The IP address of the network interface this server binds to.
By default, the address is "0.0.0.0", which causes the server to bind to all interfaces. The bind address and the port can both be set with the #serverAddress property.
-
Encryption encryption
[read, write]The encryption mode.
The default value is
NoEncryption. The encryption mode only affects new connection attempts, not already accepted connections. -
int port
[read, write]The TCP port number to bind to.
The default value is 0 and must be changed before the server can work.
-
int readTimeout
[read, write]The number of milliseconds the server will wait for client input.
If nothing has been received within the time-out period, the socket device (see createSocket()) will return with zero bytes read. It is up to the protocol implementation to deal with time-outs. The default value is 20000.
Enumeration details
-
enum Encryption
Supported encryption modes.
-
NoEncryption- the connection will not be encrypted. -
SslEncryption- the connection will be encrypted with SSLv3.
-
Function details
-
PiiTcpServer
Create a new TCP server that communicates with its clients with the given application layer protocol and encryption mode.
-
~PiiTcpServer
() -
QString bindAddress
() -
Creates a new QTcpSocket or QSslSocket and assigns
socketDescriptorto it.Reimplemented from PiiNetworkServerThread::Controller.
-
Encryption encryption
() -
int port
() -
int readTimeout
() -
Reimplemented from PiiNetworkServer.
-
void setBindAddress
-
void setEncryption
-
void setPort
(- int port
-
void setReadTimeout
(- int readTimeout
-
Set the server's bind address.
Format IPv4 addresses like "123.123.123.123:80" and IPv6 addresses like "[2001:db8::1428:57ab]:443".
Reimplemented from PiiNetworkServer.
-
virtual bool start
()[virtual]Start the server.
This function will create a TCP socket and bind it to the interface(s) and port specified. If the server cannot listen to the socket, it will return
false.Note that this function will not block. If the server starts successfully, it will continue running in the backround.
Reimplemented from PiiNetworkServer.
-
virtual void stop
(- PiiNetwork::StopMode mode
[virtual]Sends a stop signal to the server.
This function stops all worker threads and waits until they are done. Subclasses may implement any additional functionality related to stopping the server. Typically, the overridden first calls this function and then closes the low-level server interface.
See also
-
QTcpServer::close()
Reimplemented from PiiNetworkServer.
Add a note
Not a single note added yet. Be the first, add yours.