学习一点TCP网络协议的知识,利用Qt做一次调查问卷,客户端与服务器端如下:
服务器:
客户端:
客户端:
首先在创建的tcpclient工程下的tcpclient.pro中加入
QT += network
然后编写tcpclient.h
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <QDialog>
#include <QListWidget>
#include <QCheckBox>
#include <QPushButton>
#include <QLabel>
#include <QGridLayout>
#include <QHostAddress>
#include <QTcpSocket>
#include <QButtonGroup>
#include <QLineEdit>
class TcpClient : public QDialog
{
Q_OBJECT
public:
TcpClient(QWidget *parent = 0,Qt::WindowFlags f=0);
~TcpClient();
private:
QString username1;
QPushButton *saveBtn;
QPushButton *submitBtn;
QLabel *title;
QLineEdit *lineedit1;
QLabel *username;
QPushButton *yesbutton;
QLabel *question1;
QLabel *question2;
QLabel *question3;
QLabel *question31;
QLabel *question32;
QLabel *question33;
QLabel *question34;
QButtonGroup *answer1;
QButtonGroup *answer2;
QButtonGroup *answer3A;
QButtonGroup *answer3B;
QButtonGroup *answer3C;
QButtonGroup *answer3D;
QCheckBox *answer11;
QCheckBox *answer12;
QCheckBox *answer13;
QCheckBox *answer21;
QCheckBox *answer22;
QCheckBox *answer311;
QCheckBox *answer312;
QCheckBox *answer321;
QCheckBox *answer322;
QCheckBox *answer331;
QCheckBox *answer332;
QCheckBox *answer341;
QCheckBox *answer342;
QGridLayout *mainLayout;
bool savestatus;
bool submitstatus;
int port;
QHostAddress *serverIP;
QTcpSocket *tcpSocket;
public slots:
QString slotSave();
void slotConnected();
void getname();
void slotSaveBox();
void dataReceived();
void slotSubmit();
QString slotAnswer1();
QString slotAnswer2();
};
#endif // TCPCLIENT_H
tcpclient.cpp
#include "tcpclient.h"
#include <QMessageBox>
#include <QHostInfo>
TcpClient::TcpClient(QWidget *parent,Qt::WindowFlags f)
: QDialog(parent,f)
{
setWindowTitle(tr("问卷调查"));
title