Hallo,
ich möchte gerne Daten über den Localhost verschicken
Hier mal mein Beispiel:
Implementierung Client (QVbox):
Code: Alles auswählen
#include "client.moc"
#include "..//network_types.h"
#include <iostream>
client::client( const QString& host, int port )
{
resize(600, 400);
QHBox *box1 = new QHBox( this );
infoText = new QLineEdit( box1 );
QHBox *box2 = new QHBox( this );
ausgabe = new QLineEdit( box2 );
connect( infoText, SIGNAL(returnPressed()), SLOT(sendToServer()) );
socket = new QSocket( this );
connect( socket, SIGNAL(connected()), this, SLOT(socketConnectedSlot()) );
connect( socket, SIGNAL(connectionClosed()), this, SLOT(socketConnectionClosedSlot()) );
connect( socket, SIGNAL(readyRead()), this, SLOT(socketReadyReadSlot()) );
connect( socket, SIGNAL( error(int) ), this, SLOT( errorSlot(int) ) );
socket->connectToHost( "127.0.0.1", PORT ); //PORT 12345 per define
}
void client::socketConnectedSlot()
{
std::cout << "client::socketConnected()" << std:: endl;
}
void client::socketConnectionClosedSlot()
{
delete socket;
std::cout << "client::socketConnectionClosed()" << std:: endl;
exit(1);
}
void client::socketReadyReadSlot()
{
std::cout << "client::socketReadyRead()" << std:: endl;
}
void client::errorSlot(int errorInt)
{
std::cout << "ERROR NO: " << errorInt << std::endl;
}
void client::sendToServer()
{
QTextStream tstr(socket);
tstr << infoText->text();
std::cout << "client::sendToServer(): " << infoText->text() << std:: endl;
infoText->clear();
}
Code: Alles auswählen
#include "server.moc"
#include "..//network_types.h"
void server::newConnection( int socketId )
{
QSocket* socket = new QSocket( this );
socket->setSocket( socketId );
connect( socket, SIGNAL( delayedCloseFinished() ),this, SLOT( deleteSocketSlot() ) );
connect( socket, SIGNAL( readyRead() ),this, SLOT( incomingDataSlot() ) );
connect( socket, SIGNAL( connectionClosed() ),this, SLOT( lostConnectionSlot() ) );
connect( socket, SIGNAL( error(int) ),this, SLOT( errorSlot(int) ) );
std::cout << "server::newConnection()" << std::endl;
}
void server::deleteSocketSlot()
{
QSocket* socket =(QSocket*)sender();
delete socket;
socket = NULL;
std::cout << "server::deleteSocketSlot()" << std::endl;
}
void server::incomingDataSlot()
{
QSocket* nsocket =(QSocket*)sender();
QTextStream data( nsocket );
if( nsocket->canReadLine() )
{
std::cout << "CAN READ\n";
}
else
{
std::cout << "CAN NOT READ " << data.readLine() << std::endl;
}
}
void server::lostConnectionSlot()
{
QSocket* socket = (QSocket*)sender();
socket->close();
delete socket;
std::cout << "server::lostConnectionSlot(): SOCKET CLOSED" << std::endl;
}
void server::errorSlot(int errorInt)
{
std::cout << "ERROR NO: " << errorInt << std::endl;
}
Hat jemand ne Erklärung dafür?
Und wo ich gerade schonmal hier bin. Wie kann ich später andere Daten als Text verschicken z.B struct?
QDataStream?
Vielen Dank schonmal für eure Hilfe....
Okay das Problem war, dass in dem Text kein Zeilenumbruch enthalten war. D.h. canReadLine() eignet sich nicht zum verschicken einzelner Textzeilen ohne Umbruch. Wäre dann nicht Frage nach noch verfügbaren Daten sinnvoller?? Gibt es soetwas?