Eneboo - Documentación para desarrolladores
|
The QSocketDevice class provides a platform-independent low-level socket API.network. Más...
#include <qsocketdevice.h>
The QSocketDevice class provides a platform-independent low-level socket API.
network.
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().
enum QSocketDevice::Error |
This enum type describes the error states of QSocketDevice.
NoError No error has occurred.
AlreadyBound The device is already bound, according to bind().
Inaccessible The operating system or firewall prohibited the action.
NoResources The operating system ran out of a resource.
InternalError An internal error occurred in 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(), the impossible happens.
NoFiles The operating system will not let QSocketDevice open another file.
ConnectionRefused A connection attempt was rejected by the peer.
NetworkFailure There is a network failure.
UnknownError The operating system did something unexpected.
enum QSocketDevice::Error |
This enum type describes the protocol family of the socket. Possible values are:
IPv4 The socket is an IPv4 socket. IPv6 The socket is an IPv6 socket. 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.
enum QSocketDevice::Type |
enum QSocketDevice::Type |
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 an unreliable UDP socket.
The socket is created as an IPv4 socket.
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 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.
Creates a QSocketDevice object for the existing socket socket.
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.
QSocketDevice::~QSocketDevice | ( | ) | [virtual] |
Destroys the socket device and closes the socket if it is open.
QSocketDevice::QSocketDevice | ( | Type | type = Stream | ) |
virtual QSocketDevice::~QSocketDevice | ( | ) | [virtual] |
int QSocketDevice::accept | ( | ) | [virtual] |
virtual int QSocketDevice::accept | ( | ) | [virtual] |
QHostAddress QSocketDevice::address | ( | ) | 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.
QHostAddress QSocketDevice::address | ( | ) | const |
bool QSocketDevice::addressReusable | ( | ) | const |
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.
The read/write index is meaningless for a socket, therefore this function does nothing and returns TRUE.
Reimplementado de QIODevice.
Offset QSocketDevice::at | ( | ) | const [virtual] |
QIODevice::Offset QSocketDevice::at | ( | ) | const [virtual] |
The read/write index is meaningless for a socket, therefore this function returns 0.
Reimplementado de QIODevice.
bool QSocketDevice::atEnd | ( | ) | const [virtual] |
Virtual function that returns TRUE if the I/O device position is at the end of the input; otherwise returns FALSE.
Reimplementado de QIODevice.
bool QSocketDevice::atEnd | ( | ) | const [virtual] |
Returns TRUE if no data is currently available at the socket; otherwise returns FALSE.
Reimplementado de QIODevice.
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().
virtual bool QSocketDevice::bind | ( | const QHostAddress & | , |
Q_UINT16 | |||
) | [virtual] |
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().
bool QSocketDevice::blocking | ( | ) | const |
Q_LONG QSocketDevice::bytesAvailable | ( | ) | const |
Returns the number of bytes available for reading, or -1 if an error occurred.
Q_LONG QSocketDevice::bytesAvailable | ( | ) | const |
void QSocketDevice::close | ( | void | ) | [virtual] |
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-specfic errors that we haven't come across, however.)
Implementa QIODevice.
void QSocketDevice::close | ( | void | ) | [virtual] |
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.
virtual bool QSocketDevice::connect | ( | const QHostAddress & | , |
Q_UINT16 | |||
) | [virtual] |
QSocketDevice::Error QSocketDevice::error | ( | ) | const |
Returns the first error seen.
Error QSocketDevice::error | ( | ) | const |
void QSocketDevice::flush | ( | ) | [virtual] |
The current QSocketDevice implementation does not buffer at all, so this is a no-op.
Implementa QIODevice.
void QSocketDevice::flush | ( | ) | [virtual] |
Flushes an open I/O device.
This virtual function must be reimplemented by all subclasses.
Implementa QIODevice.
int QSocketDevice::getch | ( | ) | [virtual] |
int QSocketDevice::getch | ( | ) | [virtual] |
Implementa QIODevice.
bool QSocketDevice::isValid | ( | ) | const |
bool QSocketDevice::isValid | ( | void | ) | const |
Returns TRUE if this is a valid socket; otherwise returns FALSE.
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().
Opens the I/O device using the specified mode. Returns TRUE if the device was successfully opened; otherwise returns FALSE.
The mode parameter mode must be an OR'ed combination of the following flags. Mode flags Meaning IO_Raw
specifies raw (unbuffered) file access. IO_ReadOnly
opens a file in read-only mode. IO_WriteOnly
opens a file in write-only mode. IO_ReadWrite
opens a file in read/write mode. IO_Append
sets the file index to the end of the file. IO_Truncate
truncates the file. IO_Translate
enables carriage returns and linefeed translation for text files under MS-DOS, Windows and Macintosh. On Unix systems this flag has no effect. Use with caution as it will also transform every linefeed written to the file into a CRLF pair. This is likely to corrupt your file if you write write binary data. Cannot be combined with IO_Raw
.
This virtual function must be reimplemented by all subclasses.
Implementa QIODevice.
Opens the socket using the specified QIODevice file mode. This function is called from the QSocketDevice constructors and from the setSocket() function. You should not call it yourself.
Implementa QIODevice.
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.
QHostAddress QSocketDevice::peerAddress | ( | ) | const |
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::peerPort | ( | ) | const |
Q_UINT16 QSocketDevice::port | ( | ) | const |
Q_UINT16 QSocketDevice::port | ( | ) | const |
Protocol QSocketDevice::protocol | ( | ) | const |
QSocketDevice::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
.
Implementa QIODevice.
Q_LONG QSocketDevice::readBlock | ( | char * | data, |
Q_ULONG | maxlen | ||
) | [virtual] |
Reads at most maxlen bytes from the I/O device into data and returns the number of bytes actually read.
This function should return -1 if a fatal error occurs and should return 0 if there are no bytes to read.
The device must be opened for reading, and data must not be 0.
This virtual function must be reimplemented by all subclasses.
Implementa QIODevice.
Q_LONG QSocketDevice::readBlock | ( | char * | data, |
Q_ULONG | maxlen | ||
) | [virtual] |
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.
Implementa QIODevice.
int QSocketDevice::receiveBufferSize | ( | ) | const |
Returns the size of the operating system receive buffer.
int QSocketDevice::receiveBufferSize | ( | ) | const |
int QSocketDevice::sendBufferSize | ( | ) | const |
Returns the size of the operating system send buffer.
int QSocketDevice::sendBufferSize | ( | ) | const |
void QSocketDevice::setAddressReusable | ( | 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.
virtual void QSocketDevice::setAddressReusable | ( | bool | ) | [virtual] |
virtual void QSocketDevice::setBlocking | ( | bool | ) | [virtual] |
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.
void QSocketDevice::setError | ( | Error | err | ) | [protected] |
void QSocketDevice::setError | ( | Error | err | ) | [protected] |
Allows subclasses to set the error state to err.
void QSocketDevice::setReceiveBufferSize | ( | 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.
virtual void QSocketDevice::setReceiveBufferSize | ( | uint | ) | [virtual] |
void QSocketDevice::setSendBufferSize | ( | 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.
virtual void QSocketDevice::setSendBufferSize | ( | uint | ) | [virtual] |
Sets the socket device to operate on the existing socket socket.
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.
Offset QSocketDevice::size | ( | ) | const [virtual] |
QIODevice::Offset QSocketDevice::size | ( | ) | const [virtual] |
The size is meaningless for a socket, therefore this function returns 0.
Implementa QIODevice.
int QSocketDevice::socket | ( | ) | const |
int QSocketDevice::socket | ( | ) | const |
QSocketDevice::Type QSocketDevice::type | ( | ) | const |
Returns the socket type which is either QSocketDevice::Stream
or QSocketDevice::Datagram
.
Type QSocketDevice::type | ( | ) | const |
This implementation of ungetch returns -1 (error). A socket is a sequential device and does not allow any ungetch operation.
Implementa QIODevice.
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.
virtual Q_LONG QSocketDevice::writeBlock | ( | const char * | data, |
Q_ULONG | len, | ||
const QHostAddress & | host, | ||
Q_UINT16 | port | ||
) | [virtual] |
Q_LONG QSocketDevice::writeBlock | ( | const char * | data, |
Q_ULONG | 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.
Implementa QIODevice.
Q_LONG QSocketDevice::writeBlock | ( | const char * | data, |
Q_ULONG | len | ||
) | [virtual] |
Writes len bytes from data to the I/O device and returns the number of bytes actually written.
This function should return -1 if a fatal error occurs.
This virtual function must be reimplemented by all subclasses.
Implementa QIODevice.
Q_LONG QSocketDevice::writeBlock | ( | const char * | data, |
Q_ULONG | len, | ||
const QHostAddress & | host, | ||
Q_UINT16 | port | ||
) | [virtual] |
Esta es una función miembro sobrecargada que se suministra por conveniencia. Difiere de la anterior función solamente en los argumentos que acepta. 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.