Bind or "Connect" Qudpsocket to Remote Host and Port?
I Would Like to "Connect" Qudpsocket to a Remote Host-Port, but Fail to Do So. Is It Possible? the Scenario I Would Like to Implement Is the Following: 1) the...
I would like to "connect" QUdpSocket to a remote host-port, but fail to do so. Is it possible?
The scenario I would like to implement is the following:
1) The server binds to localhost/port:
// On server side, let server IP be "192.168.0.235"
serverUdpSocket->connectToHost(QHostAddress(QHostAddress::LocalHost), 44444);
... // check if connection is ok, etc
serverUdpSocket->write(someByteArray);
2) The client reads data from server, I tried:
// bind fails with SocketAddressNotAvailableError
udpSocket->bind(QHostAddress("192.168.0.235"), 44444);
and this way:
udpSocket->connectToHost(QHostAddress("192.168.0.235"), 44444, QIODevice::ReadOnly);
// State becomes "Connected", but I don't get any data and `readyRead()`
// is never emitted, though I connected to it.
but it doesn't work.
I know that UDP is a connectionless protocol. A also managed to do it vice versa - bind to local host and send data to this host from another. But I'm interested in this way of doing it, as remote host might be a server providing audio stream, that I want to read with my code.
In examples and tutorials I only see binding to local port and reading data from it. No examples with binding to remote host-port provided.
1 Answer
bind() binds a socket to a local address. connect() connects a socket to a remote address. Usually servers bind and clients connect.