qt tcp 通信实例
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHostAddress>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->socket = new QTcpSocket(this);
}
void MainWindow::on_pushButton_2_clicked(){
this->socket->close();
}
void MainWindow::on_pushButton_clicked(){
this->socket->connectToHost("127.0.0.1",80000,QTcpSocket::ReadWrite);
connect(this->socket,SIGNAL(connected()),this,SLOT(connected()));
}
void MainWindow::connected(){
QMessageBox::about(this,"notice","connect successful");
connect(this->socket,SIGNAL(readyRead()),this,SLOT(readyread()));
}
void MainWindow::readyread(){
QMessageBox::about(this,"notice","ready read");
QByteArray arr = this->socket->readAll();
QDataStream *des = new QDataStream(&arr,QIODevice::ReadOnly);//重点
QString str1;
QString str2;
(*des)>>str1>>str2;
qDebug()<<str1+str2;
QMessageBox::about(this,"x",str1+str2); } MainWindow::~MainWindow()
{
delete ui;
}
服务端
#include "mainwindow.h"
#include "ui_mainwindow.h" #include <QTcpServer>
#include <QTcpSocket>
#include <QMessageBox>
#include <QByteArray>
#include <QString>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->socket = nullptr;
this->server = new QTcpServer(this);
this->server->listen(QHostAddress::Any,80000);
QObject::connect(this->server,SIGNAL(newConnection()),this,SLOT(newConnect()));
}
void MainWindow::newConnect(){
this->socket = this->server->nextPendingConnection();
QMessageBox::about(this,"notice","new connect");
connect(this->socket,SIGNAL(readyRead()),this,SLOT(ReceiveData()));
}
void MainWindow::ReceiveData(){
QByteArray arr = this->socket->readAll();
QDataStream dst(arr);
QString str1;
QString str2;
dst>>str1>>str2;
qDebug()<<str1<<str2;
}
void MainWindow::on_pushButton_clicked(){
QString str = this->ui->lineEdit->text();
QByteArray arr;
QDataStream dst(&arr,QIODevice::ReadWrite);
dst<<QString("message:")<<str;
this->socket->write(arr);
}
void MainWindow::on_pushButton_2_clicked(){
this->socket->close();
} MainWindow::~MainWindow()
{
delete ui;
}
qt tcp 通信实例的更多相关文章
- C#网络编程TCP通信实例程序简单设计
C#网络编程TCP通信实例程序简单设计 采用自带 TcpClient和TcpListener设计一个Tcp通信的例子 只实现了TCP通信 通信程序截图: 压力测试服务端截图: 俩个客户端链接服务端测试 ...
- Linux 虚拟串口及 Qt 串口通信实例
Linux 虚拟串口及 Qt 串口通信实例 2011-06-22 17:49 佚名 互联网 字号:T | T Linux 虚拟串口及 Qt 串口通信实例是本文所要介绍的内容,在实现过程中,打开了两个伪 ...
- Qt TCP通信
工程文件 QT += network 服务端 #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QTcpS ...
- python网络编程之TCP通信实例
一. server.py import socket host="localhost" port= s=socket.socket(socket.AF_INET,socket.SO ...
- QT实现TCP通信服务器端和客户端(支持多个客户端)精简版
上星期接了个私活,工期两星期,报酬3000,写一个小软件,采集定向网络上的数据,并进行双向通信,捣鼓了两天,终于把QT中tcp通信这块调通了,找过N多例子,绝大部分都是基本的一个服务端一个客户端通信的 ...
- 【转】证书的应用之一 —— TCP&SSL通信实例及协议分析(下)
原文链接 前面两部分分别讲解了如何在.net程序中使用SSL实现安全通信以及SSL的通信过程,并通过抓包工具具体分析了ssl的握手过程,本文通过一个demo来模拟ssl协议,在TCP之上实现自己的安全 ...
- VC++使用socket进行TCP、UDP通信实例总结
1. 两台计算机通信需要协议,通信的两台计算机IP必须唯一 2. 同一个计算机可以进行多个应用程序与其他计算机通信,IP地址唯一,而端口号是区别同一计算机(同一IP)的唯一 ...
- [C语言]一个很实用的服务端和客户端进行TCP通信的实例
本文给出一个很实用的服务端和客户端进行TCP通信的小例子.具体实现上非常简单,只是平时编写类似程序,具体步骤经常忘记,还要总是查,暂且将其记下来,方便以后参考. (1)客户端程序,编写一个文件clie ...
- 套接字、UDP通信、TCP通信、TCP/IP协议簇
一.套接字(socket) 1.英语单词socket:n.插座:穴:v.插入插座 2.套接字就是源IP地址和目的IP地址.源端口号和目的端口号的组合,是通过传输层进行通信的.IP指定电脑,端口指定某一 ...
随机推荐
- 解决java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener
启动eclipse 发现如下错误 Error configuring application listener of class org.springframework.web.util.Log4jC ...
- golang 如何查看channel通道中未读数据的长度
可以通过内建函数len查看channel中元素的个数. 内建函数len的定义如下: func len(v Type) int The len built-in function returns the ...
- Hadoop HDFS DataNode 目录结构
DataNode 目录结构 和namenode不同的是,datanode的存储目录是初始阶段自动创建的,不需要额外格式化. 1. 在/opt/module/hadoop-2.7.2/data/t ...
- 由于未能创建Visual C# 2015编译器,因此未能打开项目xxx。请重新安装Visual Studio。
解决方案1: 清除如下文件夹里的内容. %AppData%\Local\microsoft\VisualStudio\14.0\ComponentModelCache or C:\Users\DEL ...
- 如何在Macbook苹果笔记本上按右键点击(适用小米黑苹果)
1.按下Control键.保持按下Control(Ctrl)键,同时点击鼠标. 这一操作相当于在一个双键鼠标上右击. 点击鼠标后,你可以松开Control键. 该方法适用于单键鼠标或者MacBook ...
- php解析url并得到url中的参数及获取url参数
<?php $url = 'http://www.baidu.com/index.php?m=content&c=index&a=lists&catid=6&ar ...
- Delphi常用API,API函数
auxGetDevCaps API 获取附属设备容量 auxGetNumDevs API 返回附属设备数量 auxGetVolume API 获取当前卷设置 auxOutMessage API 向输出 ...
- .net core 连接sql server 时提示Connection Timeout Expired
.net core开发环境是ubuntu LINUX, 在ubuntu 上 telnet 数据库IP 端口是通的. SQL SERVER 是2008 ,未打SP补丁. 打完 SQL SERVER 2 ...
- VirtualBox 虚拟机复制
本文简单讲两种情况下的复制方式 1 跨电脑复制 2 同一virtrul box下 虚拟机复制 ---------------------------------------------- 1 跨电脑复 ...
- linux开机出现一下错误Give root password for maintenance (or type Control-D to continue):
由于错误的编辑/etc/fstab文件 而引起的不能正常进入系统.假如你将某一个分区或者磁盘最后一个参数设置为1或2时,系统默认会在开机过程中检查这个磁盘的扇区.假如系统检查不到这个磁盘,或者这个磁盘 ...