Seite 1 von 1

QTcpServer QTCPSocket

Verfasst: 18. Juni 2007 11:31
von gyrosundmaronen
Moin, Moin

Hab n'paar Probleme mit meiner Client-Server Anwendung. Der Client stellt eine Verbindung zum Server her. Aber er empfängt keine Daten. Nun weiß ich allerdings nicht ob das an meiner Empfangs- oder Senderoutine liegt!?
Kann mir einer helfen hier mal meine Code

Client (TCPComm.h):

Code: Alles auswählen

#ifndef _TCPCOMM_H__
#define _TCPCOMM_H__

#include <QTcpSocket>
#include <QIODevice>

class TCPComm : public QObject
{
	Q_OBJECT

public:

	TCPComm();

	virtual ~TCPComm();

	void connectToHost( void );
	void writeByte		  ( QByteArray bytes );

protected:

	QTcpSocket *_psock; 


};

class SendData 
{
public:
	
	void sendDataTCP ( void );
};
	
#endif

TCPComm.cpp

Code: Alles auswählen


#include "TCPComm.h"
#include <iostream>

TCPComm::TCPComm()
{
}
TCPComm::~TCPComm()
{
}
void TCPComm::connectToHost( void )
{
	
   QString ip("127.0.0.1"); 
	std::cout << "TCP Connect" << std::endl;
	
	

	_psock = new QTcpSocket();
	_psock->disconnectFromHost();

	_psock->connectToHost(ip,7,QIODevice::ReadWrite);
	
}
void TCPComm::writeByte(QByteArray bytes)
{
	if(!_psock)
		return;

	_psock->write(bytes);
	std::cout << "Bytes Groesse:" << bytes.length() << std::endl;
//	std::cout << _psock->peerAddress << std::endl;

	for (int i = 0; i < bytes.length(); i++)
	{
		std::cout << QString::number(bytes.at(i)).toStdString() << std::endl;
	}

}

int main(int argc, char* argv[])
{

	
	SendData *m_send = new SendData;

	m_send->sendDataTCP();
	return 0;
}
void SendData::sendDataTCP()
{
	QByteArray byte;
	byte.clear();
	
	int b;
	b = 1;
	byte.append(b);
	b =  2;
	byte.append(b);
	b =  3;
	byte.append(b);
	b =  4;
	byte.append(b);
	
	
	TCPComm  *m_tcp; 
	m_tcp	= new TCPComm();
	m_tcp->connectToHost();

	m_tcp->writeByte(byte);
	
}
Server (TCPServer.h)

Code: Alles auswählen

#ifndef _TCPSERVER_H__
#define _TCPSERVER_H__

#include <QApplication>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include <iostream>




class TCPServer : public QObject
{
	Q_OBJECT

public:
	TCPServer()
	{
		
	
	}

	void listenData ( void );
	QTcpServer	*m_server;
	QTcpSocket  *m_socket;
	QTcpSocket	*_psock;

protected:

		

protected slots:

	void	slotrec ( void );
	
	void    readFromClient( void );
	

};

#endif
TCPServer.cpp

Code: Alles auswählen

#include "TCPServer.h"
#include <iostream>

/////////////////////////////////////////////////////////////////////////////////////////

void TCPServer::listenData()
{

	QString ip = "127.0.0.1";
	
	m_server = new QTcpServer();
	m_socket = 0;
//	connect(&_server,SIGNAL(newConnection()),this,SLOT(slotNewConnection()));
	m_server->listen(QHostAddress::Any, 7);//QHostAddress::Any,6666);
	
	if (!m_server->isListening())
		std::cout << "Server im Arsch" << std::endl;
	else
	{
		std::cout << "Laeuft ne!!!" << std::endl;
		std::cout << m_server->serverPort() << std::endl;
//		slotsend();
		connect(m_server,SIGNAL(newConnection()),this,SLOT(slotrec()));
	}
}
/////////////////////////////////////////////////////////////////////////////////////////

void TCPServer::readFromClient( void )
{
		std::cout << "ReadFromClient" << std::endl;
		if (m_socket->canReadLine()) 
			{
				_psock = m_socket;
				QByteArray bytes = m_socket->readAll();
		
				for (int i = 0; i < bytes.length(); i++)
				{
				std::cout << QString(bytes.at(i)).toStdString() << std::endl;
				}
			}
}
/////////////////////////////////////////////////////////////////////////////////////////

void TCPServer::slotrec( void )
{
		QByteArray bytes;
		bytes.clear();
		std::cout << "Verbindung steht" << std::endl;
		


		if (m_socket == 0)
		{
			m_socket = m_server->nextPendingConnection();
			std::cout << "PeerPort:" << m_socket->peerPort() << std::endl;

//		    m_socket->waitForReadyRead(-1 );

			connect(m_socket, SIGNAL(readyRead()),this, SLOT(readFromClient()));

		}
		m_socket = 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{

	QApplication app(argc, argv);
	TCPServer *server;
	server = new TCPServer();
	server->listenData();
	
	return app.exec();
	
}
Danke schon mal vorab...

Beste Grüsse

gyros

Verfasst: 18. Juni 2007 12:42
von Christian81
Der Server ohne MainLoop? mhhh wie sollte das denn gehen?

Verfasst: 18. Juni 2007 12:56
von gyrosundmaronen
Moin Christian

Hab die Empfangsroutine in eine while-Schleife gepackt, aber irgendwie funzt das trotzdem nicht.

Code: Alles auswählen

while (m_server->hasPendingConnections()) 
			{
//				_psock = m_socket;
				 
				 bytes = m_socket->readAll();
				 std::cout << "Bytes:" <<	bytes.length() << std::endl;
				for (int i = 0; i < bytes.length(); i++)
				{
					std::cout << "Bytes empfangen" << std::endl;
					std::cout << QString(bytes.at(i)).toStdString() << std::endl;
				}
			}
Also die Verbindung zwischen Client und Server steht. Das Signal newConnection wird emitiert und ruft mit SignalSlot die Empfangsroutine auf. Aber wenn ich mit readAll() bytes emfangen möchte kommt nüscht an.

Beste Grüsse

gyros[/quote]

Verfasst: 18. Juni 2007 13:01
von Christian81
Mist, ich meinte ja den Client.
Und warum wird beim Server QApplication anstatt QCoreApplication benutzt? Oder ist es ein 'graphischer Server'?

Verfasst: 18. Juni 2007 13:37
von gyrosundmaronen
Also ich hab die Sendefunktion in eine while Schlaife gepackt. Er sendet ständig bytes aber es kommt einfach nichts an. Kann mir das nicht erklären.... Es ist kein grafischer Server

Beste Grüsse

gyros

Verfasst: 18. Juni 2007 13:38
von Christian81
Ok, nochmal. Du brauchst eine Q(Core)Application für die Eventloop!

Verfasst: 18. Juni 2007 13:45
von gyrosundmaronen
Geil Christian ich danke dir. Ich dachte ich brauch den EventLoop nur für den Server. Kannst du mir das erklären?
Aber es funktioniert soweit...

Gruss gyros

Verfasst: 18. Juni 2007 13:46
von Christian81
Man braucht immer eine Eventloop wenn man irgendwas mit Signals/Slots machen will. Auch andere Funktionen gehen ohne Eventloop nicht.

Verfasst: 18. Juni 2007 14:07
von gyrosundmaronen
Aha... Das leuchtet ein.
Danke für die Unterstützung.

Gruss

gyros