这里直接上代码:(工程文件在我的资源中:QT 之 TCP/IP 服务器和客户端(数据流出流入操作版本)链接:http://download.csdn.net/download/zhaoyuaiweide/9960458
操作很简单,都是最基本的一些调用,作为基础都总结在这里啦!
client.h
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <QMainWindow>
#include "QtNetwork"
namespace Ui {
class tcpclient;
}
class tcpclient : public QMainWindow
{
Q_OBJECT
public:
explicit tcpclient(QWidget *parent = 0);
~tcpclient();
void sendMessage();
private slots:
void tcpConnect();
void receiveMessage();
void displayError(QAbstractSocket::SocketError); //显示错误
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::tcpclient *ui;
QTcpSocket *client;
quint8 m_rec[7];
QByteArray m_recByte;
quint8 m_blocksize;
};
#endif // TCPCLIENT_H
client.cpp
#include "tcpclient.h"
#include "ui_tcpclient.h"
#include "qdebug.h"
#include <QDataStream>
tcpclient::tcpclient(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::tcpclient)
{
ui->setupUi(this);
m_blocksize = 0;
client = new QTcpSocket(this);
connect(client,SIGNAL(readyRead()),this,SLOT(receiveMessage()));
connect(client,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
}
tcpclient::~tcpclient()
{
delete ui;
}
void tcpclient::tcpConnect()
{
//取消所有连接
client->abort();
//连接客户端
client->connectToHost(ui->IPlineEdit->text(),ui->PortlineEdit->text().toInt());
}
//接收数据
void tcpclient::receiveMessage()
{
QDataStream in(client);
in.setVersion(QDataStream::Qt_5_5);
m_blocksize = 0;
if(m_blocksize == 0)
{
if(client->bytesAvailable() < sizeof(quint8))
{
return;
}
in>>m_blocksize;
}
if(client->bytesAvailable() < m_blocksize)
{
return;
}
m_recByte.clear();
m_recByte.resize(m_blocksize);
for(int i = 0;i<5;i++)
{
in>>m_rec[i];
m_recByte[i] = m_rec[i];
}
qDebug()<<"接收:"<<m_recByte.toHex()<<endl;
}
//发送数据
void tcpclient::sendMessage()
{
QByteArray mm_block;
QDataStream out(&mm_block,QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_5);
out<<(quint8)0;
out<<(quint8)0x01;
out<<(quint8)0x02;
out<<(quint8)0x03;
out<<(quint8)0x04;
out<<(quint8)0x05;
out.device()->seek(0);
out<<(quint8)(mm_block.size()-sizeof(quint8));
qDebug()<<mm_block.size()<<" "<<mm_block.toHex()<<endl;
client->write(mm_block);
}
void tcpclient::displayError(QAbstractSocket::SocketError)
{
qDebug()<<client->errorString();
}
//连接按钮
void tcpclient::on_pushButton_clicked()
{
this->tcpConnect();
}
//发送消息按钮
void tcpclient::on_pushButton_2_clicked()
{
sendMessage();
}
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QMainWindow>
#include <QtNetwork>
namespace Ui {
class tcpserver;
}
class tcpserver : public QMainWindow
{
Q_OBJECT
public:
explicit tcpserver(QWidget *parent = 0);
~tcpserver();
private slots:
void sendMessage();
void receiveMessage();
void successful();
void on_pushButton_clicked();
private:
Ui::tcpserver *ui;
QTcpServer *server;
QTcpSocket *client;
QByteArray m_recByte;
quint8 m_rec[7];
quint8 m_blocksize;
};
#endif // TCPSERVER_H
#include "tcpserver.h"
#include "ui_tcpserver.h"
#include <QDebug>
tcpserver::tcpserver(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::tcpserver)
{
ui->setupUi(this);
server = new QTcpServer(this);
m_blocksize = 0;
if(!server->listen(QHostAddress::Any,8001))//如果要监听本机地址:QHostAddress::LocalHost
{
qDebug()<<server->errorString();
//监听本地主机的6666端口,如果出错就输出错误信息,并关闭
close();
}
connect(server,SIGNAL(newConnection()),this,SLOT(successful()));
}
tcpserver::~tcpserver()
{
delete ui;
}
void tcpserver::successful()
{
ui->sendlabel->setText("successful");
//获取连接客户端的套接字
client = server->nextPendingConnection();
//服务器等待接收数据
connect(client,SIGNAL(readyRead()),this,SLOT(receiveMessage()));
}
//发送数据
void tcpserver::sendMessage()
{
QByteArray mm_block;
QDataStream out(&mm_block,QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_5);
out<<(quint8)0;
out<<(quint8)0x01;
out<<(quint8)0x02;
out<<(quint8)0x03;
out<<(quint8)0x04;
out<<(quint8)0x05;
out.device()->seek(0);
out<<(quint8)(mm_block.size()-sizeof(quint8));
qDebug()<<mm_block.size()<<" "<<mm_block.toHex()<<endl;
client->write(mm_block);
}
//接收数据
void tcpserver::receiveMessage()
{
QDataStream in(client);
in.setVersion(QDataStream::Qt_5_5);
m_blocksize = 0;
if(m_blocksize == 0)
{
if(client->bytesAvailable() < sizeof(quint8))
{
return;
}
in>>m_blocksize;
}
if(client->bytesAvailable() < m_blocksize)
{
return;
}
m_recByte.clear();
m_recByte.resize(m_blocksize);
for(int i = 0;i<5;i++)
{
in>>m_rec[i];
m_recByte[i] = m_rec[i];
}
qDebug()<<"接收:"<<m_recByte.toHex()<<endl;
}
//按键
void tcpserver::on_pushButton_clicked()
{
sendMessage();
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。