免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6185 | 回复: 3
打印 上一主题 下一主题

向各位大狭请教一个QT的网络编程问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-19 11:06 |只看该作者 |倒序浏览
我用Qt做了一个网络的client程序,可以和windows系统下的server程序进行连接,并能给server端发数据,可是当server端给我发数据的时候,我的程序 中检测网络数据到来的信号readyRead()根本就不触发,也就无法读数据了,不知问题出在哪,请各位大狭多多指教,不胜感激!!接收数据主要的程序流程如下:
创建Qsocket-->连接消息和槽connect(socket,SIGNAL(readyRead),this,SLOT(readserver))
在readserver中将发过来的数据追加到QTextView  *infotext控件上
infotext->append(socket->readLine());   
可以发但是不能收

论坛徽章:
0
2 [报告]
发表于 2006-06-03 16:28 |只看该作者
emit readyRead

论坛徽章:
0
3 [报告]
发表于 2009-03-18 17:23 |只看该作者

回复 #1 askfalano 的帖子

我也遇到了和你同样的问题,请问你现在解决了吗?大家一起讨论吧
*****************************************
Pop.h
------------------------------------------
#ifndef POP_H
#define POP_H
#include <qobject.h>
#include <qstring.h>
class QSocket;
class QTextStream;
class QDns;
class Pop : public QObject
{
    Q_OBJECT
public:
    Pop( const QString &, const QString &, const QString & );
    ~Pop();
signals:
    void status( const QString & );

private slots:
    void readyRead();
    void connected();
    void deleteMe();
    void dnsLookupHelper();
private:
    enum State {
USER,
PASS,
LIST,
RETR,
QUIT,
CLOSE,
    };
    QString from;
    QString username;
    QString password;
    QSocket *socket;
    QTextStream * t;
    int state;
    QString response;
    QDns * mxLookup;
};
#endif
------------------------------------------------
Pop.cpp
********************************************
#include "op.h"
#include <qtextstream.h>
#include <qsocket.h>
#include <qdns.h>
#include <qtimer.h>
#include <qapplication.h>
#include <qmessagebox.h>
#include <iostream.h>
using namespace std;
Pop:op( const QString &from ,const QString &username, const QString &password )
{
socket = new QSocket( this );
connect ( socket, SIGNAL(readyRead() ),
       this, SLOT(readyRead() ) );
connect ( socket, SIGNAL( connected() ),
       this, SLOT( connected() ) );

mxLookup = new QDns( from.mid( from.find( '@' )+1 ), QDns::Mx );
connect( mxLookup, SIGNAL(resultsReady()),
      this, SLOT(dnsLookupHelper()) );
  this->from = from;
  this->username = username;
  this->password = password;
   state = USER;
  }

Pop::~Pop()
{
  delete t;
  delete socket;
}
void Pop::dnsLookupHelper()
{
    QValueList<QDns::MailServer> s = mxLookup->mailServers();
    if ( s.isEmpty() && mxLookup->isWorking() )
return;
   
     std::cout << tr( "Connecting to %1" ).arg( s.first().name ) << endl;

    socket->connectToHost( s.first().name, 110 );   
    t = new QTextStream( socket );
   }
void Pop::connected()
{
    QMessageBox::information( qApp->activeWindow(),
         tr( "OK" ),
         tr( "connected" ));
  std::cout << tr( "Connected to %1" ).arg( socket->peerName() ) << endl;
}
void Pop::readyRead()      //                              {
  QMessageBox::information( qApp->activeWindow(),//for debug
         tr( "OK" ),
         tr( "ready" ));
      
    // Pop3 is line-oriented
    if ( !socket->canReadLine() )
return;

    QString responseLine;
    do {
responseLine = socket->readLine();
//std::cout << responseLine << endl;
if ((responseLine.find("+OK",0,true)!=0)&&(responseLine.find("-ERR",0,true)!=0) )
response += responseLine;
    } while( socket->canReadLine() && responseLine[3] != ' ' );
    responseLine.truncate( 3 );
//std::cout << response << endl;
  // email.append(response);
    if ( state == USER ) {
*t << "USER:<" << username << ">\r\n";
//responseLine = socket->readLine();
     //std::cout << *t;
state = PASS;
    } else if ( state == PASS) {
*t << "ASS:<" << password << ">\r\n";
//responseLine = socket->readLine();
     //std::cout << responseLine << endl;
state = LIST;
    }  else if ( state == LIST) {
*t << "LIST\r\n";
//responseLine = socket->readLine();
     //std::cout << responseLine << endl;
state = RETR;
    }else if ( state == RETR) {
*t << "RETR 5\r\n";
//responseLine = socket->readLine();
     //std::cout << responseLine << endl;
state = QUIT;
    } else if ( state == QUIT) {
*t << "QUIT\r\n";
//responseLine = socket->readLine();
     //std::cout << responseLine << endl;
state = CLOSE;
emit status( tr( "Message receive" ) );
    } else if ( state == CLOSE ) {
// we ignore it
    } else {
// something broke.
QMessageBox::warning( qApp->activeWindow(),
         tr( "Qt Mail Example" ),
         tr( "Unexpected reply from POP3 server:\n\n" ) +
         response );
state = CLOSE;
    }
response = "";
    if ( state == CLOSE) {
QTimer::singleShot( 0, this, SLOT(deleteMe()) );
      }
   }
void Pop::deleteMe()
{
     delete this;
}

论坛徽章:
0
4 [报告]
发表于 2009-03-25 12:52 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP