Q3SocketDevice Class Reference

The Q3SocketDevice class provides a platform-independent low-level socket API.

  1. #include <Q3SocketDevice>

This class is part of the Qt 3 support library. It is provided to keep old source code working. We strongly advise against using it in new code. See Porting to Qt 4 for more information.

Inherits: QIODevice.

Note: All functions in this class are reentrant.

Detailed Description

The Q3SocketDevice class provides a platform-independent low-level socket API.

This class provides a low level API for working with sockets. Users of this class are assumed to have networking experience. For most users the Q3Socket class provides a much easier and high level alternative, but certain things (like UDP) can't be done with Q3Socket and if you need a platform-independent API for those, Q3SocketDevice is the right choice.

The essential purpose of the class is to provide a QIODevice that works on sockets, wrapped in a platform-independent API.

When calling connect() or bind(), Q3SocketDevice detects the protocol family (IPv4, IPv6) automatically. Passing the protocol family to Q3SocketDevice's constructor or to setSocket() forces creation of a socket device of a specific protocol. If not set, the protocol will be detected at the first call to connect() or bind().

See also Q3Socket, QSocketNotifier, and QHostAddress.

Public Types

Toggle detailsenum Q3SocketDevice::

ErrorError { NoError , AlreadyBound , Inaccessible , NoResources , InternalError , Bug , , , , , UnknownError ...} { NoError , AlreadyBound , Inaccessible , NoResources , InternalError , Bug , , , , , UnknownError }

This enum type describes the error states of Q3SocketDevice.

ConstantValueDescription
Q3SocketDevice::NoError 0 No error has occurred.
Q3SocketDevice::AlreadyBound 1 The device is already bound, according to bind().
Q3SocketDevice::Inaccessible 2 The operating system or firewall prohibited the action.
Q3SocketDevice::NoResources 3 The operating system ran out of a resource.
Q3SocketDevice::InternalError 4 An internal error occurred in Q3SocketDevice.
Q3SocketDevice::Impossible ?An attempt was made to do something which makes no sense. For example:
  1. ::close( sd->socket() );
  2. sd->writeBlock( someData, 42 );

The libc ::close() closes the socket, but Q3SocketDevice is not aware of this. So when you call writeBlock(), the impossible happens.

ConstantValueDescription
Q3SocketDevice::NoFiles ?The operating system will not let Q3SocketDevice open another file.
Q3SocketDevice::ConnectionRefused ?A connection attempt was rejected by the peer.
Q3SocketDevice::NetworkFailure ?There is a network failure.
Q3SocketDevice::UnknownError ?The operating system did something unexpected.

Look up this member in the source code.

Toggle detailsenum Q3SocketDevice::

ProtocolProtocol { IPv4 , IPv6 , Unknown 2 ...} { IPv4 , IPv6 , Unknown 2 }

This enum type describes the protocol family of the socket. Possible values are:

ConstantValueDescription
Q3SocketDevice::IPv4 0 The socket is an IPv4 socket.
Q3SocketDevice::IPv6 1 The socket is an IPv6 socket.
Q3SocketDevice::Unknown 2 The protocol family of the socket is not known. This can happen if you use Q3SocketDevice with an already existing socket; it tries to determine the protocol family, but this can fail if the protocol family is not known to Q3SocketDevice.

See also protocol() and setSocket().

Look up this member in the source code.

Toggle detailsenum Q3SocketDevice::

TypeType { Stream , Datagram 1 ...} { Stream , Datagram 1 }

This enum type describes the type of the socket:

ConstantValueDescription
Q3SocketDevice::Stream 0 a stream socket (TCP, usually)
Q3SocketDevice::Datagram 1 a datagram socket (UDP, usually)

Look up this member in the source code.

    Public Functions

    Toggle details Q3SocketDevice

    Q3SocketDeviceQ3SocketDevice ( Type type=Stream ) ( Type type=Stream )

    Creates a Q3SocketDevice object for a stream or datagram socket.

    The type argument must be either Q3SocketDevice::Stream for a reliable, connection-oriented TCP socket, or Q3SocketDevice::Datagram for an unreliable UDP socket.

    The socket is created as an IPv4 socket.

    See also blocking() and protocol().

    Look up this member in the source code.

    Toggle details Q3SocketDevice

    Q3SocketDeviceQ3SocketDevice ( Type type , Protocol protocol , int dummy ...) ( Type type , Protocol protocol , int dummy )

    Creates a Q3SocketDevice object for a stream or datagram socket.

    The type argument must be either Q3SocketDevice::Stream for a reliable, connection-oriented TCP socket, or Q3SocketDevice::Datagram for an unreliable UDP socket.

    The protocol indicates whether the socket should be of type IPv4 or IPv6. Passing Unknown is not meaningful in this context and you should avoid using (it creates an IPv4 socket, but your code is not easily readable).

    The argument dummy is necessary for compatibility with some compilers.

    See also blocking() and protocol().

    Look up this member in the source code.

    Toggle details Q3SocketDevice

    Q3SocketDeviceQ3SocketDevice ( int socket , Type type ) ( int socket , Type type )

    Creates a Q3SocketDevice object for the existing socket socket.

    The type argument must match the actual socket type; use Q3SocketDevice::Stream for a reliable, connection-oriented TCP socket, or Q3SocketDevice::Datagram for an unreliable, connectionless UDP socket.

    Look up this member in the source code.

    Toggle details Q3SocketDevice

    ~Q3SocketDevice~Q3SocketDevice () () [virtual]

    Destroys the socket device and closes the socket if it is open.

    Look up this member in the source code.

    Toggle details int Q3SocketDevice

    acceptaccept () () [virtual]

    Extracts the first connection from the queue of pending connections for this socket and returns a new socket identifier. Returns -1 if the operation failed.

    See also bind() and listen().

    Look up this member in the source code.

    Toggle details QHostAddress Q3SocketDevice

    addressaddress () ()const

    Returns the address of this socket device. This may be 0.0.0.0 for a while, but is set to something sensible as soon as a sensible value is available.

    Look up this member in the source code.

    Toggle details bool Q3SocketDevice

    addressReusableaddressReusable () ()const

    Returns true if the address of this socket can be used by other sockets at the same time, and false if this socket claims exclusive ownership.

    See also setAddressReusable().

    Look up this member in the source code.

    Toggle details Offset Q3SocketDevice

    atat () ()const

    The read/write index is meaningless for a socket, therefore this function returns 0.

    Look up this member in the source code.

    Toggle details bool Q3SocketDevice

    atat ( Offset offset ) ( Offset offset )

    The read/write index is meaningless for a socket, therefore this function does nothing and returns true.

    The offset parameter is ignored.

    Look up this member in the source code.

    Toggle details bool Q3SocketDevice

    atEndatEnd () ()const [virtual]

    Reimplemented from QIODevice::atEnd().

    Returns true if no data is currently available at the socket; otherwise returns false.

    Look up this member in the source code.

    Toggle details bool Q3SocketDevice

    bindbind ( const QHostAddress &address , Q_UINT16 port ...) ( const QHostAddress &address , Q_UINT16 port ) [virtual]

    Assigns a name to an unnamed socket. The name is the host address address and the port number port. If the operation succeeds, bind() returns true; otherwise it returns false without changing what port() and address() return.

    bind() is used by servers for setting up incoming connections. Call bind() before listen().

    Look up this member in the source code.

    Toggle details bool Q3SocketDevice

    blockingblocking () ()const

    Returns true if the socket is valid and in blocking mode; otherwise returns false.

    Note that this function does not set error().

    Warning: On Windows, this function always returns true since the ioctlsocket() function is broken.

    See also setBlocking() and isValid().

    Look up this member in the source code.

    Toggle details qint64 Q3SocketDevice

    bytesAvailablebytesAvailable () ()const [virtual]

    Reimplemented from QIODevice::bytesAvailable().

    Returns the number of bytes available for reading, or -1 if an error occurred.

    Warning: On Microsoft Windows, we use the ioctlsocket() function to determine the number of bytes queued on the socket. According to Microsoft (KB Q125486), ioctlsocket() sometimes returns an incorrect number. The only safe way to determine the amount of data on the socket is to read it using readBlock(). QSocket has workarounds to deal with this problem.

    Look up this member in the source code.

    Toggle details void Q3SocketDevice

    closeclose () () [virtual]

    Reimplemented from QIODevice::close().

    Closes the socket and sets the socket identifier to -1 (invalid).

    (This function ignores errors; if there are any then a file descriptor leakage might result. As far as we know, the only error that can arise is EBADF, and that would of course not cause leakage. There may be OS-specific errors that we haven't come across, however.)

    See also open().

    Look up this member in the source code.

    Toggle details bool Q3SocketDevice

    connectconnect ( const QHostAddress &addr , Q_UINT16 port ...) ( const QHostAddress &addr , Q_UINT16 port ) [virtual]

    Connects to the IP address and port specified by addr and port. Returns true if it establishes a connection; otherwise returns false. If it returns false, error() explains why.

    Note that error() commonly returns NoError for non-blocking sockets; this just means that you can call connect() again in a little while and it'll probably succeed.

    Look up this member in the source code.

    Toggle details Error Q3SocketDevice

    errorerror () ()const

    Returns the first error seen.

    See also setError().

    Look up this member in the source code.

    Toggle details bool Q3SocketDevice

    flushflush () ()

    The current Q3SocketDevice implementation does not buffer at all, so this is a no-op. This function always returns true.

    Look up this member in the source code.

    Toggle details bool Q3SocketDevice

    isValidisValid () ()const

    Returns true if this is a valid socket; otherwise returns false.

    See also socket().

    Look up this member in the source code.

    Toggle details bool Q3SocketDevice

    listenlisten ( int backlog ) ( int backlog ) [virtual]

    Specifies how many pending connections a server socket can have. Returns true if the operation was successful; otherwise returns false. A backlog value of 50 is quite common.

    The listen() call only applies to sockets where type() is Stream, i.e. not to Datagram sockets. listen() must not be called before bind() or after accept().

    See also bind() and accept().

    Look up this member in the source code.

    Toggle details bool Q3SocketDevice

    openopen ( OpenMode mode ) ( OpenMode mode ) [virtual]

    Reimplemented from QIODevice::open().

    Opens the socket using the specified QIODevice file mode. This function is called from the Q3SocketDevice constructors and from the setSocket() function. You should not call it yourself.

    See also close().

    Look up this member in the source code.

    Toggle details bool Q3SocketDevice

    openopen ( int mode ) ( int mode )

    This is an overloaded function.

    Look up this member in the source code.

    Toggle details QHostAddress Q3SocketDevice

    peerAddresspeerAddress () ()const

    Returns the address of the port this socket device is connected to. This may be 0.0.0.0 for a while, but is set to something sensible as soon as a sensible value is available.

    Note that for Datagram sockets, this is the source port of the last packet received.

    Look up this member in the source code.

    Toggle details Q_UINT16 Q3SocketDevice

    peerPortpeerPort () ()const

    Returns the port number of the port this socket device is connected to. This may be 0 for a while, but is set to something sensible as soon as a sensible value is available.

    Note that for Datagram sockets, this is the source port of the last packet received, and that it is in native byte order.

    Look up this member in the source code.

    Toggle details Q_UINT16 Q3SocketDevice

    portport () ()const

    Returns the port number of this socket device. This may be 0 for a while, but is set to something sensible as soon as a sensible value is available.

    Note that Qt always uses native byte order, i.e. 67 is 67 in Qt; there is no need to call htons().

    Look up this member in the source code.

    Toggle details Protocol Q3SocketDevice

    protocolprotocol () ()const

    Returns the socket's protocol family, which is one of Unknown, IPv4, or IPv6.

    Q3SocketDevice either creates a socket with a well known protocol family or it uses an already existing socket. In the first case, this function returns the protocol family it was constructed with. In the second case, it tries to determine the protocol family of the socket; if this fails, it returns Unknown.

    See also Protocol and setSocket().

    Look up this member in the source code.

    Toggle details qint64 Q3SocketDevice

    readBlockreadBlock ( char *data , Q_ULONG maxlen ) ( char *data , Q_ULONG maxlen )

    Reads maxlen bytes from the socket into data and returns the number of bytes read. Returns -1 if an error occurred. Returning 0 is not an error. For Stream sockets, 0 is returned when the remote host closes the connection. For Datagram sockets, 0 is a valid datagram size.

    Look up this member in the source code.

    Toggle details qint64 Q3SocketDevice

    readDatareadData ( char *data , qint64 maxlen ) ( char *data , qint64 maxlen ) [virtual protected]

    Reimplemented from QIODevice::readData().

    Reads maxlen bytes from the socket into data and returns the number of bytes read. Returns -1 if an error occurred.

    Look up this member in the source code.

    Toggle details int Q3SocketDevice

    receiveBufferSizereceiveBufferSize () ()const

    Returns the size of the operating system receive buffer.

    See also setReceiveBufferSize().

    Look up this member in the source code.

    Toggle details int Q3SocketDevice

    sendBufferSizesendBufferSize () ()const

    Returns the size of the operating system send buffer.

    See also setSendBufferSize().

    Look up this member in the source code.

    Toggle details void Q3SocketDevice

    setAddressReusablesetAddressReusable ( bool enable ) ( bool enable ) [virtual]

    Sets the address of this socket to be usable by other sockets too if enable is true, and to be used exclusively by this socket if enable is false.

    When a socket is reusable, other sockets can use the same port number (and IP address), which is generally useful. Of course other sockets cannot use the same (address,port,peer-address,peer-port) 4-tuple as this socket, so there is no risk of confusing the two TCP connections.

    See also addressReusable().

    Look up this member in the source code.

    Toggle details void Q3SocketDevice

    setBlockingsetBlocking ( bool enable ) ( bool enable ) [virtual]

    Makes the socket blocking if enable is true or nonblocking if enable is false.

    Sockets are blocking by default, but we recommend using nonblocking socket operations, especially for GUI programs that need to be responsive.

    Warning: On Windows, this function should be used with care since whenever you use a QSocketNotifier on Windows, the socket is immediately made nonblocking.

    See also blocking() and isValid().

    Look up this member in the source code.

    Toggle details void Q3SocketDevice

    setErrorsetError ( Error err ) ( Error err )[protected]

    Allows subclasses to set the error state to err.

    See also error().

    Look up this member in the source code.

    Toggle details void Q3SocketDevice

    setReceiveBufferSizesetReceiveBufferSize ( uint size ) ( uint size ) [virtual]

    Sets the size of the operating system receive buffer to size.

    The operating system receive buffer size effectively limits two things: how much data can be in transit at any one moment, and how much data can be received in one iteration of the main event loop.

    The default is operating system-dependent. A socket that receives large amounts of data is probably best with a buffer size of 49152.

    See also receiveBufferSize().

    Look up this member in the source code.

    Toggle details void Q3SocketDevice

    setSendBufferSizesetSendBufferSize ( uint size ) ( uint size ) [virtual]

    Sets the size of the operating system send buffer to size.

    The operating system send buffer size effectively limits how much data can be in transit at any one moment.

    The default is operating system-dependent. A socket that sends large amounts of data is probably best with a buffer size of 49152.

    See also sendBufferSize().

    Look up this member in the source code.

    Toggle details void Q3SocketDevice

    setSocketsetSocket ( int socket , Type type ) ( int socket , Type type ) [virtual]

    Sets the socket device to operate on the existing socket socket.

    The type argument must match the actual socket type; use Q3SocketDevice::Stream for a reliable, connection-oriented TCP socket, or Q3SocketDevice::Datagram for an unreliable, connectionless UDP socket.

    Any existing socket is closed.

    See also socket(), isValid(), and close().

    Look up this member in the source code.

    Toggle details Offset Q3SocketDevice

    sizesize () ()const [virtual]

    Reimplemented from QIODevice::size().

    The size is meaningless for a socket, therefore this function returns 0.

    Look up this member in the source code.

    Toggle details int Q3SocketDevice

    socketsocket () ()const

    Returns the socket number, or -1 if it is an invalid socket.

    See also setSocket(), isValid(), and type().

    Look up this member in the source code.

    Toggle details Type Q3SocketDevice

    typetype () ()const

    Returns the socket type which is either Q3SocketDevice::Stream or Q3SocketDevice::Datagram.

    See also socket().

    Look up this member in the source code.

    Toggle details Q_LONG Q3SocketDevice

    waitForMorewaitForMore ( int msecs , bool *timeout=0 ) ( int msecs , bool *timeout=0 )const

    Wait up to msecs milliseconds for more data to be available. If msecs is -1 the call will block indefinitely.

    Returns the number of bytes available for reading, or -1 if an error occurred.

    If timeout is non-null and no error occurred (i.e. it does not return -1): this function sets *timeout to true, if the reason for returning was that the timeout was reached; otherwise it sets *timeout to false. This is useful to find out if the peer closed the connection.

    Warning: This is a blocking call and should be avoided in event driven applications.

    See also bytesAvailable().

    Look up this member in the source code.

    Toggle details Q_LONG Q3SocketDevice

    writeBlockwriteBlock ( const char *data , Q_ULONG len ) ( const char *data , Q_ULONG len )

    Writes len bytes to the socket from data and returns the number of bytes written. Returns -1 if an error occurred.

    This is used for Q3SocketDevice::Stream sockets.

    Look up this member in the source code.

    Toggle details Q_LONG Q3SocketDevice

    writeBlockwriteBlock ( const char *data , Q_ULONG len , const QHostAddress &host , Q_UINT16 port ...) ( const char *data , Q_ULONG len , const QHostAddress &host , Q_UINT16 port ) [virtual]

    This is an overloaded function.

    Writes len bytes to the socket from data and returns the number of bytes written. Returns -1 if an error occurred.

    This is used for Q3SocketDevice::Datagram sockets. You must specify the host and port of the destination of the data.

    Look up this member in the source code.

    Toggle details qint64 Q3SocketDevice

    writeDatawriteData ( const char *data , qint64 len ) ( const char *data , qint64 len ) [virtual protected]

    Reimplemented from QIODevice::writeData().

    Writes len bytes to the socket from data and returns the number of bytes written. Returns -1 if an error occurred.

    This is used for Q3SocketDevice::Stream sockets.

    Look up this member in the source code.

      Notes provided by the Qt Community

      No notes