2012-10-26 34 views
0

我想在qt framework中编写一个控制台聊天程序。我在发送消息时遇到问题。使用QTcpSocket和QTcpServer进行控制台聊天

客户端向服务器发送消息,但服务器在客户端程序关闭之前不会接收消息。当客户端关闭时,服务器显示所有消息。我不想那样。我希望服务器在我发送消息时获取消息到它。

我写了下面的代码。你会看到我想要做的,如果你看看客户端的主要功能。

/* 

    Created BY : 
    Creation DATE : 26/10/2012 

    Client interface 

    */ 

    #ifndef CLIENT_H 
    #define CLIENT_H 

    #include <QtNetwork> 
    #include <QObject> 
    #include <QtNetwork/QTcpSocket> 

    namespace NetworkArdic 
    { 

    class Client : public QObject 
    { 
     Q_OBJECT 
     public: 

      Client(QObject * obj = 0,QString add="localhost", quint16 port = 4000); 


      void SendData(QString data); 

      virtual ~Client(); 

     private slots: 


      void ReadData(); 

      void connected(); 

     private: 

      QTcpSocket *socket; 
    }; 

    } 

    #endif 


/* 

    Created BY : 
    Creation DATE : 26/10/2012 

    Client source file 

    */ 

    #include "Client.h" 
    #include <QHostAddress> 
    #include <iostream> 
    using namespace std; 

    namespace NetworkArdic{ 

    Client::Client(QObject * obj, QString add, quint16 port) : QObject(obj) 
    { 

     socket = new QTcpSocket(this); 


     connect(socket, SIGNAL(readyRead()), this, SLOT(ReadData())); 
     connect(socket, SIGNAL(connected()), this, SLOT(connected())); 

     socket->connectToHost(QHostAddress(add), port); 
    } 

    Client::~Client(){ 
     socket->close(); 
     delete socket; 
    } 



    void Client::SendData(QString data) 
    { 
     if(!data.isEmpty()) 
     { 
      socket->write(QString(data + "\n").toUtf8()); 
     } 
    } 

    void Client::ReadData() 
    { 
     while(socket->canReadLine()) 
     { 

      QString line = QString::fromUtf8(socket->readLine()).trimmed(); 
      qDebug() << line; 
     } 
    } 

    void Client::connected() 
    { 
     socket->write(QString("Client : Server connection has been made (: \n").toUtf8()); 
    } 

    } 

    int main(int argc, char *argv[]) 
    { 
     QCoreApplication a(argc, argv); 

     Client cli(0,"127.0.0.1",4000); 

     string line; 
     while(line!="exit"){ 
      cout << "Message : "; 
      cin >> line; 
      cli.SendData(QString(line.c_str())); 
     } 

     return a.exec(); 
    } 

    /* 

    Created BY : 
    Creation DATE : 26/10/2012 

    Server interface 


    */ 

    #ifndef SERVER_H 
    #define SERVER_H 

    #include <QtNetwork> 
    #include <QObject> 
    #include <QtNetwork/QTcpServer> 
    #include <QtNetwork/QTcpSocket> 

    namespace NetworkArdic 
    { 

     class Server: public QTcpServer 
     { 

      Q_OBJECT 
      public: 

       Server(QObject * parent = 0 , quint16 port = 4000); 
       virtual ~Server(); 

      private slots: 

       void acceptConnection(); 
       void startRead(); 
       void disconnected(); 

      private: 

       QTcpSocket * client; 

     }; 

    } 

    #endif // SERVER_H 



    /* 

    Created BY : 
    Creation DATE : 26/10/2012 

    Server source file 

    */ 


    #include "Server.h" 
    #include <iostream> 
    using namespace std; 

    namespace NetworkArdic{ 

    Server::Server(QObject* parent , quint16 port): QTcpServer(parent) 
    { 
     connect(this, SIGNAL(newConnection()),this, SLOT(acceptConnection())); 

     listen(QHostAddress::Any, port); 
    } 

    Server::~Server() 
    { 
     delete client; 
     close(); 
    } 

    void Server::acceptConnection() 
    { 
     client = nextPendingConnection(); 

     connect(client, SIGNAL(readyRead()), this, SLOT(startRead())); 
     connect(client, SIGNAL(disconnected()), this, SLOT(disconnected())); 

     qDebug() << "New client from:" << client->peerAddress().toString(); 
    } 

    void Server::startRead() 
    { 
     while(client->canReadLine()) 
     { 
      QString line = QString::fromUtf8(client->readLine()).trimmed(); 
      qDebug() << "Client :" << line; 

      client->write(QString("Server : I've taken your message (:\n").toUtf8()); 
     } 

    } 

    void Server::disconnected() 
    { 

     qDebug() << "Client disconnected:" << client->peerAddress().toString(); 

     client->write(QString("Server : I wish you didn't leave):\n").toUtf8()); 

    } 

    } 

回答

0

您使用下面的代码来读取套接字数据。

void Server::startRead() 
{ 
    while(client->canReadLine()) 
    { 
     QString line = QString::fromUtf8(client->readLine()).trimmed(); 
     qDebug() << "Client :" << line; 

     client->write(QString("Server : I've taken your message (:\n").toUtf8()); 
    } 

} 

我建议从我已经测试了下面的代码采用工程

while (tcp_server->hasPendingConnections()) { 
     client = tcp_server->nextPendingConnection(); 
     client->waitForReadyRead(100); 

     QByteArray byteArray = client->readAll(); 
     QString s_data = byteArray.data(); 

     // Process s_data 

     client->close(); 
     client->abort(); 
    }