• 10094阅读
  • 2回复

QTcpSocket與QThread問題? [复制链接]

上一主题 下一主题
离线sdaaron
 

只看楼主 倒序阅读 楼主  发表于: 2009-09-26
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
請教各位前輩,小弟初學QT,目前正在做一個chat(可以接受多個Client連接),使用方法是當有Client連入時,建立一個新QTcpSocket實例,
然後QTcpSocket是在Thread裡面建立,但日前遇到一問題困擾許久,故上來求救一下,下面是簡要源碼...

我的問題在fortuneserver.cpp裡面,connect(tcpSocket,SIGNAL(readyRead()), this, SLOT(newDataSlot()));
當Client送訊息來時,好像無法到newDataSlot()....可請各位前輩幫我指導一下問題出在哪裡?感謝囉!

dialog.h
======================
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include "fortunethread.h"
#include <iostream.h>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>

QT_BEGIN_NAMESPACE
class QLabel;
class QPushButton;
QT_END_NAMESPACE
class FortuneThread;
class Dialog : public QDialog
{
    Q_OBJECT
public:
    Dialog(QWidget *parent = 0);
    QTcpServer *server;
public slots:
    void newConnectionSlot();
    void newDataSlot();
private:
    QLabel *statusLabel;
    QPushButton *quitButton;
    quint16 port;                //add by chris 20090916
    FortuneThread* tmpClient;
};
#endif
======================

dialog.cpp
======================
#include <QtGui>
#include <QtNetwork>
#include <stdlib.h>
#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{         .
          .
         略
          .
          tmpClient = new FortuneThread(this);
          server = new QTcpServer(this);
          port=22223;
          server->listen(QHostAddress::Any, port);            
          connect(server, SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));
}

void Dialog::newConnectionSlot()
{
    FortuneThread *thread = new FortuneThread(this);
    thread->start();
}
======================

fortuneserver.h
======================
#ifndef FORTUNETHREAD_H
#define FORTUNETHREAD_H
#include <QThread>
#include <QTcpSocket>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include <iostream.h>

class FortuneThread : public QThread
{
    Q_OBJECT
public:
    FortuneThread(QObject *parent);
    void run();
    QTcpSocket *tcpSocket;
public slots:
    void newDataSlot();
signals:
    void error(QTcpSocket::SocketError socketError);
private:
    int socketDescriptor;
    QString text;
};
#endif
======================


fortuneserver.cpp
======================
#include "fortunethread.h"
#include <QtNetwork>
#include "dialog.h"

FortuneThread::FortuneThread(QObject *parent)
    : QThread(parent)
{
}

void FortuneThread::run()
{
    tcpSocket = new QTcpSocket;
    connect(tcpSocket,SIGNAL(readyRead()), this, SLOT(newDataSlot()));
}

void FortuneThread::newDataSlot()
{
    cout<<"FortuneThread::newDataSlot"<<endl;
}
======================
离线dbzhang800

只看该作者 1楼 发表于: 2009-09-26
void FortuneThread::run()
{
    tcpSocket = new QTcpSocket;
    connect(tcpSocket,SIGNAL(readyRead()), this, SLOT(newDataSlot()));
}

你这个socket如何和客户端socket连接的??
离线sdaaron

只看该作者 2楼 发表于: 2009-09-27
引用第1楼dbzhang800于2009-09-26 23:35发表的  :
void FortuneThread::run()
{
    tcpSocket = new QTcpSocket;
    connect(tcpSocket,SIGNAL(readyRead()), this, SLOT(newDataSlot()));
}
.......


在dialog.cpp裡面
server = new QTcpServer(this);
port=22223;
server->listen(QHostAddress::Any, port);            
connect(server, SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));

如果有client連接進來,就會到newConnectionSlot()裡面...
void Dialog::newConnectionSlot()
{
    FortuneThread *thread = new FortuneThread(this);
    thread->start();
}
快速回复
限100 字节
 
上一个 下一个