
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQ3SocketDevice class provides a platform-independent low-level socket API.More...
| Header: | #include <Q3SocketDevice> |
| Inherits: | QIODevice |
Note: All functions in this class arereentrant.
| enum | Error { NoError, AlreadyBound, Inaccessible, NoResources, ..., UnknownError } |
| enum | Protocol { IPv4, IPv6, Unknown } |
| enum | Type { Stream, Datagram } |
| Q3SocketDevice(Type type = Stream) | |
| Q3SocketDevice(Type type, Protocol protocol, int dummy) | |
| Q3SocketDevice(int socket, Type type) | |
| virtual | ~Q3SocketDevice() |
| virtual int | accept() |
| QHostAddress | address() const |
| bool | addressReusable() const |
| Offset | at() const |
| bool | at(Offset offset) |
| virtual bool | bind(const QHostAddress & address, Q_UINT16 port) |
| bool | blocking() const |
| virtual bool | connect(const QHostAddress & addr, Q_UINT16 port) |
| Error | error() const |
| bool | flush() |
| bool | isValid() const |
| virtual bool | listen(int backlog) |
| bool | open(int mode) |
| QHostAddress | peerAddress() const |
| Q_UINT16 | peerPort() const |
| Q_UINT16 | port() const |
| Protocol | protocol() const |
| qint64 | readBlock(char * data, Q_ULONG maxlen) |
| int | receiveBufferSize() const |
| int | sendBufferSize() const |
| virtual void | setAddressReusable(bool enable) |
| virtual void | setBlocking(bool enable) |
| 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 |
| Q_LONG | writeBlock(const char * data, Q_ULONG len) |
| virtual Q_LONG | writeBlock(const char * data, Q_ULONG len, const QHostAddress & host, Q_UINT16 port) |
| virtual bool | atEnd() const |
| virtual qint64 | bytesAvailable() const |
| virtual void | close() |
| virtual bool | open(OpenMode mode) |
| virtual Offset | size() const |
| void | setError(Error err) |
| virtual qint64 | readData(char * data, qint64 maxlen) |
| virtual qint64 | writeData(const char * data, qint64 len) |
TheQ3SocketDevice 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 theQ3Socket class provides a much easier and high level alternative, but certain things (like UDP) can't be done withQ3Socket and if you need a platform-independent API for those,Q3SocketDevice is the right choice.
The essential purpose of the class is to provide aQIODevice that works on sockets, wrapped in a platform-independent API.
When callingconnect() orbind(),Q3SocketDevice detects the protocol family (IPv4, IPv6) automatically. Passing the protocol family toQ3SocketDevice's constructor or tosetSocket() forces creation of a socket device of a specific protocol. If not set, the protocol will be detected at the first call toconnect() orbind().
See alsoQ3Socket,QSocketNotifier, andQHostAddress.
This enum type describes the error states ofQ3SocketDevice.
| Constant | Value | Description |
|---|---|---|
Q3SocketDevice::NoError | 0 | No error has occurred. |
Q3SocketDevice::AlreadyBound | 1 | The device is already bound, according tobind(). |
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 inQ3SocketDevice. |
Q3SocketDevice::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, butQ3SocketDevice is not aware of this. So when you callwriteBlock(), the impossible happens.
| Constant | Value | Description |
|---|---|---|
Q3SocketDevice::NoFiles | ? | The operating system will not letQ3SocketDevice 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. |
This enum type describes the protocol family of the socket. Possible values are:
| Constant | Value | Description |
|---|---|---|
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 useQ3SocketDevice with an already existing socket; it tries to determine the protocol family, but this can fail if the protocol family is not known toQ3SocketDevice. |
See alsoprotocol() andsetSocket().
This enum type describes the type of the socket:
| Constant | Value | Description |
|---|---|---|
Q3SocketDevice::Stream | 0 | a stream socket (TCP, usually) |
Q3SocketDevice::Datagram | 1 | a datagram socket (UDP, usually) |
Creates aQ3SocketDevice object for a stream or datagram socket.
Thetype argument must be eitherQ3SocketDevice::Stream for a reliable, connection-oriented TCP socket, orQ3SocketDevice::Datagram for an unreliable UDP socket.
The socket is created as an IPv4 socket.
See alsoblocking() andprotocol().
Creates aQ3SocketDevice object for a stream or datagram socket.
Thetype argument must be eitherQ3SocketDevice::Stream for a reliable, connection-oriented TCP socket, orQ3SocketDevice::Datagram for an unreliable UDP socket.
Theprotocol indicates whether the socket should be of type IPv4 or IPv6. PassingUnknown is not meaningful in this context and you should avoid using (it creates an IPv4 socket, but your code is not easily readable).
The argumentdummy is necessary for compatibility with some compilers.
See alsoblocking() andprotocol().
Creates aQ3SocketDevice object for the existing socketsocket.
Thetype argument must match the actual socket type; useQ3SocketDevice::Stream for a reliable, connection-oriented TCP socket, orQ3SocketDevice::Datagram for an unreliable, connectionless UDP socket.
[virtual]Q3SocketDevice::~Q3SocketDevice()Destroys the socket device and closes the socket if it is open.
[virtual]int Q3SocketDevice::accept()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 as soon as a sensible value is available.
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 alsosetAddressReusable().
The read/write index is meaningless for a socket, therefore this function returns 0.
The read/write index is meaningless for a socket, therefore this function does nothing and returns true.
Theoffset parameter is ignored.
[virtual]bool Q3SocketDevice::atEnd() constReimplemented fromQIODevice::atEnd().
Returns true if no data is currently available at the socket; otherwise returns false.
[virtual]bool Q3SocketDevice::bind(constQHostAddress & address,Q_UINT16 port)Assigns a name to an unnamed socket. The name is the host addressaddress and the port numberport. If the operation succeeds, bind() returns true; otherwise it returns false without changing whatport() andaddress() return.
bind() is used by servers for setting up incoming connections. Call bind() beforelisten().
Returns true if the socket is valid and in blocking mode; otherwise returns false.
Note that this function does not seterror().
Warning: On Windows, this function always returns true since the ioctlsocket() function is broken.
See alsosetBlocking() andisValid().
[virtual]qint64 Q3SocketDevice::bytesAvailable() constReimplemented fromQIODevice::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 usingreadBlock().QSocket has workarounds to deal with this problem.
[virtual]void Q3SocketDevice::close()Reimplemented fromQIODevice::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 alsoopen().
[virtual]bool Q3SocketDevice::connect(constQHostAddress & addr,Q_UINT16 port)Connects to the IP address and port specified byaddr andport. Returns true if it establishes a connection; otherwise returns false. If it returns false,error() explains why.
Note thaterror() commonly returnsNoError 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.
See alsosetError().
The currentQ3SocketDevice implementation does not buffer at all, so this is a no-op. This function always returns true.
Returns true if this is a valid socket; otherwise returns false.
See alsosocket().
[virtual]bool Q3SocketDevice::listen(int backlog)Specifies how many pending connections a server socket can have. Returns true if the operation was successful; otherwise returns false. Abacklog value of 50 is quite common.
The listen() call only applies to sockets wheretype() isStream, i.e. not toDatagram sockets. listen() must not be called beforebind() or afteraccept().
[virtual]bool Q3SocketDevice::open(OpenMode mode)Reimplemented fromQIODevice::open().
Opens the socket using the specifiedQIODevice filemode. This function is called from theQ3SocketDevice constructors and from thesetSocket() function. You should not call it yourself.
See alsoclose().
This is an overloaded function.
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.
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.
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().
Returns the socket's protocol family, which is one ofUnknown,IPv4, orIPv6.
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 returnsUnknown.
See alsoProtocol andsetSocket().
Readsmaxlen bytes from the socket intodata 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.
[virtual protected]qint64 Q3SocketDevice::readData(char * data,qint64 maxlen)Reimplemented fromQIODevice::readData().
Readsmaxlen bytes from the socket intodata and returns the number of bytes read. Returns -1 if an error occurred.
Returns the size of the operating system receive buffer.
See alsosetReceiveBufferSize().
Returns the size of the operating system send buffer.
See alsosetSendBufferSize().
[virtual]void Q3SocketDevice::setAddressReusable(bool enable)Sets the address of this socket to be usable by other sockets too ifenable is true, and to be used exclusively by this socket ifenable 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 alsoaddressReusable().
[virtual]void Q3SocketDevice::setBlocking(bool enable)Makes the socket blocking ifenable is true or nonblocking ifenable 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 aQSocketNotifier on Windows, the socket is immediately made nonblocking.
See alsoblocking() andisValid().
[protected]void Q3SocketDevice::setError(Error err)Allows subclasses to set the error state toerr.
See alsoerror().
[virtual]void Q3SocketDevice::setReceiveBufferSize(uint size)Sets the size of the operating system receive buffer tosize.
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 alsoreceiveBufferSize().
[virtual]void Q3SocketDevice::setSendBufferSize(uint size)Sets the size of the operating system send buffer tosize.
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 alsosendBufferSize().
[virtual]void Q3SocketDevice::setSocket(int socket,Type type)Sets the socket device to operate on the existing socketsocket.
Thetype argument must match the actual socket type; useQ3SocketDevice::Stream for a reliable, connection-oriented TCP socket, orQ3SocketDevice::Datagram for an unreliable, connectionless UDP socket.
Any existing socket is closed.
See alsosocket(),isValid(), andclose().
[virtual]Offset Q3SocketDevice::size() constReimplemented fromQIODevice::size().
The size is meaningless for a socket, therefore this function returns 0.
Returns the socket number, or -1 if it is an invalid socket.
See alsosetSocket(),isValid(), andtype().
Returns the socket type which is eitherQ3SocketDevice::Stream orQ3SocketDevice::Datagram.
See alsosocket().
Wait up tomsecs milliseconds for more data to be available. Ifmsecs is -1 the call will block indefinitely.
Returns the number of bytes available for reading, or -1 if an error occurred.
Iftimeout 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 alsobytesAvailable().
Writeslen bytes to the socket fromdata and returns the number of bytes written. Returns -1 if an error occurred.
This is used forQ3SocketDevice::Stream sockets.
[virtual]Q_LONG Q3SocketDevice::writeBlock(constchar * data,Q_ULONG len, constQHostAddress & host,Q_UINT16 port)This is an overloaded function.
Writeslen bytes to the socket fromdata and returns the number of bytes written. Returns -1 if an error occurred.
This is used forQ3SocketDevice::Datagram sockets. You must specify thehost andport of the destination of the data.
[virtual protected]qint64 Q3SocketDevice::writeData(constchar * data,qint64 len)Reimplemented fromQIODevice::writeData().
Writeslen bytes to the socket fromdata and returns the number of bytes written. Returns -1 if an error occurred.
This is used forQ3SocketDevice::Stream sockets.
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.