Socket Object
The Socket
object can be used as TCP client socket to connect to a server socket to read and write strings.
Example:
var socket = Socket.connect("192.168.1.42", 4711); socket.encoding = "latin1"; socket.writeString("hello"); var line = socket.readLine(); socket.close();
socket.close()
This method closes the socket connection. Once closed a socket object cannot be read from or written to.
socket Socket.connect(host, port)
This function tries to connect to the specified host name (or the IP address) and port and returns a socket object on success. If the connection failed a catchable exception is thrown.
String socket.encoding
The socket
object's encoding
property holds the encoding to read and write strings. The default encoding is "utf-8"
. Other possible values are "ucs-2"
(2 byte Unicode) and "latin1"
(also known as ISO 8859-1) which includes the US-ASCII range.
This property should be set before doing any reading or writing of strings.
String socket.localAddress
The socket
object's localAddress
property holds the address used on the local machine for the socket.
Number socket.localPort
The socket
object's localPort
property holds the port used on the local machine for the socket.
Number socket.readInt8()
This method reads a signed 8-bit integer from the socket. On success the integer value is returned otherwise a catchable exception is thrown.
Number socket.readInt32()
This method reads a signed 32-bit integer from the socket. On success the integer value is returned otherwise a catchable exception is thrown.
String socket.readLine()
This method reads until one of the line endings "\n"
or "\r\n"
are read from the socket returned by the socket Socket.connect(host, port) function. The content is assumed to be text using the UTF-8 encoding (unless the socket.encoding property is changed). On success the string is returned without line ending. If no line ending is received a catchable exception is thrown.
String socket.remoteAddress
The socket
object's remoteAddress
property holds the address used on the remote machine for the socket.
Number socket.remotePort
The socket
object's remotePort
property holds the port used on the remote machine for the socket.
Number socket.timeout
The socket
object's timeout
property holds the timeout used for read and write operations on the socket. It's an integer number of seconds which defaults to 20 seconds.
This property should be set before doing any reading or writing.
Socket.writeString(string)
This method writes the given string
to the socket object returned by the socket Socket.connect(host, port) function. The string
is written using the UTF-8 encoding (unless the socket.encoding property is changed). If the writing fails a catchable exception is thrown.
© 2024 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 the GNU 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.