|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.trolltech.qt.internal.QSignalEmitterInternal
com.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.core.QObject
com.trolltech.qt.network.QTcpServer
public class QTcpServer
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 QAbstractSocket::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.
QTcpSocket
, Fortune Server Example, Threaded Fortune Server Example, Loopback Example, and Torrent Example.
Nested Class Summary | |
---|---|
static class |
QTcpServer.Result
|
Nested classes/interfaces inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
com.trolltech.qt.internal.QSignalEmitterInternal.AbstractSignalInternal |
Field Summary | |
---|---|
QSignalEmitter.Signal0 |
newConnection
This signal is emitted every time a new connection is available. |
Fields inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
currentSender |
Constructor Summary | |
---|---|
QTcpServer()
Constructs a QTcpServer object. |
|
QTcpServer(QObject parent)
Constructs a QTcpServer object. |
Method Summary | |
---|---|
void |
close()
Closes the server. |
java.lang.String |
errorString()
Returns a human readable description of the last error that occurred. |
boolean |
hasPendingConnections()
Returns true if the server has a pending connection; otherwise returns false. |
protected void |
incomingConnection(int handle)
This virtual function is called by QTcpServer when a new connection is available. |
boolean |
isListening()
Returns true if the server is currently listening for incoming connections; otherwise returns false. |
boolean |
listen()
Tells the server to listen for incoming connections on address address and port port. |
boolean |
listen(QHostAddress address)
Tells the server to listen for incoming connections on address address and port port. |
boolean |
listen(QHostAddress address,
int port)
Tells the server to listen for incoming connections on address address and port port. |
int |
maxPendingConnections()
Returns the maximum number of pending accepted connections. |
QTcpSocket |
nextPendingConnection()
Returns the next pending connection as a connected QTcpSocket object. |
QNetworkProxy |
proxy()
Returns the network proxy for this socket. |
QHostAddress |
serverAddress()
Returns the server's address if the server is listening for connections; otherwise returns QHostAddress::Null . |
QAbstractSocket.SocketError |
serverError()
Returns an error code for the last error that occurred. |
int |
serverPort()
Returns the server's port if the server is listening for connections; otherwise, returns 0. |
void |
setMaxPendingConnections(int numConnections)
Sets the maximum number of pending accepted connections to numConnections. |
void |
setProxy(QNetworkProxy networkProxy)
Sets the explicit network proxy for this socket to networkProxy. |
boolean |
setSocketDescriptor(int socketDescriptor)
Sets the socket descriptor this server should use when listening for incoming connections to socketDescriptor. |
int |
socketDescriptor()
Returns the native socket descriptor the server uses to listen for incoming instructions, or -1 if the server is not listening. |
QTcpServer.Result |
waitForNewConnection()
This is an overloaded method provided for convenience. |
QTcpServer.Result |
waitForNewConnection(int msec)
Waits for at most msec milliseconds or until an incoming connection is available. |
Methods inherited from class com.trolltech.qt.core.QObject |
---|
childEvent, children, connectSlotsByName, customEvent, disposeLater, dumpObjectInfo, dumpObjectTree, dynamicPropertyNames, event, eventFilter, findChild, findChild, findChild, findChildren, findChildren, findChildren, findChildren, indexOfProperty, installEventFilter, isWidgetType, killTimer, moveToThread, objectName, parent, properties, property, removeEventFilter, setObjectName, setParent, setProperty, startTimer, timerEvent, toString, userProperty |
Methods inherited from class com.trolltech.qt.QtJambiObject |
---|
dispose, disposed, equals, finalize, reassignNativeResources, tr, tr, tr |
Methods inherited from class com.trolltech.qt.QSignalEmitter |
---|
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread |
Methods inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
__qt_signalInitialization |
Methods inherited from class java.lang.Object |
---|
clone, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface com.trolltech.qt.QtJambiInterface |
---|
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership |
Field Detail |
---|
public final QSignalEmitter.Signal0 newConnection
hasPendingConnections()
, and nextPendingConnection()
.
Constructor Detail |
---|
public QTcpServer()
parent is passed to the QObject
constructor.
listen()
, and setSocketDescriptor()
.
public QTcpServer(QObject parent)
parent is passed to the QObject
constructor.
listen()
, and setSocketDescriptor()
.
Method Detail |
---|
public final void close()
listen()
.
public final java.lang.String errorString()
serverError()
.
public final boolean isListening()
listen()
.
public final int maxPendingConnections()
setMaxPendingConnections()
, and hasPendingConnections()
.
public final QNetworkProxy proxy()
QNetworkProxy::DefaultProxy
is used. setProxy()
, and QNetworkProxy
.
public final QHostAddress serverAddress()
QHostAddress::Null
. serverPort()
, and listen()
.
public final QAbstractSocket.SocketError serverError()
errorString()
.
public final void setMaxPendingConnections(int numConnections)
nextPendingConnection()
is called. By default, the limit is 30 pending connections. Clients may still able to connect after the server has reached its maximum number of pending connections (i.e., QTcpSocket
can still emit the connected() signal). QTcpServer will stop accepting the new connections, but the operating system may still keep them in queue.
maxPendingConnections()
, and hasPendingConnections()
.
public final void setProxy(QNetworkProxy networkProxy)
To disable the use of a proxy for this socket, use the QNetworkProxy::NoProxy
proxy type:
server.setProxy(new QNetworkProxy(QNetworkProxy.ProxyType.NoProxy, new String(), 0));
proxy()
, and QNetworkProxy
.
public final boolean setSocketDescriptor(int socketDescriptor)
The socket is assumed to be in listening state.
socketDescriptor()
, and isListening()
.
public final int socketDescriptor()
If the server is using QNetworkProxy
, the returned descriptor may not be usable with native socket functions.
setSocketDescriptor()
, and isListening()
.
public boolean hasPendingConnections()
nextPendingConnection()
, and setMaxPendingConnections()
.
protected void incomingConnection(int handle)
The base implementation creates a QTcpSocket
, sets the socket descriptor and then stores the QTcpSocket
in an internal list of pending connections. Finally newConnection()
is emitted.
Reimplement this function to alter the server's behavior when a connection is available.
If this server is using QNetworkProxy
then the socketDescriptor may not be usable with native socket functions, and should only be used with QTcpSocket::setSocketDescriptor()
.
newConnection()
, and nextPendingConnection()
.
public QTcpSocket nextPendingConnection()
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.
0 is returned if this function is called when there are no pending connections.
hasPendingConnections()
.
public final boolean listen(QHostAddress address, int port)
Returns true on success; otherwise returns false.
public final boolean listen(QHostAddress address)
QHostAddress::Any
, the server will listen on all network interfaces. Returns true on success; otherwise returns false.
isListening()
.
public final boolean listen()
QHostAddress::Any
, the server will listen on all network interfaces. Returns true on success; otherwise returns false.
isListening()
.
public final int serverPort()
public final QTcpServer.Result waitForNewConnection(int msec)
This is a blocking function call. Its use is not adviced 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.
public final QTcpServer.Result waitForNewConnection()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |