QT socket 服务器端代码





#ifndef TCPCLIENT_H
#define TCPCLIENT_H
 
#include <QTcpSocket>
 
namespace QTTCP
{
 
class TcpClient : public QTcpSocket
{
    Q_OBJECT
public:
    explicit TcpClient(QObject *parent = 0);
    
signals:
    void dataRevS(QString, int, int);
 
    void disConnectS(int);
public slots:
    void dataRev();
 
    void disConnect();
};
}//namespace QTTCP
 
#endif // TCPCLIENT_H


#include "tcpclient.h"
#include <memory>
namespace QTTCP
{
TcpClient::TcpClient(QObject *parent) :
    QTcpSocket(parent)
{
    connect(this, SIGNAL(readyRead()), this, SLOT(dataRev()));
    connect(this, SIGNAL(disconnected()), this, SLOT(disConnect()));
}
void TcpClient::dataRev()
{
    while(bytesAvailable()>0)
    {
        int size = bytesAvailable();
        //std::auto_ptr<char > p(new char(size));
        char buffer[1024];
        read(buffer, size);
        QString str = buffer;
        int n = str.size();
        emit dataRevS(str, size, this->socketDescriptor());
    }
}
void TcpClient::disConnect()
{
    int x = this->socketDescriptor();
    emit disConnectS(x);
}
}//namespace QTTCP

#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QTcpServer>
#include "tcpclient.h"
namespace QTTCP
{
class TcpServer : public QTcpServer
{
    Q_OBJECT
public:
    explicit TcpServer(int port, QObject *parent = 0);
    
signals:
    void updateUi(QString, int);
    void connectedS(QString);
    void disconnectS(QString);
public slots:
    void updateClient(QString, int, int);
    void disConnect(int);
    void send(QString, QString);
protected:
    void incomingConnection(int socketDescriptor );
private:
    QMap<int, TcpClient *> m_pClientMap;
};
}//namespace QTTCP
#endif // TCPSERVER_H


#include "tcpserver.h"
namespace QTTCP
{
TcpServer::TcpServer(int port, QObject *parent) :
    QTcpServer(parent)
{
    listen(QHostAddress::Any, port);
}
void TcpServer::incomingConnection(int socketDescriptor )
{
    TcpClient *client = new TcpClient();
    connect(client, SIGNAL(dataRevS(QString,int, int)), this, SLOT(updateClient(QString,int,int)));
    connect(client, SIGNAL(disConnectS(int)), this, SLOT(disConnect(int)));
    client->setSocketDescriptor(socketDescriptor);
    m_pClientMap.insert(socketDescriptor, client);
    QString str = client->peerAddress().toString() + ":" + "连接成功";
    emit connectedS(str);
}
void TcpServer::updateClient(QString msg, int size, int des)
{
    QString allmsg;
    allmsg = m_pClientMap[des]->peerAddress().toString();
    allmsg.push_back("\n");
    allmsg.push_back(msg);
    emit updateUi(allmsg, size);
}
void TcpServer::disConnect(int des)
{
    for(auto it = m_pClientMap.begin(); it != m_pClientMap.end(); ++it)
    {
        if((*it)->socketDescriptor() == des)
        {
            QString str = (*it)->peerAddress().toString();
            str += "\n";
            str += "断开连接";
            emit disconnectS(str);
            m_pClientMap.erase(it);
            break;
        }
    }
}
void TcpServer::send(QString ip, QString msg)
{
    for(auto it= m_pClientMap.begin(); it != m_pClientMap.end(); ++it)
    {
        if((*it)->peerAddress().toString() == ip)
        {
            (*it)->write(msg.toLatin1(), msg.size());
            break;
        }
    }
}
}//namespace QTTCP






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值