Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QTcpServer Class Reference

The QTcpServer class provides a TCP-based server. More...

    #include <QTcpServer>

Inherits QObject.

Note: All the functions in this class are reentrant.

- List of all members, including inherited members
- Obsolete members

### Writable Properties

- 1 writable property inherited from QObject

### Public Functions

- QTcpServer ( QObject * parent = 0 )
- virtual ~QTcpServer ()
- void close ()
- QString errorString () const
- virtual bool hasPendingConnections () const
- bool isListening () const
- bool listen ( const QHostAddress & address, Q_UINT16 port )
- bool listen ( Q_UINT16 port = 0 )
- int maxPendingConnections () const
- virtual QTcpSocket * nextPendingConnection ()
- QHostAddress serverAddress () const
- Qt::SocketError serverError () const
- Q_UINT16 serverPort () const
- void setMaxPendingConnections ( int numConnections )
- bool setSocketDescriptor ( int socketDescriptor )
- int socketDescriptor () const
- bool waitForNewConnection ( int msec = 0, bool * timedOut = 0 )

- 24 public functions inherited from QObject

### Public Slots

- 1 public slot inherited from QObject

### Signals

- void newConnection ()

- 1 signal inherited from QObject

### Static Public Members

- 4 static public members inherited from QObject

### Protected Functions

- 9 protected functions inherited from QObject

## Detailed Description

The QTcpServer class provides a TCP-based server.

This class makes it possible to accept incoming TCP connections. You can specify the port or have QTcpServer pick one automatically. You can listen on a specific address or on all the machine's addresses.

Call listen() to have the server listen for incoming connections. The newConnection() signal is then emitted each time a client connects to the server.

Call nextPendingConnection() to accept the pending connection as a connected QTcpSocket. The function returns a pointer to a QTcpSocket in Qt::ConnectedState that you can use for communicating with the client.

If an error occurs, serverError() returns the type of error, and errorString() can be called to get a human readable description of what happened.

When listening for connections, the address and port on which the server is listening are available as serverAddress() and serverPort().

Calling close() makes QTcpServer stop listening for incoming connections.

Although QTcpServer is mostly designed for use with an event loop, it's possible to use it without one. In that case, you must use waitForNewConnection(), which blocks until either a connection is available or a timeout expires.

The network/fortuneserver example illustrates how to use QTcpServer in an application.

See also QTcpSocket.

## Member Function Documentation

### QTcpServer::QTcpServer ( QObject * parent = 0 )

Constructs a QTcpServer object.

parent is passed to the QObject constructor.

See also listen() and setSocketDescriptor().

### QTcpServer::~QTcpServer () `[virtual]`

Destroys the QTcpServer object. If the server is listening for connections, the socket is automatically closed.

Any client QTcpSockets that are still connected must either disconnect or be reparented before the server is deleted.

See also close().

### void QTcpServer::close ()

Closes the server. The server will no longer listen for incoming connections.

See also listen().

### QString QTcpServer::errorString () const

Returns a human readable description of the last error that occurred.

See also serverError().

### bool QTcpServer::hasPendingConnections () const `[virtual]`

Returns true if the server has a pending connection; otherwise returns false.

See also nextPendingConnection() and setMaxPendingConnections().

### bool QTcpServer::isListening () const

Returns true if the server is currently listening for incoming connections; otherwise returns false.

See also listen().

### bool QTcpServer::listen ( const QHostAddress & address, Q_UINT16 port )

Tells the server to listen for incoming connections on address address and port port. If port is 0, a port is chosed automatically. If address is QHostAddress::Any, the server will listen on all network interfaces.

Returns true on success; otherwise returns false.

See also isListening().

### bool QTcpServer::listen ( Q_UINT16 port = 0 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Listens for connections on all network interfaces. Equivalent to calling listen(QHostAddress::Any, port).

### int QTcpServer::maxPendingConnections () const

Returns the maximum number of pending accepted connections. The default is 30.

See also setMaxPendingConnections() and hasPendingConnections().

### void QTcpServer::newConnection () `[signal]`

This signal is emitted every time a new connection is available.

See also hasPendingConnections() and nextPendingConnection().

### QTcpSocket * QTcpServer::nextPendingConnection () `[virtual]`

Returns the next pending connection as a connected QTcpSocket object.

The socket is created as a child of the server, which means that it is automatically deleted when the QTcpServer object is destroyed. It is still a good idea to delete the object explicitly when you are done with it, to avoid wasting memory.

See also hasPendingConnections().

### QHostAddress QTcpServer::serverAddress () const

Returns the server's address if the server is listening for connections; otherwise returns QHostAddress::Null.

See also serverPort() and listen().

### Qt::SocketError QTcpServer::serverError () const

Returns an error code for the last error that occurred.

See also errorString().

### Q_UINT16 QTcpServer::serverPort () const

Returns the server's port if the server is listening for connections; otherwise returns 0.

See also serverAddress() and listen().

### void QTcpServer::setMaxPendingConnections ( int numConnections )

Sets the maximum number of pending accepted connections to numConnections. QTcpServer will accept no more than numConnections incoming connections before nextPendingConnection() is called. By default, the limit is 30 pending connections.

Clients that attempt to connect to the server after it has reached its maximum number of pending connections will either immediately fail to connect or time out.

See also maxPendingConnections() and hasPendingConnections().

### bool QTcpServer::setSocketDescriptor ( int socketDescriptor )

Sets the socket descriptor this server should use when listening for incoming connections to socketDescriptor.

The socket is assumed to be in listening state.

See also socketDescriptor() and isListening().

### int QTcpServer::socketDescriptor () const

Returns the native socket descriptor the server uses to listen for incoming instructions, or -1 if the server is not listening.

See also setSocketDescriptor() and isListening().

### bool QTcpServer::waitForNewConnection ( int msec = 0, bool * timedOut = 0 )

Waits for at most msec milliseconds or until an incoming connection is available. Returns true if a connection is available; otherwise returns false. If the operation timed out and timedOut is not 0, *timedOut will be set to true.

This is a blocking function call. Its use is disadvised in a single-threaded GUI application, since the whole application will stop responding until the function returns. waitForNewConnection() is mostly useful when there is no event loop available.

The non-blocking alternative is to connect to the newConnection() signal.

See also hasPendingConnections() and nextPendingConnection().