Qt的Socket数据通讯的一个例子。
来源:互联网 发布:java建立socket服务器 编辑:程序博客网 时间:2024/05/15 13:34
QTcpServer类 用来侦听端口 ,获取QTcpSocket.
QTcpSocket有 connected的信号(已经连接),还有readyread()信号,表示已经有数据发过来了,准备读取。
若要关闭 当前连接只需要 调用 qtcpsocket::close();就关闭了当前连接
下面有两个例子
服务器端
用的是控制台程序(QT) 当用户 发送数据过来 就cout显示,然后就write一个 I Love You的字符串 返回到客户端。然后close断开连接
客户端
用的书图形界面,一个输入框 输入数据 然后发送,最后 QMessagebox显示服务器返回消息
=======================
服务器端(三个文件)
#ifndef MYSERVER_H#define MYSERVER_H#include<QTcpSocket>#include<iostream>#include <QObject>#include<QTcpServer>class myserver : public QTcpServer{ Q_OBJECTpublic: QTcpSocket * socket; QTcpServer *server; myserver();private slots: void getData(); void newconnectslot();};#endif // MYSERVER_H#include "myserver.h"#include<QByteArray>#include<QString>#include<QDataStream>myserver::myserver(){ this->socket=new QTcpSocket; this->server=new QTcpServer; if(this->server->listen(QHostAddress::Any,520)) { std::cout<<"bind port 520 successful."<<std::endl; }else { std::cout<<"bind port 520 failed."<<std::endl; } QObject::connect(this->server,SIGNAL(newConnection()),this,SLOT(newconnectslot()));}void myserver::newconnectslot(){ this->socket=this->server->nextPendingConnection(); connect(this->socket,SIGNAL(readyRead()),this,SLOT(getData()));}void myserver::getData(){ QByteArray by=this->socket->readAll(); QDataStream ds(by); QString x; ds>>x; QByteArray ba = x.toLatin1(); char * p=ba.data(); std::cout<<p<<std::endl; socket->write("I love you");//返回给客户端 this->socket->close();//断开连接}#include <QCoreApplication>#include<iostream>#include"myserver.h"#include<QHostAddress>int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); std::cout<<"--Server initialized By HanHan--"<<std::endl; myserver *server=new myserver; return a.exec();}
客户端(三个文件)
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include<QString>#include<QByteArray>#include<QDataStream>#include<QTcpSocket>namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECTpublic: QTcpSocket * socket; explicit MainWindow(QWidget *parent = 0); ~MainWindow();private slots: void connnectslot(); void on_btn_send_clicked(); void readyslot();private: Ui::MainWindow *ui;};#endif // MAINWINDOW_H
#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;}MainWindow::~MainWindow(){ delete ui;}void MainWindow::on_btn_send_clicked(){ QHostAddress address("127.0.0.1"); this->socket->connectToHost(address,520); connect(this->socket,SIGNAL(connected()),this,SLOT(connnectslot())); connect(this->socket,SIGNAL(readyRead()),this,SLOT(readyslot()));//接收发来的数据}void MainWindow::connnectslot(){ QString data=this->ui->data_edit->toPlainText(); QByteArray array; QDataStream ds(&array,QIODevice::WriteOnly); ds<<data; this->socket->write(array);}
void MainWindow::readyslot(){ QString x=this->socket->readAll(); QMessageBox::about(this,"x",x);}#include "mainwindow.h"#include <QApplicatio>int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; w.show(); return a.exec();}
运行截图:
n
0 0
- Qt的Socket数据通讯的一个例子。
- vue组件之间的数据通讯小例子
- Qt学习 之 Socket通信(一个简单的例子)
- 一个简单的socket例子
- 一个简单socket的例子
- 一个socket的基础例子
- 一个简单的socket例子
- 一个Android Socket的例子
- 一个完整的Socket例子
- 一个Android Socket的例子
- 一个Android Socket的例子
- 一个Android Socket的例子
- 一个Android Socket的例子
- Qt 多线程的一个例子
- 计算机之间的数据通讯
- 一个完整的 epoll + socket 的例子
- win32 socket的一个简单的例子
- 关于socket通信的一个例子!
- HDU 1711 — Number Sequence简单题
- Android中实现短信验证码自动填入
- 2015年阿里巴巴研发工程师笔试题
- 《利用Python进行数据分析》第二章pivot_table报错问题
- Machine Schedule
- Qt的Socket数据通讯的一个例子。
- asp.net mvc 5.0 借助路由规则实现*.aspx与HttpHandler交互
- windows与linux下执行.class(包含main方法)
- Oracle学习笔记20150817一些复杂查询语句分析
- BI-扩展后排序
- AutoLayOut+tabbar push到二级界面遇到偏移的问题
- ChartServer
- 虚拟化的逆袭:OpenFlow和SDN
- leetcode - 字符串转换成数字(String to Integer)atoi