基于QT的TCP协议实现的通信小程序

服务端:

//main.cpp

#include "dialog.h"
#include <QApplication>

int main(int argc, char** argv)
{
 QApplication app(argc,argv);

 Dialog dlg;
 dlg.show();
 return app.exec();
}

//server.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>274</width>
    <height>241</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <layout class="QGridLayout" name="gridLayout">
   <item row="0" column="0" colspan="2">
    <widget class="QTextEdit" name="textEdit"/>
   </item>
   <item row="1" column="0">
    <widget class="QPushButton" name="sendButton">
     <property name="text">
      <string>send</string>
     </property>
    </widget>
   </item>
   <item row="1" column="1">
    <widget class="QPushButton" name="exitButton">
     <property name="text">
      <string>exit</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

//ui_server.h

/********************************************************************************
** Form generated from reading ui file 'server.ui'
**
** Created: Wed Sep 2 11:44:20 2009
**      by: Qt User Interface Compiler version 4.5.0
**
** WARNING! All changes made in this file will be lost when recompiling ui file!
********************************************************************************/

#ifndef UI_SERVER_H
#define UI_SERVER_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialog>
#include <QtGui/QGridLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QPushButton>
#include <QtGui/QTextEdit>

QT_BEGIN_NAMESPACE

class Ui_Dialog
{
public:
    QGridLayout *gridLayout;
    QTextEdit *textEdit;
    QPushButton *sendButton;
    QPushButton *exitButton;

    void setupUi(QDialog *Dialog)
    {
        if (Dialog->objectName().isEmpty())
            Dialog->setObjectName(QString::fromUtf8("Dialog"));
        Dialog->resize(274, 241);
        gridLayout = new QGridLayout(Dialog);
        gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
        textEdit = new QTextEdit(Dialog);
        textEdit->setObjectName(QString::fromUtf8("textEdit"));

        gridLayout->addWidget(textEdit, 0, 0, 1, 2);

        sendButton = new QPushButton(Dialog);
        sendButton->setObjectName(QString::fromUtf8("sendButton"));

        gridLayout->addWidget(sendButton, 1, 0, 1, 1);

        exitButton = new QPushButton(Dialog);
        exitButton->setObjectName(QString::fromUtf8("exitButton"));

        gridLayout->addWidget(exitButton, 1, 1, 1, 1);


        retranslateUi(Dialog);

        QMetaObject::connectSlotsByName(Dialog);
    } // setupUi

    void retranslateUi(QDialog *Dialog)
    {
        Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0, QApplication::UnicodeUTF8));
        sendButton->setText(QApplication::translate("Dialog", "send", 0, QApplication::UnicodeUTF8));
        exitButton->setText(QApplication::translate("Dialog", "exit", 0, QApplication::UnicodeUTF8));
        Q_UNUSED(Dialog);
    } // retranslateUi

};

namespace Ui {
    class Dialog: public Ui_Dialog {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_SERVER_H

//dialog.h

#ifndef _DIALOG_H_
#define _DIALOG_H_

#include <QDialog>
#include "ui_server.h"
class TcpServer;

class Dialog:public QDialog,public Ui_Dialog
{
 Q_OBJECT
public:
 Dialog(QWidget *parent = 0);
private:
 TcpServer* tcpServer;
};

#endif

//dialog.cpp

#include "dialog.h"
#include <QtDebug>
#include "tcpserver.h"

Dialog::Dialog(QWidget *parent) : QDialog(parent)
{
 setupUi(this);
 tcpServer = new TcpServer(this);

 connect(exitButton,SIGNAL(clicked()),this,SLOT(close()));
}

//tcpserver.h

#ifndef _TCPSERVER_H_
#define _TCPSERVER_H_

#include <QTcpServer>
#include <QTcpSocket>

class TcpServer:public QTcpServer
{
 Q_OBJECT
 
public:
 TcpServer(QWidget *parent = 0);
 void incomingConnection(int socketDiscriptor);
 //void QTcpServer::incomingConnection ( int socketDescriptor )   [virtual protected]
 //This virtual function is called by QTcpServer when a new connection is available. 
 //The socketDescriptor argument is the native socket descriptor for the accepted connection.
private:
 QTcpSocket* tcpSocket;
 
public slots:
 void recv_slot();
 void send_slot();
};
#endif

//tcpserver.cpp

#include "tcpserver.h"
#include <QByteArray>
#include "dialog.h"

TcpServer :: TcpServer(QWidget *parent) : QTcpServer(parent)
{
 listen(QHostAddress::LocalHost,8888);
}

void TcpServer::incomingConnection(int socketDescriptor)
{
 tcpSocket = new QTcpSocket(this);
 tcpSocket -> setSocketDescriptor(socketDescriptor);
 connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(recv_slot()));
 
 Dialog* dlg = qobject_cast<Dialog*>(parent());
 connect(dlg -> sendButton,SIGNAL(clicked()),this,SLOT(send_slot()));
}

void TcpServer::recv_slot()
{
 QByteArray byte;
 byte = tcpSocket -> readAll();
  
  Dialog* dlg = qobject_cast<Dialog*>(parent());
 dlg -> textEdit -> setPlainText(QString(byte));
}

void TcpServer::send_slot()
{
 Dialog* dlg = qobject_cast<Dialog*>(parent());
 
 QString str = dlg -> textEdit -> toPlainText();
 tcpSocket -> write(qPrintable(str),256);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值