I am trying to create an XML based protocol, using Qt4, and I am having problems. The problem is that only 2*n+1 connection from the client gets into the server. The client is ok (I think), since I recoded it in C# and found the same problem.
I am not sure why the server is "eating" some of the XML sent to it. Can anyone give me a hint? To debug this I used "nc -l" and I see that the client does send the correct data. When I use "nc" to emulate the client, I see that the server is not processing the datastream. The correct method is invoked but the XML is not read.
/////////////////////////////////////
// client code, parts of it at least
/////////////////////////////////////
MyClient::MyClient()
{
}
void MyClient
::connect( QString host,
int port
) {
connection->connectToHost(host,port);
if (!connection->waitForConnected()){
qDebug("Aborting, no connection");
}
}
void MyClient
::sendConnectionString(QString s
) {
QXmlStreamWriter w(connection);
// send some XML
}
/////////////////////////////////////
// client code, parts of it at least
/////////////////////////////////////
MyClient::MyClient()
{
connection = new QTcpSocket(this);
}
void MyClient::connect( QString host, int port )
{
connection->connectToHost(host,port);
if (!connection->waitForConnected()){
qDebug("Aborting, no connection");
}
}
void MyClient::sendConnectionString(QString s)
{
QXmlStreamWriter w(connection);
// send some XML
}
To copy to clipboard, switch view to plain text mode
/////////////////////////////////////
// this is the server
/////////////////////////////////////
{
qDebug("Server started");
connect(server, SIGNAL(newConnection()), this, SLOT(on_newConnection()));
}
void MyServer::start()
{
qDebug("Started server");
}
void MyServer::on_newConnection()
{
connect(s,SIGNAL(readyRead()), this, SLOT(on_bytesArrived()));
connect(s,SIGNAL(disconnected()), this, SLOT(on_connectionClosed()));
qDebug("New connection from %s:%d", qPrintable(s->peerAddress().toString()), s->peerPort() );
}
void MyServer::on_connectionClosed()
{
QTcpSocket *s
= qobject_cast<QTcpSocket
*>
(sender
());
if ( s == NULL)
return;
qDebug("Connection from %s:%d closed", qPrintable(s->peerAddress().toString()), s->peerPort() );
}
void MyServer::on_bytesArrived()
{
qDebug("1111111"); // I do see this
QTcpSocket *s
= qobject_cast<QTcpSocket
*>
(sender
());
if ( s == NULL) {
qDebug("No socket");
return;
}
d.setContent(s);
// I don't see this following line
qDebug("read\n%s", qPrintable(d.toString(4)));
}
/////////////////////////////////////
// this is the server
/////////////////////////////////////
MyServer::MyServer(QObject *parent): QObject(parent)
{
qDebug("Server started");
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(on_newConnection()));
}
void MyServer::start()
{
server->listen(QHostAddress::Any,2008);
qDebug("Started server");
}
void MyServer::on_newConnection()
{
QTcpSocket *s = server->nextPendingConnection();
connect(s,SIGNAL(readyRead()), this, SLOT(on_bytesArrived()));
connect(s,SIGNAL(disconnected()), this, SLOT(on_connectionClosed()));
qDebug("New connection from %s:%d", qPrintable(s->peerAddress().toString()), s->peerPort() );
}
void MyServer::on_connectionClosed()
{
QTcpSocket *s = qobject_cast<QTcpSocket*>(sender());
if ( s == NULL)
return;
qDebug("Connection from %s:%d closed", qPrintable(s->peerAddress().toString()), s->peerPort() );
}
void MyServer::on_bytesArrived()
{
qDebug("1111111"); // I do see this
QTcpSocket *s = qobject_cast<QTcpSocket*>(sender());
if ( s == NULL) {
qDebug("No socket");
return;
}
QDomDocument d;
d.setContent(s);
// I don't see this following line
qDebug("read\n%s", qPrintable(d.toString(4)));
}
To copy to clipboard, switch view to plain text mode
PS: I do know I need to fix the code to use QSignalMapper. This will be done as soon as I find the problem here.
EDIT:
I see this thread, but I don't learn anything about my problem... Am I close...?
http://www.qtcentre.org/threads/1991...Server-problem
http://lists.trolltech.com/qt-intere...ad01624-0.html
Bookmarks