• 大小: 18KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-28
  • 语言: 其他
  • 标签: DT  TCP  项目  QT  

资源简介

DT qt项目代码,基于tcp实现

资源截图

代码片段和文件信息

#include “ClientDemo.h“
#include 
#include 
#include 

ClientDemo::ClientDemo(Qobject* parent) : Qobject(parent) m_handler(NULL)
{
    connect(&m_client SIGNAL(connected()) this SLOT(onConnected()));
    connect(&m_client SIGNAL(disconnected()) this SLOT(onDisconnected()));
    connect(&m_client SIGNAL(readyRead()) this SLOT(onDataReady()));
    connect(&m_client SIGNAL(bytesWritten(qint64)) this SLOT(onBytesWritten(qint64)));
}

void ClientDemo::onConnected()
{
    if( m_handler != NULL )
    {
        TextMessage conn(“CONN“ m_client.peerAddress().toString() + “:“ + QString::number(m_client.peerPort()));

        m_handler->handle(m_client conn);
    }
}

void ClientDemo::onDisconnected()
{
    m_assembler.reset();

    if( m_handler != NULL )
    {
        TextMessage dscn(“DSCN“ ““);

        m_handler->handle(m_client dscn);
    }
}

void ClientDemo::onDataReady()
{
    char buf[256] =  {0};
    int len = 0;

    while( (len = m_client.read(buf sizeof(buf))) > 0 )
    {  
        QSharedPointer ptm;

        m_assembler.prepare(buf len);

        while( (ptm = m_assembler.assemble()) != NULL )
        {
            if( m_handler != NULL )
            {
                m_handler->handle(m_client *ptm);
            }
        }
    }
}

void ClientDemo::onBytesWritten(qint64 bytes)
{
    (void)bytes;
}

bool ClientDemo::connectTo(QString ip int port)
{
    m_client.connectToHost(ip port);
    return m_client.waitForConnected();
}

qint64 ClientDemo::send(TextMessage& message)
{
    QByteArray ba = message.serialize();

    return m_client.write(ba.data() ba.length());
}

qint64 ClientDemo::available()
{
    return m_client.bytesAvailable();
}

void ClientDemo::close()
{
    m_client.close();
}

bool ClientDemo::isValid()
{
    return m_client.isValid();
}

void ClientDemo::setHandler(TxtMsgHandler* handler)
{
    m_handler = handler;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       1247  2019-06-25 15:22  ChatRoom\ChatRoom.pro

     文件      23825  2019-12-25 14:32  ChatRoom\ChatRoom.pro.user

     文件       2037  2019-12-25 14:27  ChatRoom\ClientDemo.cpp

     文件        732  2019-07-12 17:46  ChatRoom\ClientDemo.h

     文件        177  2019-06-24 11:05  ChatRoom\main.cpp

     文件       1385  2019-07-12 18:58  ChatRoom\MainWin.h

     文件       2157  2019-07-12 19:00  ChatRoom\MainWinSlot.cpp

     文件       1934  2019-07-12 19:05  ChatRoom\MainWinUI.cpp

     文件       3724  2019-06-24 11:57  ChatRoom\QLoginDialog.cpp

     文件        923  2019-06-24 11:57  ChatRoom\QLoginDialog.h

     文件       1288  2019-06-11 11:53  ChatRoom\TextMessage.cpp

     文件        460  2019-06-11 11:53  ChatRoom\TextMessage.h

     文件       2022  2019-06-11 11:58  ChatRoom\TxtMsgAssembler.cpp

     文件        669  2019-06-11 11:58  ChatRoom\TxtMsgAssembler.h

     文件        226  2019-06-11 14:00  ChatRoom\TxtMsgHandler.h

     文件        956  2019-06-25 16:15  ChatRoomServer\ChatRoomServer.pro

     文件      23873  2019-12-25 14:32  ChatRoomServer\ChatRoomServer.pro.user

     文件        298  2019-06-25 16:18  ChatRoomServer\main.cpp

     文件       2979  2019-12-25 14:28  ChatRoomServer\ServerDemo.cpp

     文件        709  2019-06-11 14:49  ChatRoomServer\ServerDemo.h

     文件       2314  2019-07-12 18:56  ChatRoomServer\ServerHandler.cpp

     文件        885  2019-07-12 18:51  ChatRoomServer\ServerHandler.h

     文件       1288  2019-06-11 11:53  ChatRoomServer\TextMessage.cpp

     文件        460  2019-06-11 11:53  ChatRoomServer\TextMessage.h

     文件       2022  2019-06-11 11:58  ChatRoomServer\TxtMsgAssembler.cpp

     文件        669  2019-06-11 11:58  ChatRoomServer\TxtMsgAssembler.h

     文件        226  2019-06-11 14:00  ChatRoomServer\TxtMsgHandler.h

     目录          0  2019-12-25 14:32  ChatRoom

     目录          0  2019-12-25 14:32  ChatRoomServer

----------- ---------  ---------- -----  ----

............此处省略2个文件信息

评论

共有 条评论