Qt Socket(简单的通讯)

学习了Qt好长时间了,今天开始做聊天软件,在网上看了下被人做的,有点意思。不过它的功能有点简单,自己想扩展一下,主要增加的功能有;(下篇博客)

        1、实现客户端之间的私聊,以及群聊;

        2、传输文件;

        3、发送表情符号,类似QQ那种功能。

4、增加数据库的操作

5、实现用户注册、登陆的功能。

一下是程序的截图:


程序源代码:

client.h

#ifndef CLIENT_H #define CLIENT_H #include <QWidget> #include <QTcpSocket> #include <QTextEdit> #include <QLineEdit> #include <QPushButton> class client : public QWidget { Q_OBJECT public: explicit client(QWidget *parent = 0); signals: private: QTextEdit *textEdit; QLineEdit *lineEdit; QPushButton *sendBtn; QPushButton *connectBtn; QTcpSocket *m_tcpsocket; QString userStr; private slots: void connectTcpServer(); void connectedServer(); void readMessage(); void sendMessage(); }; #endif // CLIENT_H

client.cpp

#include "client.h"
#include <QHBoxLayout>
#include <QHostAddress>

client::client(QWidget *parent) :
    QWidget(parent)
{
    userStr = QString(tr("张三"));
    textEdit = new QTextEdit;
    textEdit->setReadOnly(true);
    lineEdit = new QLineEdit;
    connectBtn = new QPushButton("连接服务器");
    connect(connectBtn,SIGNAL(clicked()),this,SLOT(connectTcpServer()));
    sendBtn = new QPushButton("发送");
    connect(sendBtn,SIGNAL(clicked()),this,SLOT(sendMessage()));
    QHBoxLayout *bottomlayout = new QHBoxLayout;
    bottomlayout->addWidget(connectBtn);
    bottomlayout->addWidget(sendBtn);
    QVBoxLayout *mainlayout = new QVBoxLayout;
    mainlayout->addWidget(textEdit);
    mainlayout->addWidget(lineEdit);
    mainlayout->addLayout(bottomlayout);
    setLayout(mainlayout);
}

void client::connectTcpServer()
{
    m_tcpsocket = new QTcpSocket(this);
    m_tcpsocket->abort();
    m_tcpsocket->connectToHost(QHostAddress::LocalHost,55555);
    connect(m_tcpsocket,SIGNAL(connected()),this,SLOT(connectedServer()));
    connect(m_tcpsocket,SIGNAL(readyRead()),this,SLOT(readMessage()));
    textEdit->append(tr("连接服务器......")+'\n');
    connectBtn->setEnabled(false);
}

void client::connectedServer()
{
    textEdit->append(tr("连接服务器成功!")+'\n');
}

void client::readMessage()
{
    QString string;
    QByteArray block = m_tcpsocket->readAll();
    QDataStream in(block);
    in.setVersion(QDataStream::Qt_4_7);
    in>>string;
    textEdit->append(tr("用户有消息到来\n消息是:")+string+'\n');
}

void client::sendMessage()
{
    int type;
    type = 1;
    QString string = lineEdit->text();
    QByteArray block;
    QDataStream out(&block,QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_7);
    out<<userStr;
    out<<type;
    out<<string;
    m_tcpsocket->write(block);
    lineEdit->clear();
}

serverc.h

#ifndef SERVER_H
#define SERVER_H

#include <QWidget>
#include <QTcpSocket>
#include <QtNetwork>
#include <QTextEdit>
#include <QLineEdit>
#include <QPushButton>

class server : public QWidget
{
    Q_OBJECT
public:
    explicit server(QWidget *parent = 0);
    
signals:
private:
    QTextEdit *textEdit;
    QLineEdit *lineEdit;
    QPushButton *sendBtn;
    QPushButton *startBtn;
    QTcpSocket *m_tcpsocket;
    QTcpServer *m_tcpserver;
    
private slots:
    void startTcpServer();
    void sendMessage();
    void newConnect();
    void readMessage();
    
};

#endif // SERVER_H

server.cpp

#include "server.h"
#include <QHBoxLayout>

server::server(QWidget *parent) :
    QWidget(parent)
{
    textEdit = new QTextEdit;
    textEdit->setReadOnly(true);
    lineEdit = new QLineEdit;
    startBtn = new QPushButton("打开服务器");
    connect(startBtn,SIGNAL(clicked()),this,SLOT(startTcpServer()));
    sendBtn = new QPushButton("发送");
    connect(sendBtn,SIGNAL(clicked()),this,SLOT(sendMessage()));
    QHBoxLayout *bottomlayout = new QHBoxLayout;
    bottomlayout->addWidget(startBtn);
    bottomlayout->addWidget(sendBtn);
    QVBoxLayout *mainlayout = new QVBoxLayout;
    mainlayout->addWidget(textEdit);
    mainlayout->addWidget(lineEdit);
    mainlayout->addLayout(bottomlayout);
    setLayout(mainlayout);
}

void server::startTcpServer()
{
    startBtn->setEnabled(false);
    m_tcpserver = new QTcpServer;
    m_tcpserver->listen(QHostAddress::Any,55555);
    connect(m_tcpserver,SIGNAL(newConnection()),this,SLOT(newConnect()));
}

void server::sendMessage()
{
    QString string = lineEdit->text();
    QByteArray block;
    QDataStream out(&block,QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_7);
    out<<string;
    m_tcpsocket->write(block);
}

void server::newConnect()
{
    textEdit->append(tr("有用户链接...")+'\n');
    m_tcpsocket = m_tcpserver->nextPendingConnection();
    connect(m_tcpsocket,SIGNAL(readyRead()),this,SLOT(readMessage()));
}

void server::readMessage()
{
    QString userstr;
    int type;
    QString string;
    QByteArray block = m_tcpsocket->readAll();
    QDataStream in(block);
    in.setVersion(QDataStream::Qt_4_7);
    in>>userstr;
    in>>type;
    in>>string;
    textEdit->append(userstr+tr("用户有消息到来\n消息是:")+string+'\n');
}

main.cpp

#include <QApplication>
#include "widget.h"
#include "server.h"
#include "client.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //Widget w;
    //w.show();

    //setcodec
    QTextCodec::setCodecForTr(QTextCodec::codecForName("system"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("system"));
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("system"));

    server s;
    s.show();
    client c1;
    c1.show();
    return a.exec();
}

自己要多加练习,争取实现以上所列举的功能!


你可能感兴趣的:(Qt Socket(简单的通讯))