QTcpServer: Kann nicht lesen

Alles rund um die Programmierung mit Qt
Antworten
moe szyslak
Beiträge: 1
Registriert: 23. Januar 2006 17:53

QTcpServer: Kann nicht lesen

Beitrag von moe szyslak »

AeAeAeh.....
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;
};
Naja, vielleicht schaut einer von euch mal kurz den Code an und findet sogar evtl. den Fehler!!!

mfg
dynamo
Beiträge: 7
Registriert: 16. Oktober 2004 15:37

Beitrag von dynamo »

versuch mal so zu lesen


if (!m_pTcpSocket)
return;

bool wait = m_pTcpSocket->waitForReadyRead(500);

if (!m_pTcpSocket->bytesAvailable())
return;

QDataStream in(m_pTcpSocket);
in.setVersion(QDataStream::Qt_4_0);
qint16 blockSize = 0;

if (m_pTcpSocket->bytesAvailable() < (int)sizeof(qint16)){
m_output->append("no bytes availabe");
return;
}

in >> blockSize;

qint64 b = m_pTcpSocket->bytesAvailable();
if (m_pTcpSocket->bytesAvailable() < blockSize){
m_output->append(QString("Bytes available: %1, Blocksize = %2").arg(b).arg(blockSize));
return;
}

QString inputData;
in >> inputData;
if (inputData.size() == 0)
return;

m_output->append(QString("response data from client: %1").arg(inputData));
lepsai
Beiträge: 573
Registriert: 14. September 2004 21:33
Wohnort: Berlin
Kontaktdaten:

Beitrag von lepsai »

dein socket musste nicht selber erstellen. sondern du kriegst den durch QTcpServer::nextPendingConnection()... und zwar dann wenn eine neue Verbindung hergestellt wird. dafür connecten:

connect( tcpServer, SIGNAL(newConnection), myClass, (onNewConnection() ) );
Antworten