The QSocketDevice class provides a platform-independent low-level socket API. More...
#include <qsocketdevice.h>
Inherits QIODevice.
This class is not really meant for use outside Qt. It can be used to achieve some things that QSocket does not provide, but it's not particularly easy to understand or use.
The basic purpose of the class is to provide a QIODevice that works on sockets, wrapped in a platform-independent API.
See also QSocket, QSocketNotifier and QHostAddress.
NoError
- all is fine.
AlreadyBound
- bind() said so.
Inaccessible
- the operating system or firewall prohibits something.
NoResources
- the operating system ran out of something.
Bug
- there seems to be a bug in QSocketDevice.
Impossible
- the impossible happened, usually because you confused
QSocketDevice horribly. Simple example:
::close( sd->socket() ); sd->writeBlock( someData, 42 );
The libc ::close() closes the socket, but QSocketDevice is not aware of that. 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 between this host
and... and whatever.
UnknownError
- the operating system reacted in a way that the
Qt developers did not foresee.
Stream
- a stream socket
Datagram
- a datagram 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.
See also blocking().
Creates a QSocketDevice object for an existing socket.
The type argument must match the actual socket type;
QSocketDevice::Stream
for a reliable, connection-oriented TCP socket, or
QSocketDevice::Datagram
for an unreliable, connectionless UDP socket.
[virtual]
Destroys the socket device and closes the socket if it is open.
[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.
Returns the address of this socket device. This may be 0.0.0.0 for a while, but is set to something sensible when there is a sensible value it can have.
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().
[virtual]
Reimplemented for internal reasons; the API is not affected.
The read/write index is meaningless for a socket, therefore this function does nothing and returns TRUE.
Reimplemented from QIODevice.
[virtual]
Reimplemented for internal reasons; the API is not affected.
The read/write index is meaningless for a socket, therefore this function returns 0.
Reimplemented from QIODevice.
[virtual]
Reimplemented for internal reasons; the API is not affected.
The read/write index is meaningless for a socket, therefore this always returns FALSE.
Reimplemented from QIODevice.
[virtual]
Assigns a name to an unnamed socket. 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().
Returns TRUE if the socket is in blocking mode, or FALSE if it is in nonblocking mode or if the socket is invalid.
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().
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 return 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.
[virtual]
Reimplemented for internal reasons; the API is not affected.
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.)
See also open().
Reimplemented from QIODevice.
[virtual]
Connects to the IP address and port specified by addr. Returns TRUE if it establishes a connection, and FALSE if not. 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.
Returns the first error seen.
[virtual]
Reimplemented for internal reasons; the API is not affected.
QSocketDevice does not buffer at all, so this is a no-op.
Reimplemented from QIODevice.
[virtual]
Reimplemented for internal reasons; the API is not affected.
Warning: getch() is implemented as a one-byte readBlock(), so it may be very slow if you call it more than a few times.
See also putch() and readBlock().
Reimplemented from QIODevice.
Returns TRUE if this is a valid socket or FALSE if it is an invalid socket. This is actually a shortcut for socket() == -1.
See also socket().
[virtual]
Specifies how many pending connections a server socket can have. Returns TRUE if the operation was successful, otherwise FALSE.
The listen() call only applies to sockets where type() is Stream,
not Datagram
sockets. listen() must not be called before bind()
or after accept(). It is common to use a backlog value of 50 on
most Unix systems.
[virtual]
Reimplemented for internal reasons; the API is not affected.
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.
See also close().
Reimplemented from QIODevice.
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 when there is a sensible value it can have.
Note that for Datagram sockets, this is the source address of the last packet received.
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 when there is a sensible value it can have.
Note that for Datagram sockets, this is the source port of the last packet received.
Returns the port number of this socket device. This may be 0 for a while, but is set to something sensible when there is a sensible value it can have.
[virtual]
Reimplemented for internal reasons; the API is not affected.
Warning: putch() is implemented as a one-byte writeBlock(), so it may be very slow if you call it more than a few times.
See also getch().
Reimplemented from QIODevice.
[virtual]
Reads max maxlen bytes from the socket into data and returns the number of bytes read. Returns -1 if an error occurred.
Reimplemented from QIODevice.
Returns the size of the OS receive buffer.
See also setReceiveBufferSize().
Returns the size of the OS send buffer.
See also setSendBufferSize().
[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 in general good. 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().
[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 does nothing since the ioctlsocket() function is broken.
Whenever you use a QSocketNotifier on Windows, the socket is immediately made nonblocking.
See also blocking() and isValid().
[protected]
Allows subclasses to set the error state.
[virtual]
Sets the size of the OS receive buffer to size.
The OS 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 OS-dependent. A socket that receives large amounts of data is probably best off with a buffer size of 49152.
[virtual]
Sets the size of the OS send buffer to size.
The OS send buffer size effectively limits how much data can be in transit at any one moment.
The default is OS-dependent. A socket that sends large amounts of data is probably best off with a buffer size of 49152.
Sets an existing socket.
The type argument must match the actual socket type;
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().
[virtual]
Reimplemented for internal reasons; the API is not affected.
The size is meaningless for a socket, therefore this function returns 0.
Reimplemented from QIODevice.
Returns the socket number, or -1 if it is an invalid socket.
See also isValid() and type().
Returns the socket type; QSocketDevice::Stream
for a reliable,
connection-oriented TCP socket, or QSocketDevice::Datagram
for an
unreliable UDP socket.
See also socket().
[virtual]
Reimplemented for internal reasons; the API is not affected.
This implementation of ungetch returns -1 (error). A socket is a sequential device and does not allow any ungetch operation.
Reimplemented from QIODevice.
Wait up to msecs milliseconds for more data to be available. If msecs is -1 the call will block indefinitely.
This is a blocking call and should be avoided in event driven applications.
Returns the number of bytes available for reading, or -1 if an error occurred.
See also bytesAvailable().
[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.
[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::Datagram
sockets. You have to specify the
host and port of the destination of the data.
Search the documentation, FAQ, qt-interest archive and more (uses
www.trolltech.com):
This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.
Copyright © 2000 Trolltech | Trademarks | Qt version 2.2.1
|