QTcpServer synchronous connection

876 views Asked by At

I was trying to use the synchronous connection mechanism of QTcpServer to implement a server. My code is very simple one. PFB.

//////

MainScreen::MainScreen(QWidget *parent) :
QMainWindow(parent),
    ui(new Ui::MainScreen)
{
    ui->setupUi(this);
    server = new QTcpServer();

}


int MainScreen::waitForConnection()
{
    bool timeOut = 0;

    if (server->listen(QHostAddress("192.168.70.30"), 10000))
    {

        quint16 port = server->serverPort();
        qDebug() << "Server : "<< (server->serverAddress()).toString() << "Port : "<< port;
        server->waitForNewConnection(-1, &timeOut);
    }

    return 0;
}

//main

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainScreen w;

    w.waitForConnection();

    return a.exec();

}

And I'm trying to connect to the server from a Linux client application(C). Unfortunately I'm not getting any connection in my server. Do I need to do something else to work on synchronous connection? Thanks in advance

PFB result of netstat

netstat -np TCP

Active Connections

Proto Local Address Foreign Address State

TCP 127.0.0.1:62908 127.0.0.1:62909 ESTABLISHED

TCP 127.0.0.1:62909 127.0.0.1:62908 ESTABLISHED

TCP 192.168.70.89:62817 10.47.1.10:49224 ESTABLISHED

TCP 192.168.70.89:62924 10.47.1.101:3128 ESTABLISHED

TCP 192.168.70.89:63209 10.47.1.101:3128 TIME_WAIT

TCP 192.168.70.89:63213 10.47.1.101:3128 TIME_WAIT

TCP 192.168.70.89:63264 10.47.1.101:3128 TIME_WAIT

TCP 192.168.70.89:63265 10.47.1.101:3128 TIME_WAIT

TCP 192.168.70.89:63266 10.47.1.11:8014 CLOSE_WAIT

TCP 192.168.70.89:63267 111.221.112.54:995 TIME_WAIT

TCP 127.0.0.1:62908 127.0.0.1:62909 ESTABLISHED

1

There are 1 answers

1
fleed On

A few things to check:

  • On the server, is it actually listening for incoming connections? Run netstat -lt on linux or netstat -ta on windows and check if the port is open on a listening status.
  • Use QHostAddress::Any rather than a specific address on the host, in case it has a problem with that address.

Addition: You have to run netstat in windows with the command netstat -nap TCP so it shows the listening ports. But since you can connect to your network server locally then that shows it is working. It looks like you're running under windows. If so have you tried to disable window's firewall? If you're running an anti-virus, try disabling that too in case it's blocking connections from outside the machine. Lastly, make sure the machines can see each other through ping.