Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions | ![]() |
The QSocketDevice class provides a platform-independent low-level socket API. More...
#include <qsocketdevice.h>
Inherits QIODevice.
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 QSocket class provides a much easier and high level alternative, but certain things (like UDP) can't 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, QHostAddress, and Input/Output and Networking.
This enum type describes the error states of QSocketDevice.
::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(), the impossible happens.
This enum type describes the protocol family of the socket. Possible values are:
See also protocol() and setSocket().
This enum type describes the type of the socket:
The type argument must be either QSocketDevice::Stream for a reliable, connection-oriented TCP socket, or QSocketDevice::Datagram for an unreliable UDP socket.
The socket is created as an IPv4 socket.
See also blocking() and protocol().
The type argument must be either QSocketDevice::Stream for a reliable, connection-oriented TCP socket, or QSocketDevice::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().
The type argument 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.
See also setAddressReusable().
bind() is used by servers for setting up incoming connections. Call bind() before listen().
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().
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.
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.
See also socket().
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().
Note that for Datagram sockets, this is the source port of the last packet received.
Note that for Datagram sockets, this is the source port of the last packet received, and that it is in native byte order.
Note that Qt always uses native byte order, i.e. 67 is 67 in Qt; there is no need to call htons().
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().
Reimplemented from QIODevice.
See also setReceiveBufferSize().
See also setSendBufferSize().
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().
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().
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.
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.
The type argument 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.
Any existing socket is closed.
See also isValid() and close().
See also isValid() and type().
Returns the socket type which is either QSocketDevice::Stream or QSocketDevice::Datagram.
See also socket().
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().
This is used for QSocketDevice::Stream sockets.
Reimplemented from QIODevice.
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.
This file is part of the Qt toolkit. Copyright © 1995-2004 Trolltech. All Rights Reserved.
Copyright © 2004 Trolltech | Trademarks | Qt 3.3.2
|