Ilian Zapryanov
Ilian Zapryanov

Reputation: 1158

QUdpSocket client-server remote host not connectable

I've seen few threads about my question, but, still I can't seem to solve the problem, and the replies are not sufficient. So here is the task: I have 2 PCs. One must transmit 100 udp packets, with a simple test data, and the other machine must read the datagrams. My code is as follows:

The server:

m_socket.udp = new QUdpSocket(this);

if (m_socket.udp->bind(QHostAddress("192.168.32.154"), 1234)) {
    m_socket.udp->connectToHost(QHostAddress("192.168.32.154"), 1234);
    m_socket.udp->waitForConnected();
}
connect(m_socket.udp, SIGNAL(connected()),
        this, SLOT(handleConnection()));
connect(m_socket.udp, SIGNAL(readyRead()),
        this, SLOT(readyReadUdp()));

So... first - the binding to IP of machine 1 fails. I must not specify it's IP.

The client is simple:

p_socket = new QUdpSocket(this);
p_socket->connectToHost(QHostAddress("192.168.32.94"), 1234);
connect(p_socket, SIGNAL(connected()), this, SLOT(writeDgram()));
....
void writeDgram() {
    p_socket->write(QByteArray("test"));
}

So client code, as viewed in wireshark, comes to my server machine. But my server Qt code fails me. Any help here?

Upvotes: 1

Views: 1043

Answers (2)

Ilian Zapryanov
Ilian Zapryanov

Reputation: 1158

Yet, no one proposed that I can be firewalled. That was the problem. Removing the firewall solved this.

Upvotes: 1

Ilian Zapryanov
Ilian Zapryanov

Reputation: 1158

I've setup another udp socket to handle incoming connections that way:

    void Server::handleConnection()
{
    std::cout << "Connected to host" << std::endl;
    m_inaddr = new QUdpSocket(this);
    connect(m_inaddr, SIGNAL(readyRead()),
            this, SLOT(readyReadUdp()));

    QHostAddress addr = m_socket.udp->peerAddress();
    quint16 port = m_socket.udp->peerPort();

    bool conn = m_inaddr->bind(45678);

    if (conn) {
        m_inaddr->connectToHost(addr, port);
        std::cout << "Bound to: " << addr.toString().toStdString()
                  << " port:"
                  << port << std::endl;
    }

}

But it seems it's not working the proper way. When I am connected, I can read the peer host and port, but the auxilary socket, for inaddress must be bound to what? I am getting confused with the UDP. [EDIT] Just to add that I never break in the readyReadUdp() slot, I`ve put a breakpoint there. The reader is standart one, but the slot is never called. So I guess there is nothing ready to read.

Upvotes: 0

Related Questions