I am trying to create a simple nntp class to try learning some different
things with the QT library. I figure this would cover sockets, qt
slot/signals, and avoid UI stuff. However, when I try to use test
program I created here it does not seem to resolve the host, or when I
use the ipaddress it doesn't seem to stay in the connecting state. If
anyone could give me a pointer or two just to get an idea of what I
might be doing wrong here, that'd be great.
Makefile:
TARGET = nntptest
OBJS = qtrfc977.o moc_qtrfc977.o
MOCS = moc_qtrfc977.cc
CPP = g++
MOC = moc
CPPFLAGS = -Wall
LIBS = -L$(QTDIR)/lib -lqt
INCS = -I$(QTDIR)/include
all: $(MOCS) $(OBJS) $(TARGET)
.cc.o:
$(CPP) $(CPPFLAGS) $(INCS) -c $<
moc_qtrfc977.cc: qtrfc977.h
$(MOC) qtrfc977.h -o moc_qtrfc977.cc
qtrfc977.o: qtrfc977.cc qtrfc977.h
$(CPP) $(CPPFLAGS) $(INCS) -c qtrfc977.cc
# executable
nntptest: $(OBJS) nntptest.o
$(CPP) $(CPPFLAGS) $(INCS) $(LIBS) $(OBJS) nntptest.o -o nntptest
.PHONY: clean
clean:
rm -f $(TARGET) $(TARGET).o $(MOCS) $(OBJS) core
nntptest.cc:
#include <iostream.h>
#include <unistd.h>
#include "qtrfc977.h"
int main (int argc, char **argv)
{
QString host;
qt_rfc977 nntp;
if (argc != 2) {
cerr << "usage: nntptest <host>" << endl;
exit (1);
}
host = argv[1];
nntp.connectToHost (host, 119);
for (;;) {
switch (nntp.state ()) {
case QSocket::Idle:
cout << "nntp.state (): Idle\n";
exit (0);
break;
case QSocket::HostLookup:
cout << "nntp.state (): HostLookup\n";
sleep (1);
break;
case QSocket::Connecting:
cout << "nntp.state (): Connecting\n";
sleep (1);
break;
case QSocket::Connected:
cout << "nntp.state (): Connected\n";
nntp.close ();
break;
case QSocket::Closing:
cout << "nntp.state (): Closing\n";
break;
default:
cout << "nntp.state (): unknown\n";
}
}
#ifndef QT_RFC977_H
#define QT_RFC977_H
#define DEBUG_QT_RFC977 1
#include <qsocket.h>
class qt_rfc977 : public QSocket
{
private:
Q_OBJECT
public:
// constructor
qt_rfc977 (QObject *parent = 0, const char *name = 0);
// destructor
~qt_rfc977 (void);
// connect to default nntp port number (119) on specified host
virtual void connectToHost (const QString &host, Q_INT16 port = 119);
protected slots:
// hostFound () signal handler
virtual void rfc977hostFound (void);
// connected () signal handler
virtual void rfc977connected (void);
// connectionClosed () signal handler
virtual void rfc977connectionClosed (void);
// delayedCloseFinished () signal handler
virtual void rfc977delayedCloseFinished (void);
// readyRead () signal handler
virtual void rfc977readyRead (void);
// bytesWritten (int) signal handler
virtual void rfc977bytesWritten (int nbytes);
// error (int) signal handler
virtual void rfc977error (int err);
qtrfc977.cc:
#ifndef QT_RFC977_H
#include "qtrfc977.h"
#endif // QT_RFC977_H
// constructor
qt_rfc977::qt_rfc977 (QObject *parent, const char *name)
: QSocket (parent, name)
{
#if DEBUG_QT_RFC977 == 1
std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977
// connect signals
QObject::connect (this, SIGNAL (hostFound ()),
this, SLOT (rfc977hostFound ()));
QObject::connect (this, SIGNAL (connected ()),
this, SLOT (rfc977connected ()));
QObject::connect (this, SIGNAL (connectionClosed ()),
this, SLOT (rfc977connectionClosed ()));
QObject::connect (this, SIGNAL (delayedCloseFinished ()),
this, SLOT (rfc977delayedCloseFinished ()));
QObject::connect (this, SIGNAL (readyRead ()),
this, SLOT (rfc977readyRead ()));
QObject::connect (this, SIGNAL (error (int)),
this, SLOT (rfc977error (int)));
qt_rfc977::~qt_rfc977 (void)
{
#if DEBUG_QT_RFC977 == 1
std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977
{
#if DEBUG_QT_RFC977 == 1
std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977
QSocket::connectToHost (host, port);
{
#if DEBUG_QT_RFC977 == 1
std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977
return;
{
#if DEBUG_QT_RFC977 == 1
std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977
return;
{
#if DEBUG_QT_RFC977 == 1
std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977
return;
{
#if DEBUG_QT_RFC977 == 1
std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977
return;
{
#if DEBUG_QT_RFC977 == 1
std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977
return;
{
#if DEBUG_QT_RFC977 == 1
std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977
return;
{
#if DEBUG_QT_RFC977 == 1
std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977
return;