Bind or "connect" QUdpSocket to remote host and port?

Publish date: 2024-05-07

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

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoam5rYmeEdX%2BOm6CnnF2kv26vzqelnpukYr62sM%2Bsppyjlal6tbuMq5ymp6Saeqm70q1kmqaUYr2wvtM%3D