server

.server.h
#define DIALOG_H #include <QDialog>
#include <QTcpServer>
#include <QTcpSocket>
#include<QHostAddress>
namespace Ui {
class Dialog;
} class Dialog : public QDialog
{
Q_OBJECT public:
explicit Dialog(QWidget *parent = );
~Dialog(); private slots:
void on_pushButton_clicked(); void on_pushButton_2_clicked(); private:
Ui::Dialog *ui;
QTcpServer *p;
QTcpSocket *newp;//产生的新socket
QHostAddress *hp;
public slots:
void apprecvconnect();//处理连接函数
void getmsg();
}; #endif // DIALOG_H

server.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include<QString>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
p = new QTcpServer(); // newp = new QTcpSocket();
hp = new QHostAddress("192.168.1.30");
connect(p,SIGNAL(newConnection()),this,SLOT(apprecvconnect()));
} Dialog::~Dialog()
{
delete ui;
} void Dialog::on_pushButton_clicked()
{
p->listen(*hp,); }
void Dialog::apprecvconnect()
{
newp = p->nextPendingConnection();
connect(newp,SIGNAL(readyRead()),this,SLOT(getmsg())); }
void Dialog::getmsg()
{
QByteArray tmp = newp->readAll();
QString tmp2(tmp);
ui->label_2->setText(tmp2);
} void Dialog::on_pushButton_2_clicked()
{
QString tmp = ui->lineEdit->text();
newp->write(tmp.toLatin1());
}

client.h

#ifndef DIALOG_H
#define DIALOG_H #include <QDialog>
#include<QTcpServer>
#include<QTcpSocket>
#include<QHostAddress>
#include<QMessageBox>
namespace Ui {
class Dialog;
} class Dialog : public QDialog
{
Q_OBJECT public:
explicit Dialog(QWidget *parent = );
~Dialog(); private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked(); public slots:
void sett();
void getmsg(); private:
Ui::Dialog *ui;
QTcpSocket *p;
QHostAddress *hp;
}; #endif // DIALOG_H

sever.cpp

#include "dialog.h"
#include "ui_dialog.h" Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
p= new QTcpSocket();
hp = new QHostAddress("192.168.1.30");
} Dialog::~Dialog()
{
delete ui;
} void Dialog::on_pushButton_clicked()
{
p->connectToHost(*hp,);
connect(p,SIGNAL(connected()),this,SLOT(sett()));
}
void Dialog::sett()
{
QMessageBox::about(this,"about","connect success");
connect(p,SIGNAL(readyRead()),this,SLOT(getmsg())); }
void Dialog::getmsg()
{
QByteArray tmp = p->readAll();
QString tmp2(tmp);
ui->label_2->setText(tmp2);
}
void Dialog::on_pushButton_2_clicked()
{
QString tmp = ui->lineEdit->text();
p->write(tmp.toLatin1());
}

ui.h

#ifndef UI_DIALOG_H
#define UI_DIALOG_H #include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton> QT_BEGIN_NAMESPACE class Ui_Dialog
{
public:
QPushButton *pushButton;
QPushButton *pushButton_2;
QLabel *label;
QLineEdit *lineEdit;
QLabel *label_2; void setupUi(QDialog *Dialog)
{
if (Dialog->objectName().isEmpty())
Dialog->setObjectName(QStringLiteral("Dialog"));
Dialog->resize(, );
pushButton = new QPushButton(Dialog);
pushButton->setObjectName(QStringLiteral("pushButton"));
pushButton->setGeometry(QRect(, , , ));
pushButton_2 = new QPushButton(Dialog);
pushButton_2->setObjectName(QStringLiteral("pushButton_2"));
pushButton_2->setGeometry(QRect(, , , ));
label = new QLabel(Dialog);
label->setObjectName(QStringLiteral("label"));
label->setGeometry(QRect(, , , ));
lineEdit = new QLineEdit(Dialog);
lineEdit->setObjectName(QStringLiteral("lineEdit"));
lineEdit->setGeometry(QRect(, , , ));
label_2 = new QLabel(Dialog);
label_2->setObjectName(QStringLiteral("label_2"));
label_2->setGeometry(QRect(, , , )); retranslateUi(Dialog); QMetaObject::connectSlotsByName(Dialog);
} // setupUi void retranslateUi(QDialog *Dialog)
{
Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", ));
pushButton->setText(QApplication::translate("Dialog", "connect", ));
pushButton_2->setText(QApplication::translate("Dialog", "send", ));
label->setText(QApplication::translate("Dialog", "client", ));
label_2->setText(QString());
} // retranslateUi }; namespace Ui {
class Dialog: public Ui_Dialog {};
} // namespace Ui QT_END_NAMESPACE #endif // UI_DIALOG_H

qt中的tcp编程的更多相关文章

  1. QT中的SOCKET编程(QT-2.3.2)

    转自:http://mylovejsj.blog.163.com/blog/static/38673975200892010842865/ QT中的SOCKET编程 2008-10-07 23:13 ...

  2. QT中的SOCKET编程

    转自:http://mylovejsj.blog.163.com/blog/static/38673975200892010842865/ QT中的SOCKET编程 2008-10-07 23:13 ...

  3. Qt中的多线程编程

    http://www.ibm.com/developerworks/cn/linux/l-qt-mthrd/ Qt 作为一种基于 C++ 的跨平台 GUI 系统,能够提供给用户构造图形用户界面的强大功 ...

  4. python中的TCP编程学习

    今天看了一下关于python的TCP编程. 发现思路和其他语言(比如java)思路基本上差点儿相同. 先看client.基本过程例如以下: 第一步:创建一个socket 第二步:建立连接 第三步:发送 ...

  5. Qt中的串口编程之三

    QtSerialPort 今天我们来介绍一下QtSerialPort模块的源代码,学习一下该可移植的串口编程库是怎么实现的. 首先,我们下载好了源代码之后,使用QtCreator打开整个工程,可以看到 ...

  6. 5.关于QT中的网络编程,QTcpSocket,QUdpSocket

     1 新建一个项目:TCPServer.pro A  修改TCPServer.pro,注意:如果是想使用网络库,需要加上network SOURCES += \ TcpServer.cpp \ T ...

  7. Qt中的串口编程之一

    QtSerialPort 简介 功能介绍 SerialPort SerialPortInfo 源代码 编译和安装 配置编译环境 Perl只是在Qt5的时候才需要Qt4的情况下可以不配置 使用如下推荐步 ...

  8. qt中的udp编程

    UDP QUdpSocket ---> upd socket 1.创建 QUdpSocket *p = new QUdpSocket(); 2.最先接收数据一方 调用bind-> ip/p ...

  9. golang中的tcp编程

    1. tcp server package main import ( "bufio" "fmt" "net" ) func main() ...

随机推荐

  1. Spark算子--cogroup

    转载请标明出处http://www.cnblogs.com/haozhengfei/p/b612b1e6d9b951fad5574cd0ce573d7e.html cogroup--Transform ...

  2. virtuoso装载大的rdf文件的方法

    本文详细介绍了将一个比较大的rdf文件装载到virtuoso数据库的过程.参考virtuoso网站的文档说明,通过实践,将一个大约4.6G左右的nt文件装载到virtuoso数据库中,用了大概6个多小 ...

  3. Java 获取年月日方法

    package com.ob; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util ...

  4. 四、Html常用标签

    1,列表相关标签 <ul>:定义无序列表,只能包含<li>子元素 <ol>:定义有序列表,只能包含<li>子元素,因为这个标签是有序的,所有这个标签还有 ...

  5. android onSaveInstanceState应用实例

    //activity销毁之前调用,把状态值存储上 @Override protected void onSaveInstanceState(Bundle outState) { outState.pu ...

  6. ProgressDialog的使用及逻辑处理

    一般用的情况先声明一个ProgressDialog progressShow = true;(用来判断用户是否点击了取消键) final ProgressDialog pd = new Progres ...

  7. 在阿里云上搭建自己的git服务器

    这篇文章我就来介绍一下如何在一台全裸的阿里云主机上搭建自己的git服务器. 1. 安装git 首先安装git,一般而言,现在的服务器已经内置了git安装包,我们只需要执行简单的安装命令即可安装.比如: ...

  8. nginx配置中root与alias的区别

    nginx指定文件路径有两种方式root和alias,这两者的用法区别,使用方法总结了下,方便大家在应用过程中,快速响应.root与alias主要区别在于nginx如何解释location后面的uri ...

  9. Hyperledger Fabric Endorsement policies——背书策略

    背书策略 背书策略用于指导peer如何确定交易是否得到了的认可.当一个peer接收到一个事务时,它会调用与事务的Chaincode相关联的VSCC(验证系统链代码),作为事务验证流程的一部分,以确定交 ...

  10. AngularJS执行流程详解(转)

    一.启动阶段 大家应该都知道,当浏览器加载一个HTML页面时,它会将HMTL页面先解析成DOM树,然后逐个加载DOM树中的每一个元素节点.我们可以把AngularJS当做一个类似jQuery的js库, ...