基于QT的UDP协议的通信

不选用GUI project 

new project  as following :

一.new qt4 project  select empty project

二,add new ->select general ->text file->main.cpp

三,add new ->C++ ->select header file ->client 

四,add new ->C++ ->select source file  ->client 

//over


//client.h

#ifndef CLIENT_H
#define CLIENT_H


#include <QDialog>
#include <QUdpSocket>


//add
#include <QUdpSocket>
#include <QTextEdit>
#include <QPushButton>
#include <QGridLayout>
namespace Ui {
    class client;
}


class client : public QDialog {
    Q_OBJECT
public:
    client(QWidget *parent = 0);
    ~client();
public slots:
    void recieve_message();


private:
 //   Ui::client *ui;
    QUdpSocket *udpsocket;
  //  QLineEdit *lineedit;
    QTextEdit *textEdit_recieve;
    QPushButton *pushbutton_send;
    QGridLayout *layout;
};






#endif // CLIENT_H


//client.cpp



#include "client.h"


#include <QDebug>
#include <QUdpSocket>


client::client(QWidget *parent) :
    QDialog(parent)//,
    //ui(new Ui::client)
{
  //  ui->setupUi(this);
    //add
    //window  set
    textEdit_recieve= new QTextEdit;
    pushbutton_send=new QPushButton("send") ;


    layout= new QGridLayout;
    layout->addWidget(this->textEdit_recieve,0,0,4,6);
    layout->addWidget(this->pushbutton_send,5,0,1,2);
    this->setLayout(layout);
    this->setWindowTitle("UDP_Client");


    //UDP set
    udpsocket=new QUdpSocket;
  //  udpsocket->bind(QHostAddress::LocalHost,8888);
    udpsocket->bind(8888);
   //connect(this->pushbutton_send,SIGNAL(clicked()),this,SLOT(send_message()));
    connect(this->udpsocket,SIGNAL(readyRead()),this,SLOT(recieve_message()));
}


client::~client()
{
    //delete ui;
}




void client::recieve_message()
{
    qDebug()<<"in client ";

//receive message .................
}

//main.cpp

#include <QtGui/QApplication>
#include "client.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    client w;
   // w.resize(320/2,200);
    w.show();
    return a.exec();
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值