PDA

View Full Version : QTCPSocket not connecting



DPinLV
24th August 2006, 19:10
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:

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

wysota
24th August 2006, 19:13
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.

DPinLV
24th August 2006, 19:18
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:

connect (tcpSocket, SIGNAL(connected ()), this, SLOT(connectionComplete ()));

...

void Client::connectionComplete ()
{
statusLabel->setText ("Connection is complete.");
}

wysota
25th August 2006, 00:41
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?


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?

ball
25th August 2006, 04:12
Yes, please try using the Telnet client to connect first to see it is the problem of firewall or not.

DPinLV
25th August 2006, 07:17
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:

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.

connect (statusButton, SIGNAL(clicked()), this, SLOT(getSocketState ()) );
...
void Client::getSocketState ()
{
switch (tcpSocket->state ())
{
case QAbstractSocket::UnconnectedState:
status2Label->setText (tr("unconnected state"));
break;
case QAbstractSocket::HostLookupState:
status2Label->setText (tr("host lookup state"));
break;
case QAbstractSocket::ConnectingState:
status2Label->setText (tr("connecting state"));
break;
case QAbstractSocket::ConnectedState:
status2Label->setText (tr("connected state"));
break;
case QAbstractSocket::BoundState:
status2Label->setText (tr("bound state"));
break;
case QAbstractSocket::ClosingState:
status2Label->setText (tr("Closing state"));
break;
case QAbstractSocket::ListeningState:
status2Label->setText (tr("Listening state"));
break;
default:
status2Label->setText (tr("Unknown state"));
break;
}
}


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

wysota
25th August 2006, 08:18
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.