功能:
1.用户注册登录功能
2.群聊功能
3.私聊功能
客户端代码:
//tcpclient.h
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <QMainWindow>
#include <QtNetwork>
#include <QMessageBox>
//#include "userinterface.h"
namespace Ui {
class TcpClient;
}
class TcpClient : public QMainWindow
{
Q_OBJECT
public:
explicit TcpClient(QWidget *parent = 0);
~TcpClient();
protected:
void init();
void connectServer();
private slots:
void on_sendBtn_clicked();
void displayError(QAbstractSocket::SocketError);
void on_signBtn_clicked();
void readMessages();
private:
Ui::TcpClient *ui;
QTcpSocket *tcpSocket;
int readFlag;
//UserInterface *user;
};
#endif // TCPCLIENT_H
//tcpclient.cpp
#include "tcpclient.h"
#include "ui_tcpclient.h"
#include "userinterface.h"
#define ip "192.168.1.165"
//#define ip "127.0.0.1"
#define port 8000
TcpClient::TcpClient(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TcpClient)
{
readFlag=1;
ui->setupUi(this);
ui->passwardLineEdit->setEchoMode(QLineEdit::Password); //密码方式显示文本
init();
connectServer();
}
TcpClient::~TcpClient()
{
delete ui;
}
void TcpClient::init()
{
tcpSocket=new QTcpSocket(this);
connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),
this,SLOT(displayError(QAbstractSocket::SocketError))); //发生错误时执行displayError函数
}
void TcpClient::connectServer()
{
tcpSocket->abort(); //取消已有的连接
tcpSocket->connectToHost(ip,port);
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readMessages()));
}
void TcpClient::on_sendBtn_clicked()
{
QString userName=ui->userLineEdit->text();
QString passward=ui->passwardLineEdit->text();
if(userName=="" || passward=="")
QMessageBox::information(this,"警告","输入不能为空",QMessageBox::Ok);
QString bs="b";
QString data=bs+"#"+userName+"#"+passward;
tcpSocket->write(data.toLatin1());
}
void TcpClient::on_signBtn_clicked()
{
QString userName=ui->userLineEdit->text();
QString passward=ui->passwardLineEdit->text();
if(userName=="" || passward=="")
QMessageBox::information(this,"警告","输入不能为空",QMessageBox::Ok);
QString as="a";
QString data=as+"#"+userName+"#"+passward;
tcpSocket->write(data.toLatin1());
}
void TcpClient::displayError(QAbstractSocket::SocketError)
{
qDebug()<<tcpSocket->errorString(); //输出出错信息
}
void TcpClient::readMessages()
{
if(readFlag==0)
return;
QString data=tcpSocket->readAll();
QStringList list=data.split("#");
if(list[0]=="a" && list[2]=="true")
QMessageBox::information(this,"信息提示","注册成功!",QMessageBox::Ok);
else if(list[0]=="a" && list[2]=="false")
QMessageBox::information(this,"信息提示","注册失败,用户名已经被注册!",QMessageBox::Ok);
else if(list[0]=="b" && list[2]=="true")
//QMessageBox::information(this,"信息提示","登录成功!",QMessageBox::Ok);
{
UserInterface *user=new UserInterface(this,tcpSocket,list[1]);
this->close();
user->show();
readFlag=0;
}
else if(list[0]=="b" && list[2]=="fals