QSocketDevice(3qt)

πŸ“¦ version 3.3.8b, in fedora 43, fedora 44

✨ QSocketDevice ✨ β€” the QSocketDevice(3qt) manual, bestie edition πŸ’…

okay so you opened the QSocketDevice(3qt) page and honestly? iconic of you. this is the low-level socket class from Qt 3, and she is not for the faint of heart. grab a snack, we're going in 🫢


🌸 NAME 🌸

QSocketDevice β€” the platform-independent low-level socket API. that's it. that's the whole vibe. πŸ¦‹


✨ SYNOPSIS ✨

#include <qsocketdevice.h>

she Inherits QIODevice. periodt.

πŸŽ€ Public Members

  • enum Type { Stream, Datagram }
  • enum Protocol { IPv4, IPv6, Unknown }
  • QSocketDevice ( Type type = Stream )
  • QSocketDevice ( Type type, Protocol protocol, int dummy )
  • QSocketDevice ( int socket, Type type )
  • virtual ~QSocketDevice ()
  • bool isValid () const
  • Type type () const
  • Protocol protocol () const
  • int socket () const
  • virtual void setSocket ( int socket, Type type )
  • bool blocking () const
  • virtual void setBlocking ( bool enable )
  • bool addressReusable () const
  • virtual void setAddressReusable ( bool enable )
  • int receiveBufferSize () const
  • virtual void setReceiveBufferSize ( uint size )
  • int sendBufferSize () const
  • virtual void setSendBufferSize ( uint size )
  • virtual bool connect ( const QHostAddress & addr, Q_UINT16 port )
  • virtual bool bind ( const QHostAddress & address, Q_UINT16 port )
  • virtual bool listen ( int backlog )
  • virtual int accept ()
  • Q_LONG bytesAvailable () const
  • Q_LONG waitForMore ( int msecs, bool * timeout = 0 ) const
  • virtual Q_LONG readBlock ( char * data, Q_ULONG maxlen )
  • virtual 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 )
  • Q_UINT16 port () const
  • Q_UINT16 peerPort () const
  • QHostAddress address () const
  • QHostAddress peerAddress () const
  • enum Error { NoError, AlreadyBound, Inaccessible, NoResources, InternalError, Bug = InternalError, Impossible, NoFiles, ConnectionRefused, NetworkFailure, UnknownError }
  • Error error () const

🧚 Protected Members

  • void setError ( Error err )

πŸ’– DESCRIPTION πŸ’–

so. the QSocketDevice class provides a platform-independent low-level socket API. no cap, that's the whole assignment. πŸ”₯

this class hands you a low level API for working with sockets, and ngl, you're assumed to already have networking experience going in β€” she's not holding your hand through the basics, bestie. for most users the QSocket class gives you a way easier and much more high level alternative, but certain things (like UDP) literally cannot be done with QSocket, and if you need a platform-independent API for those, QSocketDevice is the right choice. it's giving "for the girlies who need the raw stuff" πŸ’…

the essential purpose of the class is to give you a QIODevice that works on sockets, wrapped up all cute in a platform-independent API.

when you call 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 it's not set, the protocol will be detected at the first call to connect() or bind(). automatic. slay. ✨

See also QSocket, QSocketNotifier, QHostAddress, and Input/Output and Networking. πŸ‘‘


🌈 Member Type Documentation 🌈

QSocketDevice::Error

this enum type describes the error states of QSocketDevice. buckle up, there's a lot of them 🫠

  • QSocketDevice::NoError β€” No error has occurred. the golden child. ✨
  • QSocketDevice::AlreadyBound β€” The device is already bound, according to bind().
  • QSocketDevice::Inaccessible β€” The operating system or firewall prohibited the action. rude, honestly.
  • 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(), the impossible happens. 😭 like girl. she didn't know.
  • QSocketDevice::NoFiles β€” The operating system will not let QSocketDevice open another file.
  • QSocketDevice::ConnectionRefused β€” A connection attempt was rejected by the peer. the peer said no thanks πŸ’€
  • QSocketDevice::NetworkFailure β€” There is a network failure.
  • QSocketDevice::UnknownError β€” The operating system did something unexpected.

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

QSocketDevice::Type

this enum type describes the type of the socket:

  • QSocketDevice::Stream β€” a stream socket (TCP, usually)
  • QSocketDevice::Datagram β€” a datagram socket (UDP, usually)

πŸ’œ 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 an unreliable UDP socket.

The socket is created as an IPv4 socket.

See also blocking() and protocol().


QSocketDevice::QSocketDevice ( Type type, Protocol protocol, int dummy )

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 (it creates an IPv4 socket, but your code is not easily readable). bestie don't do it, it's giving chaos 🚫

The argument dummy is necessary for compatibility with some compilers. yes it's silly. no we don't make the rules. πŸ™ƒ

See also blocking() and protocol().


QSocketDevice::QSocketDevice ( int socket, Type type )

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. match it or she will simply not cooperate πŸ’…


QSocketDevice::~QSocketDevice () [virtual]

Destroys the socket device and closes the socket if it is open. she's leaving and she's taking the socket with her πŸ¦‹


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 for a while, but is set to something sensible as soon as a sensible value is available. she's still figuring herself out okay 😭


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. possessive much πŸ’…

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(). order matters bestie, do not freestyle this one πŸ”₯


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. πŸ’€ windows said "blocking forever, hope this helps"

See also setBlocking() and isValid().


Q_LONG QSocketDevice::bytesAvailable () const

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 using readBlock(). QSocket has workarounds to deal with this problem. 🫠 so like. don't trust her on windows.

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. she's not ghosting you, she's just busy 🫢


Error QSocketDevice::error () const

Returns the first error seen. the first one. she keeps receipts πŸ“


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(). ma'am. the order. respect the order. πŸ‘‘

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 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(). no conversion era, we love to see it πŸ’–


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_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. yes zero can be a whole mood and also a valid datagram, both things are true 🌸

Reimplemented from QIODevice.


more of this manual is coming, bestie β€” don't go anywhere πŸ’…βœ¨

int QSocketDevice::receiveBufferSize () const

okay so this one just tells you how big the operating system's receive buffer is. that's it, that's the whole vibe. 🫢

peep also setReceiveBufferSize().


int QSocketDevice::sendBufferSize () const

and this one's its sibling ✨ it hands you the size of the operating system's send buffer.

peep also setSendBufferSize().


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

bestie, this is the "can we share tho" switch πŸ’…

  • if enable is TRUE, the address of this socket becomes usable by other sockets too
  • if enable is FALSE, this socket gets it exclusively, no sharing, no plus-ones

the way this works is: when a socket is reusable, other sockets can use the same port number (and IP address), which is generally super useful. and no, it's not chaos β€” 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. iconic behavior, truly. πŸ”₯

peep also addressReusable().


void QSocketDevice::setBlocking ( bool enable ) [virtual]

sets the whole mood of the socket: blocking if enable is TRUE, nonblocking if enable is FALSE.

sockets are blocking by default, but ngl we recommend using nonblocking socket operations, especially for GUI programs that need to stay responsive. main character apps don't freeze. ✨

⚠️ warning: on Windows, use this function with care, because whenever you use a QSocketNotifier on Windows, the socket is immediately made nonblocking. like, immediately immediately. no cap.

peep also blocking() and isValid().


void QSocketDevice::setError ( Error err ) [protected]

a lil protected backstage pass πŸŽ€ β€” it lets subclasses set the error state to err.


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

sets the size of the operating system receive buffer to size.

so here's why this matters: the operating system receive buffer size effectively limits two things:

  1. how much data can be in transit at any one moment
  2. how much data can be received in one iteration of the main event loop

the default is operating system-dependent. and if a socket receives large amounts of data, it's probably best with a buffer size of 49152. that's the magic number, slay. πŸ”’


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.

default is operating system-dependent. a socket that sends large amounts of data is probably best with a buffer size of 49152 too. twins. πŸ’–


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

points the socket device at an existing socket, socket. like adopting a socket you already had. πŸ¦‹

the type argument must match the actual socket type:

  • use QSocketDevice::Stream for a reliable, connection-oriented TCP socket
  • use QSocketDevice::Datagram for an unreliable, connectionless UDP socket
⚠️ any existing socket is closed. gone. bye. 😭

peep also isValid() and close().


int QSocketDevice::socket () const

hands you the socket number, or -1 if it's an invalid socket. -1 is giving "no thoughts, no socket". πŸ’€

peep also isValid() and type().


Type QSocketDevice::type () const

returns the socket type, which is either QSocketDevice::Stream or QSocketDevice::Datagram. that's the whole list, periodt.

peep also socket().


Q_LONG QSocketDevice::waitForMore ( int msecs, bool * timeout = 0 ) const

waits up to msecs milliseconds for more data to be available. if msecs is -1, the call will block indefinitely. forever. until the data shows up. πŸ•°οΈ

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

now the timeout part, pay attention bestie:

  • 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. do not do it. i'm serious.

peep also bytesAvailable().


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.

reimplemented from QIODevice.


Q_LONG QSocketDevice::writeBlock ( const char * data, Q_ULONG len, const QHostAddress & host, Q_UINT16 port ) [virtual]

this is an overloaded member function, provided for convenience. it behaves essentially like the above function. ✨ same energy, extra args.

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. and you must specify the host and port of the destination of the data β€” no destination, no delivery, fr fr. πŸ“¬


🌈 SEE ALSO 🌈


Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.


✨ AUTHOR

Generated automatically from the source code.


πŸ› BUGS

if you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. good bug reports help us to help you. thank you. 🫢

the definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. this man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech.

if you find errors in this manual page, please report them to qt-bugs@trolltech.com. please include the name of the manual page (qsocketdevice.3qt) and the Qt version (3.3.8).


and that's the whole page, bestie πŸ’… this one came from Trolltech AS, dated 2 February 2007, and it's the QSocketDevice(3qt) manual β€” version 3.3.8, no cap. go forth and socket responsibly. πŸŽ€πŸ’–βœ¨

πŸ“¦ where this page comes from