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 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.

4

1 Answer

bind() binds a socket to a local address. connect() connects a socket to a remote address. Usually servers bind and clients connect.

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest