PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : QT: Qsocket (hylafax)



jonasge
21-12-2002, 18:20
Hallo,

ich versuche gerade eine ClientApplication für den Hylafax-Server zu schreibe, jetzt hänge ich bei der Programmierung der Netzwerkanbindung.

Ich will über das Netzwerk mich mit dem Hylafax-Server verbinden, dieser Lauscht auf Port 4559.

Hier mein Code:


void KHylafax::slotupdate()
{
const QString host="127.0.0.1";
Q_UINT16 port=4559;
cerr << "KHylafax: In slotupdate-Funktion am Anfang...."<< endl;
sock= new QSocket(this);
connect(sock, SIGNAL(connected()), this, SLOT(slotconnected()));
connect(sock, SIGNAL(error(int)), this, SLOT(sloterror(int)));
connect(sock, SIGNAL(hostFound()), this, SLOT(slotfound()));
sock->connectToHost( host,port );


if (sock->state() == QSocket::Connected)
{
cerr << "State == connected"<< endl;
}
else if (sock->state() ==QSocket::Idle)
{
cerr << "State == idle "<< endl;
}
else if (sock->state() == QSocket::HostLookup)
{
cerr << "State == HostLookup"<< endl;
}
else if (sock->state() == QSocket::Closing )
{
cerr << "State == Closing "<< endl;
}
else if ( sock->state() == QSocket::Connecting)
{
cerr << "State == connecting" << endl;
}

QTextStream os(sock);
os << "USER hggh\n";
os << "stat"<< "\n";

while ( sock->canReadLine() ) {
cerr << sock->readLine() << endl;
}
cerr << "KHylafax: closing socket"<< endl;
sock->close();
}


void KHylafax::slotconnected()
{
cerr << "KHylafax: Connected to server"<< endl;
}

void KHylafax::sloterror(int error)
{
cerr << "ERROR: " << error << " Connect error!"<< endl;
}

void KHylafax::slotfound()
{
cerr << "Host found..."<< endl;
}



Wenn ich jetzt das programm startekommt das auf die cerr std out:
KHylafax: In slotupdate-Funktion am Anfang....
Host found...
State == connecting
KHylafax: closing socket

Das heißt er verbindet gar nicht richtig, was ist falsch ?
Habe eigendlich alles so gemacht, wie es im QT-Manual steht...


Gruss
Jonas

anda_skoa
21-12-2002, 21:54
Der QSocket ist nach dem connectToHost im connecting Zustand.

Sobald er connected ist, emittet er das Signal connected.

Dazu mußt du aber in die Eventloop zurückkehren, es ist asynchron.

Dein Code muß also geteilt werden.

Erst der Connect.
Dann im slotconnected das senden.

Dann in einem Slot, der mit readRead verbunden ist, machst du die Prüfung auf canReadLine und liest dann.

Alle Signals von QSocket werden über die Eventloop der QApplication verteilt.
D.h., dass sie erst abgearbeitet werden, wenn die eventloop "dran kommt".

Ich hoffe, ich hab das halbwegs verständlich rüber gebracht.

Ciao,
_

jonasge
22-12-2002, 10:01
Hi !

Danke für deine Erklärung, habe jetzt nochmal in der Qt-Docu geschaut, besonders das eine BSp, und dort war es auch so, in verschiedenen Funktionen gemacht, ich dachte, ich kann das in einer machen...


Danke für deinen Tipp..


Gruss
Jonas