Qyoto  4.0.5
Qyoto is a C# language binding for Qt
 All Classes Namespaces Functions Variables Typedefs Enumerations Properties
QtNetwork.QAbstractSocket Class Reference

The QAbstractSocket class provides the base functionality common to all socket types. More...

Inheritance diagram for QtNetwork.QAbstractSocket:
Collaboration diagram for QtNetwork.QAbstractSocket:

Public Types

enum  NetworkLayerProtocol { IPv4Protocol = 0, IPv6Protocol = 1, UnknownNetworkLayerProtocol = -1 }
  More...
 
enum  SocketError {
  AddressInUseError = 8, ConnectionRefusedError = 0, DatagramTooLargeError = 6, HostNotFoundError = 2,
  NetworkError = 7, ProxyAuthenticationRequiredError = 12, ProxyConnectionClosedError = 15, ProxyConnectionRefusedError = 14,
  ProxyConnectionTimeoutError = 16, ProxyNotFoundError = 17, ProxyProtocolError = 18, RemoteHostClosedError = 1,
  SocketAccessError = 3, SocketAddressNotAvailableError = 9, SocketResourceError = 4, SocketTimeoutError = 5,
  SslHandshakeFailedError = 13, UnfinishedSocketOperationError = 11, UnknownSocketError = -1, UnsupportedSocketOperationError = 10
}
  More...
 
enum  SocketOption { KeepAliveOption = 1, LowDelayOption = 0, MulticastLoopbackOption = 3, MulticastTtlOption = 2 }
  More...
 
enum  SocketState {
  BoundState = 4, ClosingState = 6, ConnectedState = 3, ConnectingState = 2,
  HostLookupState = 1, ListeningState = 5, UnconnectedState = 0
}
  More...
 
enum  SocketType { TcpSocket = 0, UdpSocket = 1, UnknownSocketType = -1 }
  More...
 
- Public Types inherited from QtCore.QIODevice
enum  OpenModeFlag {
  Append = 4, NotOpen = 0, ReadOnly = 1, ReadWrite = 3,
  Text = 16, Truncate = 8, Unbuffered = 32, WriteOnly = 2
}
  More...
 

Public Member Functions

 QAbstractSocket (QAbstractSocket.SocketType socketType, QObject parent)
 
 
override void CreateProxy ()
 
new void Abort ()
 
 
override bool AtEnd ()
 
 
override long BytesAvailable ()
 
 
override long BytesToWrite ()
 
 
override bool CanReadLine ()
 
 
override void Close ()
 
 
new void ConnectToHost (QHostAddress address, ushort port, QIODevice.OpenModeFlag mode=QIODevice.OpenModeFlag.ReadWrite)
 
 
new void ConnectToHost (string hostName, ushort port, QIODevice.OpenModeFlag mode=QIODevice.OpenModeFlag.ReadWrite)
 
 
new void DisconnectFromHost ()
 
 
new QAbstractSocket.SocketError OnError ()
 
 
new bool Flush ()
 
 
override bool IsSequential ()
 
 
new bool IsValid ()
 
 
new QHostAddress LocalAddress ()
 
 
new ushort LocalPort ()
 
 
new QHostAddress PeerAddress ()
 
 
new string PeerName ()
 
 
new ushort PeerPort ()
 
 
new bool SetSocketDescriptor (int socketDescriptor, QAbstractSocket.SocketState state=QAbstractSocket.SocketState.ConnectedState, QIODevice.OpenModeFlag openMode=QIODevice.OpenModeFlag.ReadWrite)
 
 
new void SetSocketOption (QAbstractSocket.SocketOption option, object value)
 
 
new int SocketDescriptor ()
 
 
new object socketOption (QAbstractSocket.SocketOption option)
 
 
new QAbstractSocket.SocketType socketType ()
 
 
new QAbstractSocket.SocketState State ()
 
 
override bool WaitForBytesWritten (int msecs=30000)
 
 
new bool WaitForConnected (int msecs=30000)
 
 
new bool WaitForDisconnected (int msecs=30000)
 
 
override bool WaitForReadyRead (int msecs=30000)
 
 
new void Dispose ()
 
- Public Member Functions inherited from QtCore.QIODevice
 QIODevice ()
 
 
 QIODevice (QObject parent)
 
 
new string ErrorString ()
 
 
new bool GetChar (Pointer< sbyte > c)
 
 
new bool IsOpen ()
 
 
new bool IsReadable ()
 
 
new bool IsTextModeEnabled ()
 
 
new bool IsWritable ()
 
 
virtual bool Open (QIODevice.OpenModeFlag mode)
 
 
new QIODevice.OpenModeFlag OpenMode ()
 
 
new QByteArray Peek (long maxlen)
 
 
new long Peek (Pointer< sbyte > data, long maxlen)
 
 
virtual long Pos ()
 
 
new bool PutChar (sbyte c)
 
 
new QByteArray Read (long maxlen)
 
 
new long Read (Pointer< sbyte > data, long maxlen)
 
 
new QByteArray ReadAll ()
 
 
new QByteArray ReadLine (long maxlen=0)
 
 
new long ReadLine (Pointer< sbyte > data, long maxlen)
 
 
virtual bool Reset ()
 
 
virtual bool Seek (long pos)
 
 
new void SetTextModeEnabled (bool enabled)
 
 
virtual long Size ()
 
 
new void UngetChar (sbyte c)
 
 
new long Write (QByteArray data)
 
 
new long Write (string data)
 
 
new long Write (string data, long len)
 
 
new void Dispose ()
 
- Public Member Functions inherited from QtCore.QObject
 QObject (QObject parent=null)
 
 
new bool BlockSignals (bool b)
 
 
new
System.Collections.Generic.List
< QObject
Children ()
 
 
new bool Connect (QObject sender, string signal, string member, Qt.ConnectionType type=Qt.ConnectionType.AutoConnection)
 
 
new void DeleteLater ()
 
 
new bool Disconnect (QObject receiver, string member=null)
 
 
new bool Disconnect (string signal=null, QObject receiver=null, string member=null)
 
 
new void DumpObjectInfo ()
 
 
new void DumpObjectTree ()
 
 
new
System.Collections.Generic.List
< QByteArray
DynamicPropertyNames ()
 
 
virtual bool OnEvent (QEvent arg1)
 
 
virtual bool EventFilter (QObject arg1, QEvent arg2)
 
 
new bool Inherits (string classname)
 
 
new void InstallEventFilter (QObject arg1)
 
 
new bool IsWidgetType ()
 
 
new void KillTimer (int id)
 
 
new object Property (string name)
 
 
new void RemoveEventFilter (QObject arg1)
 
 
new bool SetProperty (string name, object value)
 
 
new void SetUserData (uint id, QObjectUserData data)
 
new bool SignalsBlocked ()
 
 
new int StartTimer (int interval)
 
 
new QObjectUserData UserData (uint id)
 
new void Dispose ()
 
- Public Member Functions inherited from QtCore.Qt
delegate QTextStream FuncQTextStreamQTextStream (QTextStream qTextStream)
 
delegate string FuncStringQByteArray (QByteArray qByteArray)
 
delegate QByteArray FuncQByteArrayString (string @string)
 
delegate void ActionQtMsgTypeString (QtMsgType qtMsgType, string @string)
 
delegate QObject FuncQObject ()
 

Static Public Member Functions

static string Tr (string s, string c=null)
 
static string Tr (string s, string c, int n)
 
static string TrUtf8 (string s, string c=null)
 
static string TrUtf8 (string s, string c, int n)
 
- Static Public Member Functions inherited from QtCore.QIODevice
static string Tr (string s, string c=null)
 
static string Tr (string s, string c, int n)
 
static string TrUtf8 (string s, string c=null)
 
static string TrUtf8 (string s, string c, int n)
 
- Static Public Member Functions inherited from QtCore.QObject
static bool Connect (QObject sender, QMetaMethod signal, QObject receiver, QMetaMethod method, Qt.ConnectionType type=Qt.ConnectionType.AutoConnection)
 
 
static bool Connect (QObject sender, string signal, QObject receiver, string member, Qt.ConnectionType arg5=Qt.ConnectionType.AutoConnection)
 
 
static bool Disconnect (QObject sender, QMetaMethod signal, QObject receiver, QMetaMethod member)
 
 
static bool Disconnect (QObject sender, string signal, QObject receiver, string member)
 
 
static uint RegisterUserData ()
 
static string Tr (string s, string c=null)
 
 
static string Tr (string s, string c, int n)
 
 
static string TrUtf8 (string s, string c=null)
 
 
static string TrUtf8 (string s, string c, int n)
 
 
- Static Public Member Functions inherited from QtCore.Qt
static QDataStream Write (QDataStream s, object p)
 
static QDataStream Write (QDataStream arg1, QSizeF arg2)
 
static QTextStream Write (QTextStream s, Qt.FuncQTextStreamQTextStream f)
 
static QDataStream Write (QDataStream arg1, QPoint arg2)
 
static QDataStream Write (QDataStream arg1, QLineF arg2)
 
static QDataStream Write (QDataStream arg1, QPointF arg2)
 
static QDataStream Write (QDataStream arg1, QChar arg2)
 
static QDataStream Write (QDataStream arg1, QByteArray arg2)
 
static QDataStream Write (QDataStream arg1, QEasingCurve arg2)
 
static QDataStream Write (QDataStream arg1, QRectF arg2)
 
static QDataStream Write (QDataStream arg1, QDateTime arg2)
 
static QDataStream Write (QDataStream arg1, QUrl arg2)
 
static QDataStream Write (QDataStream arg1, QTime arg2)
 
static QDataStream Write (QDataStream arg1, QLocale arg2)
 
static QDataStream Write (QDataStream arg1, QBitArray arg2)
 
static QDataStream Write (QDataStream arg1, QUuid arg2)
 
static QDataStream Write (QDataStream arg1, QLine arg2)
 
static QDataStream Write (QDataStream arg1, QDate arg2)
 
static QDataStream Write (QDataStream @out, QRegExp regExp)
 
static QDataStream Write (QDataStream arg1, QRect arg2)
 
static QDataStream Write (QDataStream arg1, QSize arg2)
 
static QDataStream Write (QDataStream s, QVariant.Type p)
 
static QDataStream Write (QDataStream arg1, string arg2)
 
static QDataStream Write (QDataStream @out, System.Collections.Generic.List< string > list)
 
static QDataStream Read (QDataStream arg1, QChar arg2)
 
static QDataStream Read (QDataStream arg1, QLocale arg2)
 
static QDataStream Read (QDataStream arg1, QRect arg2)
 
static QDataStream Read (QDataStream arg1, QEasingCurve arg2)
 
static QDataStream Read (QDataStream arg1, QDate arg2)
 
static QDataStream Read (QDataStream arg1, QUrl arg2)
 
static QDataStream Read (QDataStream arg1, QUuid arg2)
 
static QTextStream Read (QTextStream s, Qt.FuncQTextStreamQTextStream f)
 
static QDataStream Read (QDataStream arg1, QLineF arg2)
 
static QDataStream Read (QDataStream arg1, QRectF arg2)
 
static QDataStream Read (QDataStream arg1, QPointF arg2)
 
static QDataStream Read (QDataStream arg1, QLine arg2)
 
static QDataStream Read (QDataStream arg1, QBitArray arg2)
 
static QDataStream Read (QDataStream arg1, QSize arg2)
 
static QDataStream Read (QDataStream arg1, QDateTime arg2)
 
static QDataStream Read (QDataStream arg1, QTime arg2)
 
static QDataStream Read (QDataStream arg1, QPoint arg2)
 
static QDataStream Read (QDataStream @in, QRegExp regExp)
 
static QDataStream Read (QDataStream s, object p)
 
static QDataStream Read (QDataStream arg1, QByteArray arg2)
 
static QDataStream Read (QDataStream arg1, QSizeF arg2)
 
static QDataStream Read (QDataStream arg1, string arg2)
 
static QDataStream Read (QDataStream s, QVariant.Type p)
 
static QDataStream Read (QDataStream @in, System.Collections.Generic.List< string > list)
 
static double qAcos (double v)
 
static void qAddPostRoutine (System.Action arg1)
 
static string qAppName ()
 
static double qAsin (double v)
 
static double qAtan (double v)
 
static double qAtan2 (double x, double y)
 
static void qBadAlloc ()
 
static int qCeil (double v)
 
static ushort qChecksum (string s, uint len)
 
static QByteArray qCompress (QByteArray data)
 
static QByteArray qCompress (QByteArray data, int compressionLevel=-1)
 
static QByteArray qCompress (Pointer< byte > data, int nbytes)
 
static QByteArray qCompress (Pointer< byte > data, int nbytes, int compressionLevel=-1)
 
static double qCos (double v)
 
static double qExp (double v)
 
static double qFabs (double v)
 
static double qFastCos (double x)
 
static double qFastSin (double x)
 
static string qFlagLocation (string method)
 
static int qFloor (double v)
 
static bool qFuzzyCompare (double p1, double p2)
 
static bool qFuzzyCompare (float p1, float p2)
 
static bool qFuzzyIsNull (double d)
 
static bool qFuzzyIsNull (float f)
 
static uint qHash (QBitArray key)
 
static uint qHash (QChar key)
 
static uint qHash (QStringRef key)
 
static uint qHash (QUrl url)
 
static uint qHash (QPersistentModelIndex index)
 
static uint qHash (QByteArray key)
 
static uint qHash (QModelIndex index)
 
static uint qHash (uint key)
 
static uint qHash (sbyte key)
 
static uint qHash (byte key)
 
static uint qHash (short key)
 
static uint qHash (NativeLong key)
 
static uint qHash (ushort key)
 
static uint qHash (long key)
 
static uint qHash (ulong key)
 
static uint qHash (int key)
 
static uint qHash (string key)
 
static uint qHash (NativeULong key)
 
static double qInf ()
 
static Qt.ActionQtMsgTypeString qInstallMsgHandler (Qt.ActionQtMsgTypeString arg1)
 
static int qIntCast (float f)
 
static int qIntCast (double f)
 
static bool qIsFinite (double d)
 
static bool qIsFinite (float f)
 
static bool qIsInf (float f)
 
static bool qIsInf (double d)
 
static bool qIsNaN (float f)
 
static bool qIsNaN (double d)
 
static bool qIsNull (float f)
 
static bool qIsNull (double d)
 
static double qLn (double v)
 
static double qPow (double x, double y)
 
static double qQNaN ()
 
static void qRegisterStaticPluginInstanceFunction (Qt.FuncQObject function)
 
static void qRemovePostRoutine (System.Action arg1)
 
static int qRound (double d)
 
static long qRound64 (double d)
 
static double qSNaN ()
 
static bool qSharedBuild ()
 
static double qSin (double v)
 
static double qSqrt (double v)
 
static bool qStringComparisonHelper (QStringRef s1, string s2)
 
static double qTan (double v)
 
static QByteArray qUncompress (QByteArray data)
 
static QByteArray qUncompress (Pointer< byte > data, int nbytes)
 
static string qVersion ()
 
static void Qbswap_helper (Pointer< byte > src, Pointer< byte > dest, int size)
 
static QByteArray Qgetenv (string varName)
 
static bool Qputenv (string varName, QByteArray value)
 
static int Qrand ()
 
static void Qsrand (uint seed)
 
static int Qstrcmp (QByteArray str1, QByteArray str2)
 
static int Qstrcmp (QByteArray str1, string str2)
 
static int Qstrcmp (string str1, QByteArray str2)
 
static int Qstrcmp (string str1, string str2)
 
static Pointer< sbyte > Qstrcpy (Pointer< sbyte > dst, string src)
 
static Pointer< sbyte > Qstrdup (string arg1)
 
static int Qstricmp (string arg1, string arg2)
 
static uint Qstrlen (string str)
 
static int Qstrncmp (string str1, string str2, uint len)
 
static Pointer< sbyte > Qstrncpy (Pointer< sbyte > dst, string src, uint len)
 
static int Qstrnicmp (string arg1, string arg2, uint len)
 
static uint Qstrnlen (string str, uint maxlen)
 
static string QtTrId (string id, int n=-1)
 

Protected Member Functions

 QAbstractSocket (System.Type dummy)
 
new void ConnectToHostImplementation (string hostName, ushort port, QIODevice.OpenModeFlag mode=QIODevice.OpenModeFlag.ReadWrite)
 
 
new void OnConnected ()
 
 
new void DisconnectFromHostImplementation ()
 
 
new void OnDisconnected ()
 
 
new void OnError (QAbstractSocket.SocketError arg1)
 
 
new void OnHostFound ()
 
 
new void OnProxyAuthenticationRequired (QNetworkProxy proxy, QAuthenticator authenticator)
 
 
override long ReadData (Pointer< sbyte > data, long maxlen)
 
 
override long ReadLineData (Pointer< sbyte > data, long maxlen)
 
 
new void SetLocalAddress (QHostAddress address)
 
 
new void SetLocalPort (ushort port)
 
 
new void SetPeerAddress (QHostAddress address)
 
 
new void SetPeerName (string name)
 
 
new void SetPeerPort (ushort port)
 
 
new void SetSocketError (QAbstractSocket.SocketError socketError)
 
 
new void SetSocketState (QAbstractSocket.SocketState state)
 
 
new void OnStateChanged (QAbstractSocket.SocketState arg1)
 
 
override long WriteData (string data, long len)
 
 
- Protected Member Functions inherited from QtCore.QIODevice
 QIODevice (System.Type dummy)
 
new void OnAboutToClose ()
 
 
new void OnBytesWritten (long bytes)
 
 
new void OnReadChannelFinished ()
 
 
new void OnReadyRead ()
 
 
new void SetErrorString (string errorString)
 
 
new void SetOpenMode (QIODevice.OpenModeFlag openMode)
 
 
- Protected Member Functions inherited from QtCore.QObject
 QObject (System.Type dummy)
 
virtual void OnChildEvent (QChildEvent arg1)
 
 
virtual void ConnectNotify (string signal)
 
 
virtual void OnCustomEvent (QEvent arg1)
 
 
new void OnDestroyed (QObject arg1=null)
 
 
virtual void DisconnectNotify (string signal)
 
 
new int Receivers (string signal)
 
 
new QObject Sender ()
 
 
new int SenderSignalIndex ()
 
 
virtual void OnTimerEvent (QTimerEvent arg1)
 
 

Properties

Slot HostFound
 
 
Slot Connected
 
 
Slot Disconnected
 
 
Slot< QAbstractSocket.SocketStateStateChanged
 
 
Slot< QAbstractSocket.SocketErrorError
 
 
Slot< QNetworkProxy,
QAuthenticator
ProxyAuthenticationRequired
 
 
Slot ConnectionClosed
 
Slot DelayedCloseFinished
 
new QNetworkProxy Proxy [get, set]
 
 
new long ReadBufferSize [get, set]
 
 
static new QMetaObject StaticMetaObject [get]
 
new IQAbstractSocketSignals Emit [get]
 
- Properties inherited from QtCore.QIODevice
Slot ReadyRead
 
 
Slot< System.Int64 > BytesWritten
 
 
Slot AboutToClose
 
 
Slot ReadChannelFinished
 
 
static new QMetaObject StaticMetaObject [get]
 
new IQIODeviceSignals Emit [get]
 
- Properties inherited from QtCore.QObject
virtual EventHandler
< QEventArgs< QChildEvent > > 
ChildEvent
 
 
virtual EventHandler
< QEventArgs< QEvent > > 
CustomEvent
 
 
virtual EventHandler
< QEventArgs< QEvent > > 
Event
 
 
virtual EventHandler
< QEventArgs< QTimerEvent > > 
TimerEvent
 
 
Slot< QObjectDestroyedQObject
 
Slot Destroyed
 
 
new QObject Parent [get, set]
 
 
virtual System.IntPtr SmokeObject [get, set]
 
new string ObjectName [get, set]
 
 
static new QMetaObject StaticMetaObject [get]
 
 
static new QMetaObject StaticQtMetaObject [get]
 
new IQObjectSignals Emit [get]
 

Additional Inherited Members

- Protected Attributes inherited from QtCore.QObject
object Q_EMIT = null
 
SmokeInvocation interceptor
 
readonly List< QEventHandler > eventFilters = new List<QEventHandler>()
 

Detailed Description

The QAbstractSocket class provides the base functionality common to all socket types.

QAbstractSocket is the base class for QTcpSocket and QUdpSocket and contains all common functionality of these two classes. If you need a socket, you have two options:

Instantiate QTcpSocket or QUdpSocket.

Create a native socket descriptor, instantiate QAbstractSocket, and call setSocketDescriptor() to wrap the native socket.

TCP (Transmission Control Protocol) is a reliable, stream-oriented, connection-oriented transport protocol. UDP (User Datagram Protocol) is an unreliable, datagram-oriented, connectionless protocol. In practice, this means that TCP is better suited for continuous transmission of data, whereas the more lightweight UDP can be used when reliability isn't important.

QAbstractSocket's API unifies most of the differences between the two protocols. For example, although UDP is connectionless, connectToHost() establishes a virtual connection for UDP sockets, enabling you to use QAbstractSocket in more or less the same way regardless of the underlying protocol. Internally, QAbstractSocket remembers the address and port passed to connectToHost(), and functions like read() and write() use these values.

At any time, QAbstractSocket has a state (returned by state()). The initial state is UnconnectedState. After calling connectToHost(), the socket first enters HostLookupState. If the host is found, QAbstractSocket enters ConnectingState and emits the hostFound() signal. When the connection has been established, it enters ConnectedState and emits connected(). If an error occurs at any stage, error() is emitted. Whenever the state changes, stateChanged() is emitted. For convenience, isValid() returns true if the socket is ready for reading and writing, but note that the socket's state must be ConnectedState before reading and writing can occur.

Read or write data by calling read() or write(), or use the convenience functions readLine() and readAll(). QAbstractSocket also inherits getChar(), putChar(), and ungetChar() from QIODevice, which work on single bytes. The bytesWritten() signal is emitted when data has been written to the socket (i.e., when the client has read the data). Note that Qt does not limit the write buffer size. You can monitor its size by listening to this signal.

The readyRead() signal is emitted every time a new chunk of data has arrived. bytesAvailable() then returns the number of bytes that are available for reading. Typically, you would connect the readyRead() signal to a slot and read all available data there. If you don't read all the data at once, the remaining data will still be available later, and any new incoming data will be appended to QAbstractSocket's internal read buffer. To limit the size of the read buffer, call setReadBufferSize().

To close the socket, call disconnectFromHost(). QAbstractSocket enters QAbstractSocket::ClosingState. After all pending data has been written to the socket, QAbstractSocket actually closes the socket, enters QAbstractSocket::ClosedState, and emits disconnected(). If you want to abort a connection immediately, discarding all pending data, call abort() instead. If the remote host closes the connection, QAbstractSocket will emit error(QAbstractSocket::RemoteHostClosedError), during which the socket state will still be ConnectedState, and then the disconnected() signal will be emitted.

The port and address of the connected peer is fetched by calling peerPort() and peerAddress(). peerName() returns the host name of the peer, as passed to connectToHost(). localPort() and localAddress() return the port and address of the local socket.

QAbstractSocket provides a set of functions that suspend the calling thread until certain signals are emitted. These functions can be used to implement blocking sockets:

waitForConnected() blocks until a connection has been established.

waitForReadyRead() blocks until new data is available for reading.

waitForBytesWritten() blocks until one payload of data has been written to the socket.

waitForDisconnected() blocks until the connection has closed.

We show an example:

int numRead = 0, numReadTotal = 0;

char buffer[50];

forever {

numRead = socket.read(buffer, 50);

// do whatever with array

numReadTotal += numRead;

if (numRead == 0 && !socket.waitForReadyRead())

break;

}

If waitForReadyRead() returns false, the connection has been closed or an error has occurred.

Programming with a blocking socket is radically different from programming with a non-blocking socket. A blocking socket doesn't require an event loop and typically leads to simpler code. However, in a GUI application, blocking sockets should only be used in non-GUI threads, to avoid freezing the user interface. See the network/fortuneclient and network/blockingfortuneclient examples for an overview of both approaches.

Note: We discourage the use of the blocking functions together with signals. One of the two possibilities should be used.

QAbstractSocket can be used with QTextStream and QDataStream's stream operators (operator<<() and operator>>()). There is one issue to be aware of, though: You must make sure that enough data is available before attempting to read it using operator>>().

See also QFtp, QNetworkAccessManager, and QTcpServer.

Member Enumeration Documentation

This enum describes the network layer protocol values used in Qt.

See also QHostAddress::protocol().

Enumerator:
IPv4Protocol 

IPv4

IPv6Protocol 

IPv6

UnknownNetworkLayerProtocol 

Other than IPv4 and IPv6

This enum describes the socket errors that can occur.

See also QAbstractSocket::error().

Enumerator:
AddressInUseError 

The address specified to QUdpSocket::bind() is already in use and was set to be exclusive.

ConnectionRefusedError 

The connection was refused by the peer (or timed out).

DatagramTooLargeError 

The datagram was larger than the operating system's limit (which can be as low as 8192 bytes).

HostNotFoundError 

The host address was not found.

NetworkError 

An error occurred with the network (e.g., the network cable was accidentally plugged out).

ProxyAuthenticationRequiredError 

The socket is using a proxy, and the proxy requires authentication.

ProxyConnectionClosedError 

The connection to the proxy server was closed unexpectedly (before the connection to the final peer was established) (This value was introduced in 4.5.)

ProxyConnectionRefusedError 

Could not contact the proxy server because the connection to that server was denied (This value was introduced in 4.5.)

ProxyConnectionTimeoutError 

The connection to the proxy server timed out or the proxy server stopped responding in the authentication phase. (This value was introduced in 4.5.)

ProxyNotFoundError 

The proxy address set with setProxy() (or the application proxy) was not found. (This value was introduced in 4.5.)

ProxyProtocolError 

The connection negotiation with the proxy server because the response from the proxy server could not be understood. (This value was introduced in 4.5.)

RemoteHostClosedError 

The remote host closed the connection. Note that the client socket (i.e., this socket) will be closed after the remote close notification has been sent.

SocketAccessError 

The socket operation failed because the application lacked the required privileges.

SocketAddressNotAvailableError 

The address specified to QUdpSocket::bind() does not belong to the host.

SocketResourceError 

The local system ran out of resources (e.g., too many sockets).

SocketTimeoutError 

The socket operation timed out.

SslHandshakeFailedError 

The SSL/TLS handshake failed, so the connection was closed (only used in QSslSocket) (This value was introduced in 4.4.)

UnfinishedSocketOperationError 

Used by QAbstractSocketEngine only, The last operation attempted has not finished yet (still in progress in the background). (This value was introduced in 4.4.)

UnknownSocketError 

An unidentified error occurred.

UnsupportedSocketOperationError 

The requested socket operation is not supported by the local operating system (e.g., lack of IPv6 support).

This enum represents the options that can be set on a socket. If desired, they can be set after having received the connected() signal from the socket or after having received a new socket from a QTcpServer.

This enum was introduced or modified in Qt 4.6.

See also QAbstractSocket::setSocketOption() and QAbstractSocket::socketOption().

Enumerator:
KeepAliveOption 

Set this to 1 to enable the SO_KEEPALIVE socket option

LowDelayOption 

Try to optimize the socket for low latency. For a QTcpSocket this would set the TCP_NODELAY option and disable Nagle's algorithm. Set this to 1 to enable.

MulticastLoopbackOption 

Set this to 1 to enable the IP_MULTICAST_LOOP (multicast loopback) socket option.

MulticastTtlOption 

Set this to an integer value to set IP_MULTICAST_TTL (TTL for multicast datagrams) socket option.

This enum describes the different states in which a socket can be.

See also QAbstractSocket::state().

Enumerator:
BoundState 

The socket is bound to an address and port (for servers).

ClosingState 

The socket is about to close (data may still be waiting to be written).

ConnectedState 

A connection is established.

ConnectingState 

The socket has started establishing a connection.

HostLookupState 

The socket is performing a host name lookup.

ListeningState 

For internal use only.

UnconnectedState 

The socket is not connected.

This enum describes the transport layer protocol.

See also QAbstractSocket::socketType().

Enumerator:
TcpSocket 

TCP

UdpSocket 

UDP

UnknownSocketType 

Other than TCP and UDP

Constructor & Destructor Documentation

QtNetwork.QAbstractSocket.QAbstractSocket ( System.Type  dummy)
protected
QtNetwork.QAbstractSocket.QAbstractSocket ( QAbstractSocket.SocketType  socketType,
QObject  parent 
)

Creates a new abstract socket of type socketType. The parent argument is passed to QObject's constructor.

See also socketType(), QTcpSocket, and QUdpSocket.

Member Function Documentation

new void QtNetwork.QAbstractSocket.Abort ( )

Aborts the current connection and resets the socket. Unlike disconnectFromHost(), this function immediately closes the socket, discarding any pending data in the write buffer.

See also disconnectFromHost() and close().

override bool QtNetwork.QAbstractSocket.AtEnd ( )
virtual

Reimplemented from QIODevice::atEnd().

Returns true if no more data is currently available for reading; otherwise returns false.

This function is most commonly used when reading data from the socket in a loop. For example:

// This slot is connected to QAbstractSocket::readyRead()

void SocketClass::readyReadSlot()

{

while (!socket.atEnd()) {

QByteArray data = socket.read(100);

....

}

}

See also bytesAvailable() and readyRead().

Reimplemented from QtCore.QIODevice.

Reimplemented in QtNetwork.QSslSocket.

override long QtNetwork.QAbstractSocket.BytesAvailable ( )
virtual

Reimplemented from QIODevice::bytesAvailable().

Returns the number of incoming bytes that are waiting to be read.

See also bytesToWrite() and read().

Reimplemented from QtCore.QIODevice.

Reimplemented in QtNetwork.QSslSocket.

override long QtNetwork.QAbstractSocket.BytesToWrite ( )
virtual

Reimplemented from QIODevice::bytesToWrite().

Returns the number of bytes that are waiting to be written. The bytes are written when control goes back to the event loop or when flush() is called.

See also bytesAvailable() and flush().

Reimplemented from QtCore.QIODevice.

Reimplemented in QtNetwork.QSslSocket.

override bool QtNetwork.QAbstractSocket.CanReadLine ( )
virtual

Reimplemented from QIODevice::canReadLine().

Returns true if a line of data can be read from the socket; otherwise returns false.

See also readLine().

Reimplemented from QtCore.QIODevice.

Reimplemented in QtNetwork.QSslSocket.

override void QtNetwork.QAbstractSocket.Close ( )
virtual

Reimplemented from QIODevice::close().

Closes the I/O device for the socket, disconnects the socket's connection with the host, closes the socket, and resets the name, address, port number and underlying socket descriptor.

See QIODevice::close() for a description of the actions that occur when an I/O device is closed.

See also abort().

Reimplemented from QtCore.QIODevice.

Reimplemented in QtNetwork.QSslSocket.

new void QtNetwork.QAbstractSocket.ConnectToHost ( QHostAddress  address,
ushort  port,
QIODevice.OpenModeFlag  mode = QIODevice.OpenModeFlag.ReadWrite 
)

Attempts to make a connection to hostName on the given port.

The socket is opened in the given openMode and first enters HostLookupState, then performs a host name lookup of hostName. If the lookup succeeds, hostFound() is emitted and QAbstractSocket enters ConnectingState. It then attempts to connect to the address or addresses returned by the lookup. Finally, if a connection is established, QAbstractSocket enters ConnectedState and emits connected().

At any point, the socket can emit error() to signal that an error occurred.

hostName may be an IP address in string form (e.g., "43.195.83.32"), or it may be a host name (e.g., "example.com"). QAbstractSocket will do a lookup only if required. port is in native byte order.

See also state(), peerName(), peerAddress(), peerPort(), and waitForConnected().

new void QtNetwork.QAbstractSocket.ConnectToHost ( string  hostName,
ushort  port,
QIODevice.OpenModeFlag  mode = QIODevice.OpenModeFlag.ReadWrite 
)

Attempts to make a connection to hostName on the given port.

The socket is opened in the given openMode and first enters HostLookupState, then performs a host name lookup of hostName. If the lookup succeeds, hostFound() is emitted and QAbstractSocket enters ConnectingState. It then attempts to connect to the address or addresses returned by the lookup. Finally, if a connection is established, QAbstractSocket enters ConnectedState and emits connected().

At any point, the socket can emit error() to signal that an error occurred.

hostName may be an IP address in string form (e.g., "43.195.83.32"), or it may be a host name (e.g., "example.com"). QAbstractSocket will do a lookup only if required. port is in native byte order.

See also state(), peerName(), peerAddress(), peerPort(), and waitForConnected().

new void QtNetwork.QAbstractSocket.ConnectToHostImplementation ( string  hostName,
ushort  port,
QIODevice.OpenModeFlag  mode = QIODevice.OpenModeFlag.ReadWrite 
)
protected

Contains the implementation of connectToHost().

Attempts to make a connection to hostName on the given port. The socket is opened in the given openMode.

This function was introduced in Qt 4.1.

override void QtNetwork.QAbstractSocket.CreateProxy ( )
virtual
new void QtNetwork.QAbstractSocket.DisconnectFromHost ( )

Attempts to close the socket. If there is pending data waiting to be written, QAbstractSocket will enter ClosingState and wait until all data has been written. Eventually, it will enter UnconnectedState and emit the disconnected() signal.

See also connectToHost().

new void QtNetwork.QAbstractSocket.DisconnectFromHostImplementation ( )
protected

Contains the implementation of disconnectFromHost().

This function was introduced in Qt 4.1.

new void QtNetwork.QAbstractSocket.Dispose ( )
new bool QtNetwork.QAbstractSocket.Flush ( )

This function writes as much as possible from the internal write buffer to the underlying network socket, without blocking. If any data was written, this function returns true; otherwise false is returned.

Call this function if you need QAbstractSocket to start sending buffered data immediately. The number of bytes successfully written depends on the operating system. In most cases, you do not need to call this function, because QAbstractSocket will start sending data automatically once control goes back to the event loop. In the absence of an event loop, call waitForBytesWritten() instead.

See also write() and waitForBytesWritten().

override bool QtNetwork.QAbstractSocket.IsSequential ( )
virtual

Reimplemented from QIODevice::isSequential().

Reimplemented from QtCore.QIODevice.

new bool QtNetwork.QAbstractSocket.IsValid ( )

Returns true if the socket is valid and ready for use; otherwise returns false.

Note: The socket's state must be ConnectedState before reading and writing can occur.

See also state().

new QHostAddress QtNetwork.QAbstractSocket.LocalAddress ( )

Returns the host address of the local socket if available; otherwise returns QHostAddress::Null.

This is normally the main IP address of the host, but can be QHostAddress::LocalHost (127.0.0.1) for connections to the local host.

See also localPort(), peerAddress(), and setLocalAddress().

new ushort QtNetwork.QAbstractSocket.LocalPort ( )

Returns the host port number (in native byte order) of the local socket if available; otherwise returns 0.

See also localAddress(), peerPort(), and setLocalPort().

new void QtNetwork.QAbstractSocket.OnConnected ( )
protected

This signal is emitted after connectToHost() has been called and a connection has been successfully established.

Note: On some operating systems the connected() signal may be directly emitted from the connectToHost() call for connections to the localhost.

See also connectToHost() and disconnected().

new void QtNetwork.QAbstractSocket.OnDisconnected ( )
protected

This signal is emitted when the socket has been disconnected.

Warning: If you need to delete the sender() of this signal in a slot connected to it, use the deleteLater() function.

See also connectToHost(), disconnectFromHost(), and abort().

new QAbstractSocket.SocketError QtNetwork.QAbstractSocket.OnError ( )

Returns the type of error that last occurred.

See also state() and errorString().

new void QtNetwork.QAbstractSocket.OnError ( QAbstractSocket.SocketError  arg1)
protected

Returns the type of error that last occurred.

See also state() and errorString().

new void QtNetwork.QAbstractSocket.OnHostFound ( )
protected

This signal is emitted after connectToHost() has been called and the host lookup has succeeded.

Note: Since Qt 4.6.3 QAbstractSocket may emit hostFound() directly from the connectToHost() call since a DNS result could have been cached.

See also connected().

new void QtNetwork.QAbstractSocket.OnProxyAuthenticationRequired ( QNetworkProxy  proxy,
QAuthenticator  authenticator 
)
protected

This signal can be emitted when a proxy that requires authentication is used. The authenticator object can then be filled in with the required details to allow authentication and continue the connection.

Note: It is not possible to use a QueuedConnection to connect to this signal, as the connection will fail if the authenticator has not been filled in with new information when the signal returns.

This function was introduced in Qt 4.3.

See also QAuthenticator and QNetworkProxy.

new void QtNetwork.QAbstractSocket.OnStateChanged ( QAbstractSocket.SocketState  arg1)
protected

This signal is emitted whenever QAbstractSocket's state changes. The socketState parameter is the new state.

QAbstractSocket::SocketState is not a registered metatype, so for queued connections, you will have to register it with Q_REGISTER_METATYPE() and qRegisterMetaType().

See also state() and Creating Custom Qt Types.

new QHostAddress QtNetwork.QAbstractSocket.PeerAddress ( )

Returns the address of the connected peer if the socket is in ConnectedState; otherwise returns QHostAddress::Null.

See also peerName(), peerPort(), localAddress(), and setPeerAddress().

new string QtNetwork.QAbstractSocket.PeerName ( )

Returns the name of the peer as specified by connectToHost(), or an empty QString if connectToHost() has not been called.

See also peerAddress(), peerPort(), and setPeerName().

new ushort QtNetwork.QAbstractSocket.PeerPort ( )

Returns the port of the connected peer if the socket is in ConnectedState; otherwise returns 0.

See also peerAddress(), localPort(), and setPeerPort().

override long QtNetwork.QAbstractSocket.ReadData ( Pointer< sbyte >  data,
long  maxlen 
)
protectedvirtual

Reimplemented from QIODevice::readData().

Implements QtCore.QIODevice.

Reimplemented in QtNetwork.QSslSocket.

override long QtNetwork.QAbstractSocket.ReadLineData ( Pointer< sbyte >  data,
long  maxlen 
)
protectedvirtual

Reimplemented from QIODevice::readLineData().

Reimplemented from QtCore.QIODevice.

new void QtNetwork.QAbstractSocket.SetLocalAddress ( QHostAddress  address)
protected

Sets the address on the local side of a connection to address.

You can call this function in a subclass of QAbstractSocket to change the return value of the localAddress() function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings.

Note that this function does not bind the local address of the socket prior to a connection (e.g., QUdpSocket::bind()).

This function was introduced in Qt 4.1.

See also localAddress(), setLocalPort(), and setPeerAddress().

new void QtNetwork.QAbstractSocket.SetLocalPort ( ushort  port)
protected

Sets the port on the local side of a connection to port.

You can call this function in a subclass of QAbstractSocket to change the return value of the localPort() function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings.

Note that this function does not bind the local port of the socket prior to a connection (e.g., QUdpSocket::bind()).

This function was introduced in Qt 4.1.

See also localPort(), localAddress(), setLocalAddress(), and setPeerPort().

new void QtNetwork.QAbstractSocket.SetPeerAddress ( QHostAddress  address)
protected

Sets the address of the remote side of the connection to address.

You can call this function in a subclass of QAbstractSocket to change the return value of the peerAddress() function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings.

This function was introduced in Qt 4.1.

See also peerAddress(), setPeerPort(), and setLocalAddress().

new void QtNetwork.QAbstractSocket.SetPeerName ( string  name)
protected

Sets the host name of the remote peer to name.

You can call this function in a subclass of QAbstractSocket to change the return value of the peerName() function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings.

This function was introduced in Qt 4.1.

See also peerName().

new void QtNetwork.QAbstractSocket.SetPeerPort ( ushort  port)
protected

Sets the port of the remote side of the connection to port.

You can call this function in a subclass of QAbstractSocket to change the return value of the peerPort() function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings.

This function was introduced in Qt 4.1.

See also peerPort(), setPeerAddress(), and setLocalPort().

new bool QtNetwork.QAbstractSocket.SetSocketDescriptor ( int  socketDescriptor,
QAbstractSocket.SocketState  state = QAbstractSocket.SocketState.ConnectedState,
QIODevice.OpenModeFlag  openMode = QIODevice.OpenModeFlag.ReadWrite 
)

Initializes QAbstractSocket with the native socket descriptor socketDescriptor. Returns true if socketDescriptor is accepted as a valid socket descriptor; otherwise returns false. The socket is opened in the mode specified by openMode, and enters the socket state specified by socketState.

Note: It is not possible to initialize two abstract sockets with the same native socket descriptor.

See also socketDescriptor().

new void QtNetwork.QAbstractSocket.SetSocketError ( QAbstractSocket.SocketError  socketError)
protected

Sets the type of error that last occurred to socketError.

See also setSocketState() and setErrorString().

new void QtNetwork.QAbstractSocket.SetSocketOption ( QAbstractSocket.SocketOption  option,
object  value 
)

Sets the given option to the value described by value.

This function was introduced in Qt 4.6.

See also socketOption().

new void QtNetwork.QAbstractSocket.SetSocketState ( QAbstractSocket.SocketState  state)
protected

Sets the state of the socket to state.

See also state().

new int QtNetwork.QAbstractSocket.SocketDescriptor ( )

Returns the native socket descriptor of the QAbstractSocket object if this is available; otherwise returns -1.

If the socket is using QNetworkProxy, the returned descriptor may not be usable with native socket functions.

The socket descriptor is not available when QAbstractSocket is in UnconnectedState.

See also setSocketDescriptor().

new object QtNetwork.QAbstractSocket.socketOption ( QAbstractSocket.SocketOption  option)

Returns the value of the option option.

This function was introduced in Qt 4.6.

See also setSocketOption().

new QAbstractSocket.SocketType QtNetwork.QAbstractSocket.socketType ( )

Returns the socket type (TCP, UDP, or other).

See also QTcpSocket and QUdpSocket.

new QAbstractSocket.SocketState QtNetwork.QAbstractSocket.State ( )

Returns the state of the socket.

See also error().

static string QtNetwork.QAbstractSocket.Tr ( string  s,
string  c = null 
)
static
static string QtNetwork.QAbstractSocket.Tr ( string  s,
string  c,
int  n 
)
static
static string QtNetwork.QAbstractSocket.TrUtf8 ( string  s,
string  c = null 
)
static
static string QtNetwork.QAbstractSocket.TrUtf8 ( string  s,
string  c,
int  n 
)
static
override bool QtNetwork.QAbstractSocket.WaitForBytesWritten ( int  msecs = 30000)
virtual

Reimplemented from QIODevice::waitForBytesWritten().

Reimplemented from QtCore.QIODevice.

Reimplemented in QtNetwork.QSslSocket.

new bool QtNetwork.QAbstractSocket.WaitForConnected ( int  msecs = 30000)

Waits until the socket is connected, up to msecs milliseconds. If the connection has been established, this function returns true; otherwise it returns false. In the case where it returns false, you can call error() to determine the cause of the error.

The following example waits up to one second for a connection to be established:

socket->connectToHost("imap", 143);

if (socket->waitForConnected(1000))

qDebug("Connected!");

If msecs is -1, this function will not time out.

Note: This function may wait slightly longer than msecs, depending on the time it takes to complete the host lookup.

Note: Multiple calls to this functions do not accumulate the time. If the function times out, the connecting process will be aborted.

See also connectToHost() and connected().

new bool QtNetwork.QAbstractSocket.WaitForDisconnected ( int  msecs = 30000)

Waits until the socket has disconnected, up to msecs milliseconds. If the connection has been disconnected, this function returns true; otherwise it returns false. In the case where it returns false, you can call error() to determine the cause of the error.

The following example waits up to one second for a connection to be closed:

socket->disconnectFromHost();

if (socket->state() == QAbstractSocket::UnconnectedState ||

socket->waitForDisconnected(1000))

qDebug("Disconnected!");

If msecs is -1, this function will not time out.

See also disconnectFromHost() and close().

override bool QtNetwork.QAbstractSocket.WaitForReadyRead ( int  msecs = 30000)
virtual

Reimplemented from QIODevice::waitForReadyRead().

This function blocks until new data is available for reading and the readyRead() signal has been emitted. The function will timeout after msecs milliseconds; the default timeout is 30000 milliseconds.

The function returns true if the readyRead() signal is emitted and there is new data available for reading; otherwise it returns false (if an error occurred or the operation timed out).

See also waitForBytesWritten().

Reimplemented from QtCore.QIODevice.

Reimplemented in QtNetwork.QSslSocket.

override long QtNetwork.QAbstractSocket.WriteData ( string  data,
long  len 
)
protectedvirtual

Reimplemented from QIODevice::writeData().

Implements QtCore.QIODevice.

Reimplemented in QtNetwork.QSslSocket.

Property Documentation

Slot QtNetwork.QAbstractSocket.Connected
addremove

This signal is emitted after connectToHost() has been called and a connection has been successfully established.

Note: On some operating systems the connected() signal may be directly emitted from the connectToHost() call for connections to the localhost.

See also connectToHost() and disconnected().

Slot QtNetwork.QAbstractSocket.ConnectionClosed
addremove
Slot QtNetwork.QAbstractSocket.DelayedCloseFinished
addremove
Slot QtNetwork.QAbstractSocket.Disconnected
addremove

This signal is emitted when the socket has been disconnected.

Warning: If you need to delete the sender() of this signal in a slot connected to it, use the deleteLater() function.

See also connectToHost(), disconnectFromHost(), and abort().

new IQAbstractSocketSignals QtNetwork.QAbstractSocket.Emit
getprotected
Slot<QAbstractSocket.SocketError> QtNetwork.QAbstractSocket.Error
addremove

Returns the type of error that last occurred.

See also state() and errorString().

Slot QtNetwork.QAbstractSocket.HostFound
addremove

This signal is emitted after connectToHost() has been called and the host lookup has succeeded.

Note: Since Qt 4.6.3 QAbstractSocket may emit hostFound() directly from the connectToHost() call since a DNS result could have been cached.

See also connected().

new QNetworkProxy QtNetwork.QAbstractSocket.Proxy
getset

Returns the network proxy for this socket. By default QNetworkProxy::DefaultProxy is used, which means this socket will query the default proxy settings for the application.

This function was introduced in Qt 4.1.

Sets the explicit network proxy for this socket to networkProxy.

To disable the use of a proxy for this socket, use the QNetworkProxy::NoProxy proxy type:

socket->setProxy(QNetworkProxy::NoProxy);

The default value for the proxy is QNetworkProxy::DefaultProxy, which means the socket will use the application settings: if a proxy is set with QNetworkProxy::setApplicationProxy, it will use that; otherwise, if a factory is set with QNetworkProxyFactory::setApplicationProxyFactory, it will query that factory with type QNetworkProxyQuery::TcpSocket.

This function was introduced in Qt 4.1.

Slot<QNetworkProxy,QAuthenticator> QtNetwork.QAbstractSocket.ProxyAuthenticationRequired
addremove

This signal can be emitted when a proxy that requires authentication is used. The authenticator object can then be filled in with the required details to allow authentication and continue the connection.

Note: It is not possible to use a QueuedConnection to connect to this signal, as the connection will fail if the authenticator has not been filled in with new information when the signal returns.

This function was introduced in Qt 4.3.

See also QAuthenticator and QNetworkProxy.

new long QtNetwork.QAbstractSocket.ReadBufferSize
getset

Returns the size of the internal read buffer. This limits the amount of data that the client can receive before you call read() or readAll().

A read buffer size of 0 (the default) means that the buffer has no size limit, ensuring that no data is lost.

Sets the size of QAbstractSocket's internal read buffer to be size bytes.

If the buffer size is limited to a certain size, QAbstractSocket won't buffer more than this size of data. Exceptionally, a buffer size of 0 means that the read buffer is unlimited and all incoming data is buffered. This is the default.

This option is useful if you only read the data at certain points in time (e.g., in a real-time streaming application) or if you want to protect your socket against receiving too much data, which may eventually cause your application to run out of memory.

Only QTcpSocket uses QAbstractSocket's internal buffer; QUdpSocket does not use any buffering at all, but rather relies on the implicit buffering provided by the operating system. Because of this, calling this function on QUdpSocket has no effect.

Slot<QAbstractSocket.SocketState> QtNetwork.QAbstractSocket.StateChanged
addremove

This signal is emitted whenever QAbstractSocket's state changes. The socketState parameter is the new state.

QAbstractSocket::SocketState is not a registered metatype, so for queued connections, you will have to register it with Q_REGISTER_METATYPE() and qRegisterMetaType().

See also state() and Creating Custom Qt Types.

new QMetaObject QtNetwork.QAbstractSocket.StaticMetaObject
staticget