Seit drei Wochen arbeite ich an einenn TCP-Server, der mich langsam zur Fustration treibt. Ich meine, Schreiben ist kein Problem, aber beim besten willen, ich kann nicht lesen ...
Code: Alles auswählen
#include "m4uTcpServer.h"
M4uTcpServer::M4uTcpServer( QHostAddress& host, quint16 port ) :
m4uTcpHost( host ), m4uTcpPort( port )
{
DEBUGNL( __LINE__ << ": " << __FILE__ << ": " << __func__
<< " ip: " << host.toString().toStdString() << " port: " << port );
if ( !listen( m4uTcpHost, m4uTcpPort ) )
{
DEBUGERR( __LINE__ << ": " << __FILE__ << ": " << __func__ );
DEBUGERR( "TcpServer can't listen!!!" );
close();
return;
}
};
M4uTcpServer::~M4uTcpServer()
{
DEBUGNL( __LINE__ << ": " << __FILE__ << ": " << __func__ );
};
void
M4uTcpServer::m4uWriteMessageToClient( QDomDocument& document, QHostAddress& host, quint16 port )
{
DEBUGNL( __LINE__ << ": " << __FILE__ << ": " << __func__ );
DEBUGNL( "DEBUG: " << std::endl << document.toByteArray().data() );
QTcpSocket socket;
socket.connectToHost( host, port, QIODevice::WriteOnly );
if ( socket.waitForConnected ( 1000 ) )
{
socket.write( document.toByteArray() );
socket.flush();
}
socket.close();
socket.disconnectFromHost();
};
void
M4uTcpServer::incomingConnection( int handle )
{
DEBUGNL( __LINE__ << ": " << __FILE__ << ": " << __func__ );
QTcpSocket* socket = new QTcpSocket( this );
DEBUGNL( __LINE__ << ": " << __FILE__ << ": " << __func__ << " state: " << socket->state() );
if ( socket->setSocketDescriptor( handle ) )
{
DEBUGNL( __LINE__ << ": " << __FILE__ << ": " << __func__ << " state: " << socket->state()
<< " IP: " << socket->peerAddress().toString().toStdString() << " port: " << socket->peerPort() );
if ( socket->state() == QAbstractSocket::ConnectedState && socket->waitForConnected( 1000 ) )
{
DEBUGNL( __LINE__ << ": " << __FILE__ << ": " << __func__ << " state: " << socket->state() );
QByteArray r = socket->readAll();
DEBUGNL( "DEBUG: " << std::endl << r.data() );
}
}
DEBUGNL( __LINE__ << ": " << __FILE__ << ": " << __func__ << " state: " << socket->state() );
socket->disconnectFromHost();
socket->close();
delete socket;
};mfg