• 大小: 9KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-07-03
  • 语言: 其他
  • 标签: tcp  

资源简介

计网——消息封/解装仿真系统的设计与实现,具体包括物理层bit流,数据链路层帧格式,网络层IP包,传输层报文和应用层数据,锻炼学生数据分析和编程能力,能够开发实现协议封装和解封仿真软件(系统)。

资源截图

代码片段和文件信息

#include “client.h“
#include “ui_client.h“
#include 
#include 
#include 

#define BUF_SIZE 1024*2
using namespace std;

const QString tcp_head=“tcp“;
const QString ip_head=“ip“;
const QString link_head=“link“;

Client::Client(QWidget *parent) :
    QMainWindow(parent)
    ui(new Ui::Client)
{
    ui->setupUi(this);
    tcpSocket = new QTcpSocket(this);
    ui->btn_send->setEnabled(false);
    ui->le_message->setEnabled(false);

    //如果连接成功
    connect(tcpSocket &QTcpSocket::connected
            [=]()
            {
                ui->btn_connect->setEnabled(false);
                QMessageBox::information(this“connect““和服务器连接成功!!“);
                ui->btn_send->setEnabled(true);
                ui->le_message->setEnabled(true);

            }
            );
    //当有数据可接收时
    connect(tcpSocket &QTcpSocket::readyRead
            [=]()
            {

                QByteArray buf = tcpSocket->readAll();
                ui->textEdit->append((QString(buf).section(“:“44)));

             }
            );

}

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



void Client::on_btn_connect_clicked()
{
        QString ip = ui->le_ip->text();
        qint16 port = ui->le_port->text().toInt();
        if(ip.isEmpty() == true || port == 0)
        {
            QMessageBox::warning(this “警告“ “ip或端口不能为空“);
            return;
        }

        tcpSocket->abort(); //取消已有的连接
        //连接服务器
        tcpSocket->connectToHost(QHostAddress(ip) port);

}


void Client::on_btn_send_clicked()
{
    QString str;
    str = ui->le_message->text();
    ui->textEdit->append(str);
    QString buf=QString(“:%1:%2:%3:%4“).arg(tcp_head).arg(ip_head).arg(link_head).arg(str);
    tcpSocket->write(buf.toUtf8().data());
    tcpSocket->waitForBytesWritten(); //等待数据发送完毕
    ui->le_message->setText(““);

}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-12-09 20:36  TCP\
     文件       12288  2016-12-07 00:04  TCP\.client.cpp.swp
     文件        1941  2016-12-02 05:53  TCP\client.cpp
     文件         409  2016-12-01 17:27  TCP\client.h
     文件        4923  2016-12-04 13:55  TCP\client.ui
     文件         212  2016-11-30 20:55  TCP\main.cpp
     文件        1980  2016-12-09 20:36  TCP\server.cpp
     文件         418  2016-12-01 20:03  TCP\server.h
     文件        2990  2016-12-01 16:10  TCP\server.ui
     文件         417  2016-12-04 13:32  TCP\TCP.pro
     文件       23841  2016-12-04 13:55  TCP\TCP.pro.user

评论

共有 条评论