0

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

vraj010
  • 116
  • 1
  • 2
  • 8
  • How do you try to connect to your server? What port do you use? The server listens on "192.168.70.30", yes. But on a random port. – Greenflow Aug 12 '13 at 16:01
  • I'm trying to connect to the server through port '10000' – vraj010 Aug 13 '13 at 04:19
  • fleet's advice is good. Try QHostAddress::Any. Or try 127.0.0.1 and see if you can connect locally. Your code is good and should work. I suppose when you tried, your machine really had 192.168.70.30? Because when you used netstat it was 192.168.70.89. – Greenflow Aug 13 '13 at 05:00
  • Now Iam using QHostAddress::Any, and my server is on 192.168.70.89. The netstat result is not showing my port. – vraj010 Aug 13 '13 at 05:05
  • The code is good. I copy/pasted your code, just to be sure. I had no problems to connect using telnet. Looks more like your network. Whatever the problem is, it is not the lines above. – Greenflow Aug 13 '13 at 05:11
  • Change netstat -np TCP to netstat -nlp TCP. – Greenflow Aug 13 '13 at 05:18
  • Yea...I tried to connect using putty from the same PC. That's working..But not from other PCs in the network..I'm new to this network related things – vraj010 Aug 13 '13 at 05:27
  • This is more or less the end of this 'thread'. Even for you your code works then. Very hard if not impossible from here to debug your local network configuration. :-) – Greenflow Aug 13 '13 at 05:47
  • OK thanks...dono how to solve this... – vraj010 Aug 13 '13 at 06:04

1 Answers1

1

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.

fleed
  • 640
  • 9
  • 15
  • I have added netstat result....I couldn't find my port in that. And I tried with QHostAddress::Any without success. – vraj010 Aug 13 '13 at 04:40