# Namespace KSocketFactory

Module | kdecore
Namespace | KSocketFactory

KSocketFactory provides functions for opening sockets to remote hosts

KSocketFactory is a socket-opener group of functions that must be used whenever a KDE application wants to communicate with a remote host. It will determine the necessary proxy and local KDE settings, then open the connection. The typical use-case is:

`d->socket = KSocketFactory.connectToHost("www.kde.org", "http"); d->socket->setParent(this); QObject.connect(d->socket, SIGNAL(connected()), this, SLOT(socketConnected()));`

Synchronous operation is not recommended, since it may lead to UI deadlocks. It is preferred to return to the mainloop if the socket is being created in the main GUI thread.

However, if the socket is created in an auxiliary thread, it is possible to use something equivalent to synchronous mode:

`d->socket = KSocketFactory.synchronousConnectToHost("www.kde.org", "http"); d->socket->setParent(this);`

All objects returned from these functions belong to the caller and must be disposed of properly. Calling QObject.setParent() or passing a parent object is the recommended way.

Author Thiago Macieira <thiago@kde.org>

### methods

- connectToHost (protocol, host, port, parent)

  returns QTcpSocket

  Argument | Type | Default
  --- | --- | ---
  protocol | QString |
  host | QString |
  port | quint16 |
  parent | QObject | 0

  Initiates a TCP/IP socket connection to remote node (host) @p host, using the protocol. Returns a QTcpSocket that is connecting (i.e., in a state before QAbstractSocket.Connected) in most cases, even if the target service doesn't exist or if a connection failure happens.

  This function never returns 0. If the returned socket cannot connect, it will emit the QAbstractSocket.error() signal. Otherwise, the QAbstractSocket.connected() signal will be emitted eventually.

  The protocol parameter must be a string representation of the protocol being attempted, like "http", "ftp" or "xmpp". It might be used to determine the proxy type -- for instance, the proxy for HTTP connections might be different from the proxy for other connections. Pass an empty QString if the connection is of no established protocol.

  The port parameter can be used to specify the port number lookup if the service is not a well-known service.

  protocol - the protocol this socket will use

  host - the remote node (host) to connect to

  port - the port number to connect to

  parent - the parent object to be passed to the QTcpSocket constructor @threadsafe

- connectToHost (url, parent)

  returns QTcpSocket

  Argument | Type | Default
  --- | --- | ---
  url | QUrl |
  parent | QObject | 0

- connectToHost (socket, protocol, host, port)

  returns Nothing

  Argument | Type | Default
  --- | --- | ---
  socket | QTcpSocket |
  protocol | QString |
  host | QString |
  port | quint16 |

- connectToHost (socket, url)

  returns Nothing

  Argument | Type | Default
  --- | --- | ---
  socket | QTcpSocket |
  url | QUrl |

- datagramSocket (protocol, host, parent)

  returns QUdpSocket

  Argument | Type | Default
  --- | --- | ---
  protocol | QString |
  host | QString |
  parent | QObject | 0

- listen (protocol, address, port, parent)

  returns QTcpServer

  Argument | Type | Default
  --- | --- | ---
  protocol | QString |
  address | QHostAddress | QHostAddress.Any
  port | quint16 | 0
  parent | QObject | 0

  Opens a TCP/IP socket for listening protocol protocol, binding only at address address. The port parameter indicates the port to be opened.

  This function does not return 0. If the opening of the socket failed for some reason, it will return a QTcpServer object that is not listening (QTcpServer.isListening() returns false), with a properly set error string indicating the reason).

  Note that passing 0 as the default port number will cause the operating system to automatically allocate a free port for the socket.

  protocol - the protocol this socket will accept

  address - the address to listen at

  port - the default port for the service

  parent - the parent object to be passed to the QTcpServer constructor

- proxyForConnection (protocol, host)

  returns QNetworkProxy

  Argument | Type | Default
  --- | --- | ---
  protocol | QString |
  host | QString |

- proxyForDatagram (protocol, host)

  returns QNetworkProxy

  Argument | Type | Default
  --- | --- | ---
  protocol | QString |
  host | QString |

- proxyForListening (protocol)

  returns QNetworkProxy

  Argument | Type | Default
  --- | --- | ---
  protocol | QString |

- synchronousConnectToHost (protocol, host, port, msecs, parent)

  returns QTcpSocket

  Argument | Type | Default
  --- | --- | ---
  protocol | QString |
  host | QString |
  port | quint16 |
  msecs | int | 30000
  parent | QObject | 0

  This function behaves exactly like connectToHost() above, except that the socket it returns is either in QAbstractSocket.Connected state, or it has failed connecting altogether.

  This function should be used if a synchronous connection is necessary instead of calling QAbstractSocket.waitForConnected(), since that may not work on all objects returned from connectToHost().

  This function does not return 0. If the connection attempt failed for some reason, the socket state will be QAbstractSocket.UnconnectedState and QAbstractSocket.isValid will return false. The socket error state and string will contain more information. This function may block.

  protocol - the protocol this socket will use

  host - the remote node (host) to connect to

  port - the port number to connect to

  msecs - the time (in milliseconds) to wait while attempting to connect

  parent - the parent object to be passed to the QTcpSocket constructor @threadsafe

- synchronousConnectToHost (url, msecs, parent)

  returns QTcpSocket

  Argument | Type | Default
  --- | --- | ---
  url | QUrl |
  msecs | int | 30000
  parent | QObject | 0

- synchronousConnectToHost (socket, protocol, host, port, msecs)

  returns Nothing

  Argument | Type | Default
  --- | --- | ---
  socket | QTcpSocket |
  protocol | QString |
  host | QString |
  port | quint16 |
  msecs | int | 30000

- synchronousConnectToHost (socket, url, msecs)

  returns Nothing

  Argument | Type | Default
  --- | --- | ---
  socket | QTcpSocket |
  url | QUrl |
  msecs | int | 30000