您的位置:首页 > 编程语言 > Qt开发

Qt的Socket数据通讯的一个例子。

2016-06-11 00:00 525 查看
QTcpServer类用来侦听端口,获取QTcpSocket.

QTcpSocket有connected的信号(已经连接),还有readyread()信号,表示已经有数据发过来了,准备读取。

若要关闭当前连接只需要调用qtcpsocket::close();就关闭了当前连接

下面有两个例子

服务器端

用的是控制台程序(QT)当用户发送数据过来就cout显示,然后就write一个ILoveYou的字符串返回到客户端。然后close断开连接

客户端

用的书图形界面,一个输入框输入数据然后发送,最后QMessagebox显示服务器返回消息

=======================

服务器端(三个文件)

#ifndefMYSERVER_H
#defineMYSERVER_H
#include<QTcpSocket>
#include<iostream>
#include<QObject>
#include<QTcpServer>
classmyserver:publicQTcpServer
{
Q_OBJECT
public:
QTcpSocket*socket;
QTcpServer*server;
myserver();
privateslots:
voidgetData();
voidnewconnectslot();
};
#endif//MYSERVER_H
#include"myserver.h"
#include<QByteArray>
#include<QString>
#include<QDataStream>
myserver::myserver()
{
this->socket=newQTcpSocket;
this->server=newQTcpServer;
if(this->server->listen(QHostAddress::Any,520))
{
std::cout<<"bindport520successful."<<std::endl;
}else
{
std::cout<<"bindport520failed."<<std::endl;
}
QObject::connect(this->server,SIGNAL(newConnection()),this,SLOT(newconnectslot()));
}
voidmyserver::newconnectslot()
{
this->socket=this->server->nextPendingConnection();
connect(this->socket,SIGNAL(readyRead()),this,SLOT(getData()));
}
voidmyserver::getData()
{
QByteArrayby=this->socket->readAll();
QDataStreamds(by);
QStringx;
ds>>x;
QByteArrayba=x.toLatin1();
char*p=ba.data();
std::cout<<p<<std::endl;
socket->write("Iloveyou");//返回给客户端
this->socket->close();//断开连接
}
#include<QCoreApplication>
#include<iostream>
#include"myserver.h"
#include<QHostAddress>
intmain(intargc,char*argv[])
{
QCoreApplicationa(argc,argv);
std::cout<<"--ServerinitializedByHanHan--"<<std::endl;
myserver*server=newmyserver;
returna.exec();
}

客户端(三个文件)

#ifndefMAINWINDOW_H#defineMAINWINDOW_H#include<QMainWindow>#include<QString>#include<QByteArray>#include<QDataStream>#include<QTcpSocket>namespaceUi{classMainWindow;}classMainWindow:publicQMainWindow{Q_OBJECTpublic:QTcpSocket*socket;explicitMainWindow(QWidget*parent=0);~MainWindow();privateslots:voidconnnectslot();voidon_btn_send_clicked();voidreadyslot();private:Ui::MainWindow*ui;};#endif//MAINWINDOW_H


#include"mainwindow.h"#include"ui_mainwindow.h"#include<QHostAddress>#include<QMessageBox>MainWindow::MainWindow(QWidget*parent):&nbs
7fe0
p;QMainWindow(parent),ui(newUi::MainWindow){ui->setupUi(this);this->socket=newQTcpSocket;}MainWindow::~MainWindow(){deleteui;}voidMainWindow::on_btn_send_clicked(){QHostAddressaddress("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()));//接收发来的数据}voidMainWindow::connnectslot(){QStringdata=this->ui->data_edit->toPlainText();QByteArrayarray;QDataStreamds(&array,QIODevice::WriteOnly);ds<<data;this->socket->write(array);}


voidMainWindow::readyslot(){QStringx=this->socket->readAll();QMessageBox::about(this,"x",x);}#include"mainwindow.h"#include<QApplicatio>

intmain(intargc,char*argv[])
{
QApplicationa(argc,argv);
MainWindoww;
w.show();

returna.exec();
}


运行截图:


n




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: