QT socket 服务器端代码





#ifndef TCPCLIENT_H
#define TCPCLIENT_H
 
  
#include 
 
  
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 
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 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 
#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
 
  





 
 

你可能感兴趣的:(QT)