第10章:TcpClient

1,TCP客户端

2,源码:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    status = false;
    port   = 5555;

    ui->portlineEdit->setText(QString::number(port));
    ui->addrlineEdit->setText(tr("127.0.0.1"));
    ui->namelineEdit->setText(tr("Joly"));

    serverIP  = new QHostAddress();
    tcpSocket = new QTcpSocket(this);

    ui->sendButton->setEnabled(false);
}

MainWindow::~MainWindow()
{
    delete ui;
}



//发送按键
void MainWindow::on_sendButton_clicked()
{
    if (ui->lineEdit->text() == "")
    {
        return;
    }

    QString msg = userName + ":" + ui->lineEdit->text();
    tcpSocket->write(msg.toLatin1(), msg.length());
    ui->lineEdit->clear();
}


//进入按键
void MainWindow::on_enterButton_clicked()
{
    if (!status)
    {
        QString ip = ui->addrlineEdit->text();
        if (!serverIP->setAddress(ip))
        {
            QMessageBox::information(this, tr("error"), tr("server ip address error"));

            return;
        }

        if (ui->namelineEdit->text() == "")
        {
            QMessageBox::information(this, tr("error"), tr("User name error!"));

            return;
        }

        userName  = ui->namelineEdit->text();

        connect(tcpSocket, SIGNAL(connected()), this, SLOT(slotConnected()));
        connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(slotDisconnected()));
        connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(dataReceived()));

        tcpSocket->connectToHost(*serverIP, port);

        status = true;
    }
    else
    {
        int length = 0;
        QString msg = userName + tr(":Leave Chat Room");
        if ((length = tcpSocket->write(msg.toLatin1(), msg.length())) != msg.length())
        {
            return;
        }

        disconnect(tcpSocket, SIGNAL(connected()), this, SLOT(slotConnected()));
        disconnect(tcpSocket, SIGNAL(disconnected()), this, SLOT(slotDisconnected()));
        disconnect(tcpSocket, SIGNAL(readyRead()), this, SLOT(dataReceived()));
        tcpSocket->disconnectFromHost();

        status = false;
    }
}



void MainWindow::slotConnected()
{
    ui->sendButton->setEnabled(true);

    ui->enterButton->setText(tr("离开"));

    int length = 0;
    QString msg = userName + tr(":Enter Chat Room");
    if ((length = tcpSocket->write(msg.toLatin1(),  msg.length())) != msg.length())
    {
        return;
    }
}


void MainWindow::slotDisconnected()
{
    ui->sendButton->setEnabled(false);
    ui->enterButton->setText(tr("进入聊天室"));
}



void MainWindow::dataReceived()
{
    QByteArray datagram;

    datagram.resize(tcpSocket->bytesAvailable());
    tcpSocket->read(datagram.data(), datagram.size());

    QString msg = datagram.data();

    ui->listWidget->addItem(msg.left(datagram.size()));
}

3,效果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值