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. UITableViewCell滑动删除及移动

    实现Cell的滑动删除, 需要实现UITableView的代理UITableViewDelegate中如下方法: //先要设Cell可编辑 - (BOOL)tableView:(UITableView ...

  2. Hive字段中文注释乱码解决办法

    Hive字段中文乱码,如执行 show create table xxx 时,表级别注释.字段级别注释发现有乱码现象, 一般都是由hive 元数据库的配置不当造成的. 此时可按如下步骤进行配置调整: ...

  3. 基础数据类型的补充和深浅copy

    一:关于str 的操作方法补充 1,s.isspace()   判断字符串是否只由空格组成,是,为True,否则,为False. s = ' ' #只能是以至少一个空格组成的字符串(全空格) prin ...

  4. canvas实现倒计时效果示例(vue组件内编写)

    前言: 此事例是在vue组件中,使用canvas实现倒计时动画的效果.其实,实现效果的逻辑跟vue没有关系,只要读懂canvas如何实现效果的这部分逻辑就可以了 canvas动画的原理:利用定时器,给 ...

  5. 多IP服务器应用可以有效的降低成本

    多IP的常规应用很多,SEO,EDM,VPN代理等.可以有效的解决成本,很多时候的租用一台高配置服务器通过XEN,hyper-V等虚拟化技术分割成VPS ,共用一台服务器就会大大的降低成本,这样就需要 ...

  6. SpringMvc4.x--Spring MVC的常用注解

    //下列代码显示用到的对象public class DemoObj { private Long id; private String name; public DemoObj() { //① sup ...

  7. 优化 gruop by 语句

    默认情况下,mysql对所有的gruop by col1,col2...的字段进行排序.如果查询包含group by但用户想要避免排序结果的消耗,则可以指定order by null禁止排序. exp ...

  8. Redis清空数据

    进入redis目录下 redis-cli -h IP -p 端口 -a 密码 flushall

  9. java IO(六):额外功能处理流

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  10. js中常用的方法(数组篇)

    1.replace(),根据释义,即为代替,用法为: stringObject.replace(regexp/substr,replacement)括号内前者是待匹配字符串,并用后者代替这个字符串.例 ...