connectToHost for QUdpSocket
-
I'm working on a class to communicate with a PLC using the Omron FINS (UDP based).
The low level functions are based onread
andwrite
, so according to the docs I have to useconnectToHost
:bool FINS::connectToHost(QHostAddress address, int port) { _address = address; _port = port; _socket = new QUdpSocket(this); _socket->connectToHost(_address, _port); return _socket->waitForConnected(1000); }
The docs for
waitForConnected
says:Waits until the socket is connected, up to msecs milliseconds. If the connection has been established, this function returns true; otherwise it returns false.
I wonder how it can work with a connection-less protocol like UDP.
In fact, it returns true even if the host is offline.I need to find a way to understand if the host is online in order to set a "connected" flag to begin the exchange of data.
-
I'm working on a class to communicate with a PLC using the Omron FINS (UDP based).
The low level functions are based onread
andwrite
, so according to the docs I have to useconnectToHost
:bool FINS::connectToHost(QHostAddress address, int port) { _address = address; _port = port; _socket = new QUdpSocket(this); _socket->connectToHost(_address, _port); return _socket->waitForConnected(1000); }
The docs for
waitForConnected
says:Waits until the socket is connected, up to msecs milliseconds. If the connection has been established, this function returns true; otherwise it returns false.
I wonder how it can work with a connection-less protocol like UDP.
In fact, it returns true even if the host is offline.I need to find a way to understand if the host is online in order to set a "connected" flag to begin the exchange of data.
@Mark81 said in connectToHost for QUdpSocket:
I need to find a way to understand if the host is online in order to set a "connected" flag to begin the exchange of data.
Not a UDP expert. But I don't think you can/are supposed to do this with UDP, which as you say is connection-less. Why do you need to do this? I would have thought at best you can send messages and receive messages, if they arrive, so at best you could send a message and see if your host sends a reply which you receive, that's it?
-
@Mark81 said in connectToHost for QUdpSocket:
I need to find a way to understand if the host is online in order to set a "connected" flag to begin the exchange of data.
Not a UDP expert. But I don't think you can/are supposed to do this with UDP, which as you say is connection-less. Why do you need to do this? I would have thought at best you can send messages and receive messages, if they arrive, so at best you could send a message and see if your host sends a reply which you receive, that's it?
@JonB I could of course. It's not very elegant, indeed.
Anyway, can you help me to understand this sentence in the
QUdpSocket
doc page?If you want to use the standard QIODevice functions read(), readLine(), write(), etc., you must first connect the socket directly to a peer by calling connectToHost().
Because there is no "socket conection" in UDP why they say we MUST connect the socket to the peer?
-
@JonB I could of course. It's not very elegant, indeed.
Anyway, can you help me to understand this sentence in the
QUdpSocket
doc page?If you want to use the standard QIODevice functions read(), readLine(), write(), etc., you must first connect the socket directly to a peer by calling connectToHost().
Because there is no "socket conection" in UDP why they say we MUST connect the socket to the peer?
-
@JonB I could of course. It's not very elegant, indeed.
Anyway, can you help me to understand this sentence in the
QUdpSocket
doc page?If you want to use the standard QIODevice functions read(), readLine(), write(), etc., you must first connect the socket directly to a peer by calling connectToHost().
Because there is no "socket conection" in UDP why they say we MUST connect the socket to the peer?
@Mark81 You can use writeToDatagram() and it will send the packet to the IP/Port without calling connectToHost.
It's saying if you want to use read(), readLine(), write(), you have to connectToHost() first.
Instead, just use writeToDatagram() and readFromDatagram().
-
Have a close read of :
QUdpSocket Documentation in particular the Detailed Description which gives a simple example.
You need a fundamental understanding of how UDP works. It is really quite different than the TCP Socket. You are not creating a "pipe" between end points, so much as a "loose association".
-
I'm working on a class to communicate with a PLC using the Omron FINS (UDP based).
The low level functions are based onread
andwrite
, so according to the docs I have to useconnectToHost
:bool FINS::connectToHost(QHostAddress address, int port) { _address = address; _port = port; _socket = new QUdpSocket(this); _socket->connectToHost(_address, _port); return _socket->waitForConnected(1000); }
The docs for
waitForConnected
says:Waits until the socket is connected, up to msecs milliseconds. If the connection has been established, this function returns true; otherwise it returns false.
I wonder how it can work with a connection-less protocol like UDP.
In fact, it returns true even if the host is offline.I need to find a way to understand if the host is online in order to set a "connected" flag to begin the exchange of data.
@Mark81 said in connectToHost for QUdpSocket:
The low level functions are based on read and write, so according to the docs I have to use connectToHost:
I think you have misunderstood the documentation. There is written:
The most common way to use this class is to bind to an address and port using bind(), then call writeDatagram() and readDatagram() / receiveDatagram() to transfer data. If you want to use the standard QIODevice functions read(), readLine(), write(), etc., you must first connect the socket directly to a peer by calling connectToHost().
You need to use
connectToHost()
to be able to useQIODevice
base functions, but this is not mandatory!
In the case ofQUpdSocket
,connectToHost()
will only record the default endpoint information soQIODevice::write()
will be able to send datagram to the right endpoint.