返回介绍

QSocketDevice Class

发布于 2019-10-04 15:02:43 字数 14404 浏览 886 评论 0 收藏 0

The QSocketDevice class provides a platform-independent low-level socket API. More...

#include <qsocketdevice.h>

Inherits QIODevice.

List of all member functions.

Public Members

  • enum Type { Stream, Datagram }
  • QSocketDevice ( Typetype = Stream )
  • QSocketDevice ( intsocket, Typetype )
  • virtual ~QSocketDevice ()
  • bool isValid () const
  • Type type () const
  • int socket () const
  • virtual void setSocket ( intsocket, Typetype )
  • bool blocking () const
  • virtual void setBlocking ( boolenable )
  • bool addressReusable () const
  • virtual void setAddressReusable ( boolenable )
  • int receiveBufferSize () const
  • virtual void setReceiveBufferSize ( uintsize )
  • int sendBufferSize () const
  • virtual void setSendBufferSize ( uintsize )
  • virtual bool connect ( constQHostAddress&addr, Q_UINT16port )
  • virtual bool bind ( constQHostAddress&address, Q_UINT16port )
  • virtual bool listen ( intbacklog )
  • virtual int accept ()
  • Q_LONG bytesAvailable () const
  • Q_LONG waitForMore ( intmsecs, bool*timeout = 0 ) const
  • virtual Q_LONG readBlock ( char*data, Q_ULONGmaxlen )
  • virtual Q_LONG writeBlock ( constchar*data, Q_ULONGlen )
  • virtual Q_LONG writeBlock ( constchar*data, Q_ULONGlen, constQHostAddress&host, Q_UINT16port )
  • Q_UINT16 port () const
  • Q_UINT16 peerPort () const
  • QHostAddress address () const
  • QHostAddress peerAddress () const
  • enum Error { NoError, AlreadyBound, Inaccessible, NoResources, Bug, Impossible, NoFiles, ConnectionRefused, NetworkFailure, UnknownError }
  • Error error () const

Protected Members

  • void setError ( Errorerr )

Detailed Description

The QSocketDevice class provides a platform-independent low-level socket API.

This class is not really intended 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 essential purpose of the class is to provide a QIODevice that works on sockets, wrapped in a platform-independent API.

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


Member Type Documentation

QSocketDevice::Error

This enum type describes the error states of QSocketDevice. At present these errors are defined:

  • QSocketDevice::NoError - all is fine.
  • QSocketDevice::AlreadyBound - bind() said so.
  • QSocketDevice::Inaccessible - the operating system or firewall prohibits something.
  • QSocketDevice::NoResources - the operating system ran out of something.
  • QSocketDevice::Bug - there seems to be a bug in QSocketDevice.
  • 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 this. So when you call writeBlock(), the impossible happens.
  • QSocketDevice::NoFiles - the operating system will not let QSocketDevice open another file.
  • QSocketDevice::ConnectionRefused - a connection attempt was rejected by the peer.
  • QSocketDevice::NetworkFailure - there is a network failure between this host and... and whatever.
  • QSocketDevice::UnknownError - the operating system reacted in a way that the Qt developers did not foresee.

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 ( Typetype = 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.

See also blocking().

QSocketDevice::QSocketDevice ( intsocket, Typetype )

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.

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 when there is a sensible value it can have.

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.

See also setAddressReusable().

bool QSocketDevice::bind ( constQHostAddress&address, Q_UINT16port ) [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().

bool QSocketDevice::blocking () const

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

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

bool QSocketDevice::connect ( constQHostAddress&addr, Q_UINT16port ) [virtual]

Connects to the IP address and port specified by addr and port. 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.

Error QSocketDevice::error () const

Returns the first error seen.

bool QSocketDevice::isValid () const

Returns TRUE if this is a valid socket; otherwise returns FALSE.

See also socket().

bool QSocketDevice::listen ( intbacklog ) [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.

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 when there is a sensible value it can have.

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

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.

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 when there is a sensible value it can have.

Note that Qt always uses native byte order, i.e. 67 is 67 in Qt, there is no need to call htons().

Q_LONG QSocketDevice::readBlock ( char*data, Q_ULONGmaxlen ) [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.

int QSocketDevice::receiveBufferSize () const

Returns the size of the OS receive buffer.

See also setReceiveBufferSize().

int QSocketDevice::sendBufferSize () const

Returns the size of the OS send buffer.

See also setSendBufferSize().

void QSocketDevice::setAddressReusable ( boolenable ) [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().

void QSocketDevice::setBlocking ( boolenable ) [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 should be used with care since whenever you use a QSocketNotifier on Windows, the socket is immediately made nonblocking.

See also blocking() and isValid().

void QSocketDevice::setError ( Errorerr ) [protected]

Allows subclasses to set the error state to err.

void QSocketDevice::setReceiveBufferSize ( uintsize ) [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.

void QSocketDevice::setSendBufferSize ( uintsize ) [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.

void QSocketDevice::setSocket ( intsocket, Typetype ) [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.

See also isValid() and close().

int QSocketDevice::socket () const

Returns the socket number, or -1 if it is an invalid socket.

See also isValid() and type().

Type QSocketDevice::type () const

Returns the socket type which is either QSocketDevice::Stream or QSocketDevice::Datagram.

See also socket().

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

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.

If timeout is non-null and no error occurred (i.e. it does not return -1), then this function sets timeout out 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.

See also bytesAvailable().

Q_LONG QSocketDevice::writeBlock ( constchar*data, Q_ULONGlen ) [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 ( constchar*data, Q_ULONGlen, constQHostAddress&host, Q_UINT16port ) [virtual]

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文