C++ 使用QTcpSocket和QTcpServer的控制台聊天
我想在qt框架中编写一个控制台聊天程序。我在发送消息时遇到问题 客户端向服务器发送消息,但在客户端程序关闭之前,服务器不会接收消息。当客户端关闭时,服务器会显示所有消息。我不希望这样。我希望服务器在我向其发送消息时获取我的消息 我写了下面的代码。若你们看一下客户端的主要功能,你们会看到我想要做什么C++ 使用QTcpSocket和QTcpServer的控制台聊天,c++,qt,qtcpsocket,qtnetwork,qtcpserver,C++,Qt,Qtcpsocket,Qtnetwork,Qtcpserver,我想在qt框架中编写一个控制台聊天程序。我在发送消息时遇到问题 客户端向服务器发送消息,但在客户端程序关闭之前,服务器不会接收消息。当客户端关闭时,服务器会显示所有消息。我不希望这样。我希望服务器在我向其发送消息时获取我的消息 我写了下面的代码。若你们看一下客户端的主要功能,你们会看到我想要做什么 /* Created BY : Creation DATE : 26/10/2012 Client interface */ #ifndef
/*
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());
}
}
/*
创建人:
创建日期:2012年10月26日
客户端接口
*/
#ifndef客户
#定义客户端
#包括
#包括
#包括
名称空间网络
{
类客户端:公共QObject
{
Q_对象
公众:
客户端(QObject*obj=0,QString add=“localhost”,quint16端口=4000);
void SendData(QString数据);
虚拟客户机();
专用插槽:
void ReadData();
空连接();
私人:
QTcpSocket*插座;
};
}
#恩迪夫
/*
创建人:
创建日期:2012年10月26日
客户端源文件
*/
#包括“Client.h”
#包括
#包括
使用名称空间std;
名称空间网络{
客户端::客户端(QObject*obj,QString添加,quint16端口):QObject(obj)
{
插座=新QTC插座(本);
连接(套接字、信号(readyRead())、此、插槽(ReadData());
连接(插座,信号(已连接()),此,插槽(已连接());
套接字->连接到主机(QHostAddress(add),端口);
}
客户::~Client(){
套接字->关闭();
删除套接字;
}
无效客户端::SendData(QString数据)
{
如果(!data.isEmpty())
{
socket->write(QString(data+“\n”).toUtf8();
}
}
void客户端::ReadData()
{
而(socket->canReadLine())
{
QString line=QString::fromUtf8(套接字->读取行()).trimmed();
qDebug()写入(QString(“客户端:服务器连接已建立(:\n”).toUtf8());
}
}
int main(int argc,char*argv[])
{
qcorea应用程序(argc、argv);
客户端cli(0,“127.0.0.1”,4000);
弦线;
while(行!=“退出”){
曲线;
cli.SendData(QString(line.c_str());
}
返回a.exec();
}
/*
创建人:
创建日期:2012年10月26日
服务器接口
*/
#ifndef服务器
#定义服务器
#包括
#包括
#包括
#包括
名称空间网络
{
类服务器:公共QTcpServer
{
Q_对象
公众:
服务器(QObject*parent=0,quint16端口=4000);
虚拟服务器();
专用插槽:
void acceptConnection();
void startRead();
无效断开连接();
私人:
QTcpSocket*客户端;
};
}
#endif//SERVER\u H
/*
创建人:
创建日期:2012年10月26日
服务器源文件
*/
#包括“Server.h”
#包括
使用名称空间std;
名称空间网络{
服务器::服务器(QObject*parent,quint16端口):QTcpServer(parent)
{
连接(此,信号(newConnection()),此,插槽(acceptConnection());
侦听(QHostAddress::任意,端口);
}
服务器::~Server()
{
删除客户端;
close();
}
无效服务器::acceptConnection()
{
client=nextPendingConnection();
连接(客户端,信号(readyRead()),此,插槽(startRead());
连接(客户端,信号(断开()),此,插槽(断开());
qDebug()可以读取行()
{
QString line=QString::fromUtf8(客户端->读取行()).trimmed();
qDebug()在写入数据后尝试使用socket->flush()
在写入数据后,尝试使用socket->flush()
您使用下面的代码读取套接字数据
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::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());
}
}