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

# QSocketDevice Class Reference

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

    #include <QSocketDevice>

Inherits QIODevice.

- List of all members, including inherited members

### Public Types

- enum Error { NoError, AlreadyBound, Inaccessible, NoResources, InternalError, ..., UnknownError }
- enum Protocol { IPv4, IPv6, Unknown }
- enum Type { Stream, Datagram }

### Public Functions

- QSocketDevice ( Type type = Stream )
- QSocketDevice ( Type type, Protocol protocol, int unused )
- QSocketDevice ( int socket, Type type )
- virtual ~QSocketDevice ()
- virtual int accept ()
- QHostAddress address () const
- bool addressReusable () const
- virtual bool bind ( const QHostAddress & address, Q_UINT16 port )
- bool blocking () const
- Q_LONG bytesAvailable () const
- virtual bool connect ( const QHostAddress & addr, Q_UINT16 port )
- Error error () const
- bool isValid () const
- virtual bool listen ( int backlog )
- QHostAddress peerAddress () const
- Q_UINT16 peerPort () const
- Q_UINT16 port () const
- Protocol protocol () const
- virtual Q_LONGLONG read ( char * data, Q_LONGLONG maxlen )
- int receiveBufferSize () const
- int sendBufferSize () const
- virtual void setAddressReusable ( bool enable )
- virtual void setBlocking ( bool enable )
- void setError ( Error error )
- virtual void setReceiveBufferSize ( uint size )
- virtual void setSendBufferSize ( uint size )
- virtual void setSocket ( int socket, Type type )
- int socket () const
- Type type () const
- Q_LONG waitForMore ( int msecs, bool * timeout = 0 ) const
- virtual Q_LONGLONG write ( const char * data, Q_LONGLONG len )
- virtual Q_LONGLONG write ( const char * data, Q_LONGLONG len, const QHostAddress & host, Q_UINT16 port )
- Q_LONG writeBlock ( const char * data, Q_LONG len, const QHostAddress & host, Q_UINT16 port )

- 36 public functions inherited from QIODevice

### Protected Functions

- 2 protected functions inherited from QIODevice

## Detailed Description

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

QSocketDevice provides a low level API for working with sockets. Users of this class are assumed to have networking experience. For most users, the QSocket class provides a much easier and high level alternative, but certain things (like UDP) cannot be done with QSocket and if you need a platform-independent API for those, QSocketDevice 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(), QSocketDevice detects the protocol family (IPv4, IPv6) automatically. Passing the protocol family to QSocketDevice'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 QSocket, QSocketNotifier, and QHostAddress.

## Member Type Documentation

### enum QSocketDevice::Error

This enum type describes the error states of QSocketDevice.

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

The libc ::close() closes the socket, but QSocketDevice is not aware of this. So when you call writeBlock(), it attempts to do the impossible.

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

### enum QSocketDevice::Protocol

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

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

See also protocol() and setSocket().

### enum QSocketDevice::Type

This enum type describes the type of the socket:

`QSocketDevice::Stream` | A stream socket (usually TCP).
`QSocketDevice::Datagram` | A datagram socket (usually UDP).

## Member Function Documentation

### QSocketDevice::QSocketDevice ( Type type = Stream )

Creates a QSocketDevice object for a stream or datagram socket.

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

The socket is created as an IPv4 socket.

See also blocking() and protocol().

### QSocketDevice::QSocketDevice ( Type type, Protocol protocol, int unused )

Creates a QSocketDevice object for a stream or datagram socket.

The type must be either `QSocketDevice::Stream` for a reliable, connection-oriented TCP socket, or `QSocketDevice::Datagram` for a 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 unused argument is necessary for compatibility with some compilers.

See also blocking() and protocol().

### QSocketDevice::QSocketDevice ( int socket, Type type )

Creates a QSocketDevice object for the existing socket.

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

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

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

### int QSocketDevice::accept () `[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().

### QHostAddress QSocketDevice::address () const

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

### bool QSocketDevice::addressReusable () 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().

### bool QSocketDevice::bind ( 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().

### bool QSocketDevice::blocking () 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().

### Q_LONG QSocketDevice::bytesAvailable () const

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

### bool QSocketDevice::connect ( 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.

### Error QSocketDevice::error () const

Returns the first error seen.

### bool QSocketDevice::isValid () const

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

See also socket().

### bool QSocketDevice::listen ( 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().

### QHostAddress QSocketDevice::peerAddress () 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.

### Q_UINT16 QSocketDevice::peerPort () 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.

### Q_UINT16 QSocketDevice::port () const

Returns the port number of this socket device. This may be 0 initially, 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().

### Protocol QSocketDevice::protocol () const

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

QSocketDevice 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().

### Q_LONGLONG QSocketDevice::read ( char * data, Q_LONGLONG maxlen ) `[virtual]`

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

Reimplemented from QIODevice.

### int QSocketDevice::receiveBufferSize () const

Returns the size of the operating system receive buffer.

See also setReceiveBufferSize().

### int QSocketDevice::sendBufferSize () const

Returns the size of the operating system send buffer.

See also setSendBufferSize().

### void QSocketDevice::setAddressReusable ( bool enable ) `[virtual]`

If enable is true, the address of this socket can be used by other sockets; otherwise the address is used exclusively by this socket.

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().

### void QSocketDevice::setBlocking ( 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().

### void QSocketDevice::setError ( Error error )

Allows subclasses to set the error state.

### void QSocketDevice::setReceiveBufferSize ( uint size ) `[virtual]`

Sets the size of the operating system receive buffer to the given size in bytes.

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 bytes.

### void QSocketDevice::setSendBufferSize ( uint size ) `[virtual]`

Sets the size of the operating system send buffer to the given size in bytes.

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 bytes.

### void QSocketDevice::setSocket ( int socket, Type type ) `[virtual]`

Sets the socket device to operate on the existing socket.

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

Any existing socket is closed.

See also isValid() and close().

### int QSocketDevice::socket () const

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

See also isValid() and type().

### Type QSocketDevice::type () const

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

See also socket().

### Q_LONG QSocketDevice::waitForMore ( 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().

### Q_LONGLONG QSocketDevice::write ( const char * data, Q_LONGLONG len ) `[virtual]`

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 `QSocketDevice::Stream` sockets.

Reimplemented from QIODevice.

### Q_LONGLONG QSocketDevice::write ( const char * data, Q_LONGLONG len, const QHostAddress & host, Q_UINT16 port ) `[virtual]`

This is an overloaded member function, provided for convenience. It behaves essentially like the above 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 `QSocketDevice::Datagram` sockets. You must specify the host and port of the destination of the data.

### Q_LONG QSocketDevice::writeBlock ( const char * data, Q_LONG len, const QHostAddress & host, Q_UINT16 port )

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 `QSocketDevice::Datagram` sockets. You must specify the host and port of the destination of the data.