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. Mac 终端 shell 公钥失效解决办法

    mac的终端下面使用ssh user@localhost输入密码不能正常登录解决   今天回来后发现系统突然很奇怪,以前在mac的终端下面使用ssh user@localhost输入密码就可以连接到远 ...

  2. 学而精计算机公共基础学习之路TEST2(程序设计基础)

    程序设计基础 程序设计方法与风格 1.程序设计方法 程序设计: 指设计.编制.调试程序的方法和过程. 程序设计方法是研究问题求解如何进行系统构造的软件方法学.常用的程序设计方法有:结构化程序设计方法. ...

  3. 再叙ASM

    上一篇文章,我们已体验到ASM的威力,那么结合上面的代码解释ASM是怎么执行的. ClassWriter clazzWriter = new ClassWriter(0); 首先看下官方文档对Clas ...

  4. myeclipse 奔溃解决办法

    myeclipse 突然奔溃 解决办法 JVM terminated.Exit code=1 意思是说java 虚拟机挂了,而不是myeclipse挂了. 进入myeclipse 目录 查看myecl ...

  5. jQuery学习笔记一

    一.jQuery版本兼容 jQuery版本2以上不支持IE6,7,8浏览器. 如果需要支持IE6/7/8,那么请选择1.9 同样还可以通过条件注释在使用IE6/7/8时只包含进1.9 <!--[ ...

  6. Spring的RestTemplata使用

    spring-web的RestTemplata是对java底层http的封装,使用RestTemplata用户可以不再关注底层的连接建立,并且RestTemplata不仅支持Rest规范,还可以定义返 ...

  7. 搞个小项目吧,做一个ppt播放器

    先来两个参考链接,接下来再进行实战 http://www.geek-workshop.com/forum.php?mod=viewthread&tid=1137 http://www.geek ...

  8. background:url() 背景图不显示

    奇怪的问题: .box-3 { width: 100%; height: 500px; border: solid 2px red; margin-top: 70px; padding: 0 0 0 ...

  9. 8_python连接数据库

    如何用python操作数据库? -- 导入pymysql                    -- import pymysql -- 创建连接                          - ...

  10. js实现最短时间走完不同速度的路程

    题目: 现在有一条公路,起点是0公里,终点是100公里.这条公路被划分为N段,每一段有不同的限速.现在他们从A公里处开始,到B公里处结束.请帮他们计算在不超过限速的情况下,最少需要多少时间完成这段路程 ...