QTCPSocket not connecting
I am running the Fortune Server and Fortune Client examples. Unfortunately, both programs start up fine, but they do not make a TCP socket connection. The server says it is listening, and 'netstat' confirms this. After the client calls 'connectToHost', the socket is supposedly open. Here is the code that I use to confirm that:
Code:
getFortuneButton->setEnabled(false);
blockSize = 0;
tcpSocket->abort();
tcpSocket->connectToHost(hostLineEdit->text(),
portLineEdit->text().toInt());
if (tcpSocket->isOpen ())
statusLabel->setText ("It is open.");
else
statusLabel->setText ("It is NOT open.");
I even tried to connect the client to another (non-QT) server application I have and the QT client does not make the connection.
Does anyone know of any problems with the Fortune Server, Fortune Client examples?
Thanks,
DP
Re: QTCPSocket not connecting
Maybe your firewall is blocking it? And do you use a vanilla fortune client/server or did you modify them in any way?
The test case you have shown won't work. The socket might be "open", but it doesn't mean the connection is active. Remember that connectToHost is an asynchronous call.
Re: QTCPSocket not connecting
I can't see how the firewall is blocking since I use TCP connections all the time on my machine and to my laptop on the same network behind the corp firewall.
I am using the vanilla program except that I added a callback for the connected signal:
Code:
connect (tcpSocket, SIGNAL(connected ()), this, SLOT(connectionComplete ()));
...
void Client::connectionComplete ()
{
statusLabel->setText ("Connection is complete.");
}
Re: QTCPSocket not connecting
Quote:
Originally Posted by DPinLV
I can't see how the firewall is blocking since I use TCP connections all the time on my machine and to my laptop on the same network behind the corp firewall.
I don't know, it's just a thing to check to identify the problem. Maybe the port is blocked or something like that. Do you use the same machine for the client and the server?
Quote:
I am using the vanilla program except that I added a callback for the connected signal:
Does the vanilla client-server combination work?
If you run the server, are you able to telnet to the port it is running on?
Re: QTCPSocket not connecting
Yes, please try using the Telnet client to connect first to see it is the problem of firewall or not.
Re: QTCPSocket not connecting
Quote:
Originally Posted by wysota
I don't know, it's just a thing to check to identify the problem. Maybe the port is blocked or something like that.
Do you use the same machine for the client and the server?
Does the vanilla client-server combination work?
Yes, they are on the same machine. This is the plain vanilla client / server, and they do not work. I then added slots to the client for when the connection is done, and I put breakpoints in those methods, but they are never reached:
Code:
connect (tcpSocket, SIGNAL(connected ()), this, SLOT(connectionComplete ()));
connect (tcpSocket, SIGNAL(hostFound ()), this, SLOT(hostFoundComplete ()) );
Next, I added a button to get the status of the client connection and supposedly the connection is in the "connected state". I even looked at netstat and the connection is established, but no data is exchanged.
Code:
connect (statusButton, SIGNAL(clicked()), this, SLOT(getSocketState ()) );
...
void Client::getSocketState ()
{
switch (tcpSocket->state ())
{
status2Label->setText (tr("unconnected state"));
break;
status2Label->setText (tr("host lookup state"));
break;
status2Label->setText (tr("connecting state"));
break;
status2Label->setText (tr("connected state"));
break;
status2Label->setText (tr("bound state"));
break;
status2Label->setText (tr("Closing state"));
break;
status2Label->setText (tr("Listening state"));
break;
default:
status2Label->setText (tr("Unknown state"));
break;
}
}
Quote:
Originally Posted by wysota
If you run the server, are you able to telnet to the port it is running on?
When I telnet to the server my server breakpoints are not hit, but if I close the server, my telnet window sees the closure. It is as though the connection is never 'accepted'?
Thanks.
DP
Re: QTCPSocket not connecting
Your computer seems to be blocking the connection somehow... Do other networking examples work? Also please try to start a fake server on the same port the fortunecookie server uses (for example by using netcat) and then try to connect to it using telnet and send some lines of text to it.