PDA

View Full Version : Qt Network Programming



Walsi
25th May 2007, 09:50
Hy!

I got a problem with small Qt Network application. I got a Client and a Server which communicates with TCP. I want, that they are able to send messages to each other. I am in an early development stage of this application and I am testing fundamental function at the moment. Following flowchat describes the problem.

..............CLIENT.............................. .................SERVER


........................TCP connect [SYN], [SYN,ACK], [ACK]
....................<---------------- ---------------------------------->

....................<----------------------------------------------------- ......sendResponse
.....................................TCP Message [PSH].................................Button

....................----------------------------------------------------->
..................................TCP Acknowledge [ACK]
display the
message

.................................................. ..........................<---- *


When I try to send at the end of the flowchat (at *) I got the Message on the console of the Server: SEGMENTATION Fault

The SourceCode for sending messages on the Server is following:


void Server::sendResponse()
{
QString responseMessage;
responseMessage = "ACK Hello";
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << responseMessage;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));

QTcpSocket *clientConnection = tcpServer->nextPendingConnection();

clientConnection->write(block);
}


What is the problem, what can do to solve it?



Best Regards!

wysota
25th May 2007, 10:14
Could you run your application through a debugger and print the backtrace?

marcel
25th May 2007, 10:24
If there are no pending connections then nextPendingConnection returns NULL.
You should test for that before calling clientConnection->write().

Regards

high_flyer
25th May 2007, 10:32
I can't understand your code when compared to your flow diagram.
What is this part for?:


out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));


Also, I am not sure this slot is a good place for:


QTcpSocket *clientConnection = tcpServer->nextPendingConnection();

Since you assume that every time you get data from what ever socket, there is another pending connection - and there is no assurance for that.
So this code:

QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
clientConnection->write(block);
Is probably what is crashing your application, when clientConnection is NULL.

At the very least you should test it first:


QTcpSocket *clientConnection = NULL;
if(tcpServer->hasPendingConnections()
{
clientConnection = tcpServer->nextPendingConnection();
clientConnection->write(block);
}

EDIT: beat to it by marcel :)

Walsi
25th May 2007, 10:46
Could you run your application through a debugger and print the backtrace?


OMG!

What do you mean with the backtrace?
You know, .... I am one of the newbs! ;-)

Walsi
25th May 2007, 10:50
Thx High_flyer,
Thx marcel

I am going to test this and I will reconsider my code, and if I have any further problems, you will tell you! ;-)

Many thx to you!