• 4732阅读
  • 3回复

一个简单的c/s聊天程序,为什么server可以向client发送信息,而client不能向server发送? [复制链接]

上一主题 下一主题
离线michaelscrip
 
只看楼主 倒序阅读 楼主  发表于: 2010-05-11
如题,用QTcpSocket和QTcpServer写的,我找了很久没发现问题出现在哪儿,请大家帮我看看。
代码如下。


这是头文件
#ifndef CHART
#define CHART
#include<QTcpSocket>
#include<QString>
#include<QTextEdit>
#include<QLineEdit>
#include<QPushButton>
#include<QWidget>
#include<QTcpServer>
#include<QHBoxLayout>
#include<QVBoxLayout>
#include<QMessageBox>
#include<QTextStream>
#include<QTextDocument>
class chart:public QWidget
{
    Q_OBJECT
    public:
        chart();
    private slots:
        void myconnect();
        void mydisconnect();
        void mysend();
                void myreceive();
                void myaccept();
                void myconnected();
                void hostFound();
    private:
                bool mode;
        QTcpSocket *client;
        QTcpServer *server;
        QWidget *win;
        QTextEdit *message;
        QTextEdit *history;
        QPushButton *sendBtn;
        QLineEdit *ipaddress;
        QLineEdit *port;
        QPushButton *connectBtn;
        QPushButton *disconnectBtn;
        QHBoxLayout *hlayout1;
        QHBoxLayout *hlayout2;
        QVBoxLayout *vlayout1;
        QVBoxLayout *vlayout2;
};


#endif


#include"chart.h"
chart::chart()
{
        mode=0;//一个标志位,基本没用到
    history = new QTextEdit;
    history->setReadOnly(1);
    message = new QTextEdit;
    ipaddress = new QLineEdit;
    port = new QLineEdit;
    connectBtn = new QPushButton;
    connectBtn->setText("connect");
    disconnectBtn = new QPushButton;
    disconnectBtn->setText("disconnect");
    sendBtn = new QPushButton;
    sendBtn->setText("send");

    vlayout1 = new QVBoxLayout;
    vlayout1->addWidget(history);
    vlayout1->addWidget(message);
    vlayout2 = new QVBoxLayout;
    vlayout2->addWidget(ipaddress);
        //vlayout2->addWidget(port);
    hlayout1 = new QHBoxLayout;
    hlayout1->addWidget(connectBtn);
    hlayout1->addWidget(disconnectBtn);
    vlayout2->addLayout(hlayout1);
    vlayout2->addWidget(sendBtn);
    hlayout2 = new QHBoxLayout;
    hlayout2->addLayout(vlayout1);
    hlayout2->addLayout(vlayout2);
    setLayout(hlayout2);

        client = new QTcpSocket(this);
        server = new QTcpServer(this);
        server->listen(QHostAddress::Any,9999);
        connect(server,SIGNAL(newConnection()),this,SLOT(myaccept()));
        connect(client,SIGNAL(readyRead()),this,SLOT(myreceive()));
        connect(connectBtn,SIGNAL(clicked()),this,SLOT(myconnect()));
        connect(disconnectBtn,SIGNAL(clicked()),this,SLOT(mydisconnect()));
        connect(client,SIGNAL(connected()),this,SLOT(myconnected()));
        //connect(client,SIGNAL(hostFound()),this,SLOT(hostFound()));
        connect(sendBtn,SIGNAL(clicked()),this,SLOT(mysend()));
        connectBtn->setEnabled(1);
        disconnectBtn->setEnabled(0);
        sendBtn->setEnabled(0);
}
void chart::hostFound()//没用的函数
{
    QMessageBox::information(this,"host found","host found!");
}

void chart::myaccept()//server接受连接请求
{
    QMessageBox::information(this,"connected","connected!");
    mode=0;//i'm the host
    client=server->nextPendingConnection();
    connectBtn->setEnabled(0);
    disconnectBtn->setEnabled(1);
    sendBtn->setEnabled(1);
}


void chart::myconnected()//client连接完成
{
    QMessageBox::information(this,"connected","connected!");
    connectBtn->setEnabled(0);
    disconnectBtn->setEnabled(1);
    sendBtn->setEnabled(1);
}

void chart::myconnect()//client请求连接
{
    mode=1;//i'm the client
    //QMessageBox::information(this,"connecting","i'm trying to connect to the host,plz wait...");
    client->connectToHost(ipaddress->text(),9999);
}

void chart::mydisconnect()//断开client连接
{
    client->disconnectFromHost();

    connectBtn->setEnabled(1);
    disconnectBtn->setEnabled(0);
    sendBtn->setEnabled(0);
}

void chart::mysend()//这个函数就是发送信息的
{
    QString text=message->document()->toPlainText()+tr("\n");
    int size=client->write(text.toAscii().data());//这里是看网上说的为了防止数据没有发送完而进行的处理,其实直接write也可以。
    while(size<text.size())
    {
        size+=client->write(text.right(text.size()-size).toAscii().data());
    }
    history->insertPlainText(tr("me: ")+text);
    message->clear();
}

void chart::myreceive()//接收信息
{
    QMessageBox::information(this,"received","received");
        history->insertPlainText(tr("he/she: ")+client->readAll());
}
[ 此帖被michaelscrip在2010-05-11 12:51重新编辑 ]
离线michaelscrip
只看该作者 1楼 发表于: 2010-05-11
我看到qt的例子里面也是一次连接就可以通信了,但为什么这里一次连接只能单向通信呢?
不能理解。。。
离线michaelscrip
只看该作者 2楼 发表于: 2010-05-11
有人知道不?
离线michaelscrip
只看该作者 3楼 发表于: 2010-05-12
顶起
快速回复
限100 字节
 
上一个 下一个