QTcpServer与QTcpSocket通讯
TCP是一个基于流的协议。对于应用程序,数据表现为一个长长的流,而不是一个大大的平面文件。基于TCP的高层协议通常是基于行的或者基于块的。
●、基于行的协议把数据作为一行文本进行传输,每行都以一个换行符结尾。
●、基于块的协议把数据作为二进制块进行传输,每块是由一个size大小字段和紧跟它的一个size字节的数据组成。
QTcpSocket通过器父类QAbstractSocket继承了QIODevice,因此他可以通过使用QTextStream和QDataStream来进行读取和写入。
QTcpServer类在服务器端处理来自TCP客户端连接数据,需要注意的是,该类直接继承于QObject基类,而不是QAbstractSocket抽象套接字类。
#define VXMAINWINDOW_H
#include
#include
class QTextEdit;
{
Q_OBJECT
CVxMainWindow(QWidget *parent = NULL);
~CVxMainWindow();
protected:
void resizeEvent(QResizeEvent *);
private slots:
void Btn_ListenClickedSlot();
void Btn_StopListenClickedSlot();
void newConnectionSlot();
void dataReceived();
private:
QTcpServer *m_pServer;
QTcpSocket *m_pSocket;
QPushButton *m_pBtn_StopListen;
QTextEdit *m_pEdt_Info;
};
: QWidget(parent)
{
m_pBtn_Listen = new QPushButton(QObject::tr("开始监听"), this);
m_pBtn_StopListen = new QPushButton(QObject::tr("停止监听"), this);
m_pEdt_Info = new QTextEdit(this);
m_pServer = new QTcpServer(this);
connect(m_pBtn_Listen, SIGNAL(clicked()), this, SLOT(Btn_ListenClickedSlot()));
connect(m_pBtn_StopListen, SIGNAL(clicked()), this, SLOT(Btn_StopListenClickedSlot()));
connect(m_pServer, SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));
}
{
{
m_pBtn_Listen->setGeometry(10, 5, 80, 20);
m_pBtn_StopListen->setGeometry(100, 5, 80, 20);
m_pEdt_Info->setGeometry(0, 30, width(), height() - 30);
}
{
if (!m_pServer->isListening())
{
if (m_pServer->listen(QHostAddress::Any, 8080))
{
m_pEdt_Info->append(QObject::tr("打开监听端口成功!"));
}
else
{
m_pEdt_Info->append(QObject::tr("打开监听端口失败!"));
}
}
else
{
m_pEdt_Info->append(QObject::tr("正在监听中...!"));
}
}
{
if (m_pServer->isListening())
{
m_pServer->close();
m_pEdt_Info->append(QObject::tr("停止监听!"));
}
}
{
m_pEdt_Info->append(QObject::tr("有新客户端连接到服务器"));
m_pSocket = m_pServer->nextPendingConnection();
connect(m_pSocket, SIGNAL(disconnected()), m_pSocket, SLOT(deleteLater()));
connect(m_pSocket, SIGNAL(readyRead()),this, SLOT(dataReceived()));
QString vMsgStr = QObject::tr("Welcome");
if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
{
}
{
while(m_pSocket->bytesAvailable())
{
QByteArray vTemp;
vTemp = m_pSocket->readLine();
m_pEdt_Info->append(vTempStr);
QString vMsgStr = QObject::tr("回复:") + vTempStr;
if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
{
}
}
#define VXMAINWINDOW_H
#include
#include
class QTextEdit;
class QLineEdit;
{
Q_OBJECT
CVxMainWindow(QWidget *parent = NULL);
~CVxMainWindow();
protected:
void resizeEvent(QResizeEvent *);
private slots:
void Btn_ConnectClickedSlot();
void Btn_DisConnectClickedSlot();
void Btn_SendClickedSlot();
void connectedSlot();
void disconnectedSlot();
void dataReceived();
void displayError(QAbstractSocket::SocketError);
private:
QTcpSocket *m_pSocket;
QHostAddress m_HostAddress;
QPushButton *m_pBtn_DisConnect;
QTextEdit *m_pEdt_Info;
QLineEdit *m_pEdt_Send;
QPushButton *m_pBtn_Send;
};
: QWidget(parent)
{
m_pBtn_Connect = new QPushButton(QObject::tr("连接服务器"), this);
m_pBtn_DisConnect = new QPushButton(QObject::tr("断开连接"), this);
m_pEdt_Send = new QLineEdit(this);
m_pBtn_Send = new QPushButton(QObject::tr("发送"), this);
m_pEdt_Info = new QTextEdit(this);
m_pSocket = new QTcpSocket(this);
connect(m_pBtn_DisConnect, SIGNAL(clicked()), this, SLOT(Btn_DisConnectClickedSlot()));
connect(m_pBtn_Send, SIGNAL(clicked()), this, SLOT(Btn_SendClickedSlot()));
connect(m_pSocket, SIGNAL(connected()), this, SLOT(connectedSlot()));
connect(m_pSocket, SIGNAL(disconnected()), this, SLOT(disconnectedSlot()));
connect(m_pSocket, SIGNAL(readyRead()),this, SLOT(dataReceived()));
connect(m_pSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
}
{
{
m_pBtn_Connect->setGeometry(10, 5, 80, 20);
m_pBtn_DisConnect->setGeometry(100, 5, 80, 20);
m_pEdt_Send->setGeometry(10, 30, 150, 20);
m_pBtn_Send->setGeometry(170, 30, 80, 20);
m_pEdt_Info->setGeometry(0, 60, width(), height() - 60);
}
{
m_HostAddress.setAddress(QObject::tr("127.0.0.1"));
m_pSocket->connectToHost(m_HostAddress, 8080);
}
{
m_pSocket->disconnectFromHost();
}
{
int length = 0;
QString vMsgStr = m_pEdt_Send->text();
if((length = m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length())) != vMsgStr.length())
{
m_pEdt_Info->append(QObject::tr("发送信息失败:") + vMsgStr);
}
}
{
m_pEdt_Info->append(QObject::tr("成功连接到服务器!"));
}
{
m_pEdt_Info->append(QObject::tr("断开与服务器的连接!"));
}
{
while(m_pSocket->bytesAvailable())
{
QString vTemp;
vTemp = m_pSocket->readLine();
m_pEdt_Info->append(vTemp);
}
}
{
{
// 用于暂存我们要发送的数据
QByteArray block;
// 使用数据流写入数据
QDataStream out(&block,QIODevice::WriteOnly);
// 设置数据流的版本,客户端和服务器端使用的版本要相同
out.setVersion(QDataStream::Qt_4_7);
out<<(quint16) 0;
out.device()->seek(0);
out<<(quint16)(block.size() - sizeof(quint16));
m_pSocket->write(block);
}
{
QString vTempStr;
quint16 blockSize = 0;
// 设置数据流版本,这里要和服务器端相同
in.setVersion(QDataStream::Qt_4_7);
// 如果是刚开始接收数据
if(blockSize == 0)
{
//判断接收的数据是否有两字节,也就是文件的大小信息
//如果有则保存到blockSize变量中,没有则返回,继续接收数据
if(m_pSocket->bytesAvailable() < (int)sizeof(quint16)) return;
in >> blockSize;
}
// 如果没有得到全部的数据,则返回,继续接收数据
if(m_pSocket->bytesAvailable() < blockSize) return;
in >> vTempStr;
m_pEdt_Info->append(vTempStr);
}
QTcpServer与QTcpSocket通讯的更多相关文章
- qt QTcpServer与QTcpSocket通讯
分类: C/C++ TCP TCP是一个基于流的协议.对于应用程序,数据表现为一个长长的流,而不是一个大大的平面文件.基于TCP的高层协议通常是基于行的或者基于块的. ...
- Qt网络编程QTcpServer和QTcpSocket的理解
前一段时间通过调试Qt源码,大致了解了Qt的事件机制.信号槽机制.毕竟能力和时间有限.有些地方理解的并不是很清楚. 开发环境:Linux((fedora 17),Qt版本(qt-everywhere- ...
- C/C++学习)22.QTcpServer、QTcpSocket、QUdpSocket使用
一.TCP/UDP通信在Qt中的实现过程: 废话不说,首先下面是Qt中TCP/UDP的实现图解: 1.Qt下TCP通信详解: 针对上图进行简单的说明: QTcpServer用来创建服务 ...
- 基于QTcpSocket和QTcpServer的Tcp通讯以及QDataStream序列化数据
最近要在QT下开发Tcp通讯,发送序列化数据以便于接收. 这里涉及到几个问题: 1.QTcpSocket.QTcpServer的通讯 2.QDataStream序列化数据 多的不说,直接上干货!!! ...
- QT5 网络通讯
QT5 TCP网络通讯 服务器与客户端建立连接listen() - connectToHost(); 触发newPendingConnect信号 实时数据通讯write(); read(); 触发 ...
- QT5在VS2013中找不到QtNetwork或QTcpSocket或QTcpSocket等头文件
一.首先是要有相关的库文件 方法一:手动添加库文件Qt5Networkd.lib 对项目进行右键,找到相关的属性,然后查看Linker中input部分的红色选项中是否含有Qt5Networkd.lib ...
- 5.关于QT中的网络编程,QTcpSocket,QUdpSocket
1 新建一个项目:TCPServer.pro A 修改TCPServer.pro,注意:如果是想使用网络库,需要加上network SOURCES += \ TcpServer.cpp \ T ...
- QTcpSocket使用过程中的一些问题记录
目前,在将原来C的socket通讯改为使用Qt类库QTcpSocket通讯,在修改过程中遇到不少问题,在此将问题一并记录,以备后面使用. 采用的通讯方式:QTimer定时器.QThread多线程和QT ...
- Qt NetWork即时通讯网络聊天室(基于TCP)
本文使用QT的网络模块来创建一个网络聊天室程序,主要包括以下功能: 1.基于TCP的可靠连接(QTcpServer.QTcpSocket) 2.一个服务器,多个客户端 3.服务器接收到某个客户端的请求 ...
随机推荐
- asp.net中,<%#%>,<%=%>和<%%>各自是什么意思,有什么差别
在asp.net中经常出现包括这样的形式<%%>的html代码,总的来说包括以下这样几种格式: 一. <%%> 这样的格式实际上就是和asp的使用方法一样的,仅仅是asp中里面 ...
- js进阶 11-8 jquery如何获取元素相对于父元素的位置
js进阶 11-8 jquery如何获取元素相对于父元素的位置 一.总结 一句话总结:用jquery的position方法,但是使用这个方法的前提是父元素相对定位,子元素绝对定位,否则和offset ...
- php标准库spl栈SplStack如何使用?
php标准库spl栈SplStack如何使用? 一.总结 php标准库spl栈SplStack介绍.(SplStack类)(各种方法都支持) 1.SplStack类:$stack = new SplS ...
- Xcode7 不能使用http网络请求问题解决
最近使用Xcode 7.0 写代码,发送网路请求提示: App Transport Security has blocked a cleartext HTTP (http://) resource ...
- Swift API设计原则
注: 本文摘自 Swift API设计指南 一.基本原则 通俗易懂的API是设计者最重要的目标.实体.变量.函数等都具有一次申明.重复使用的性质,所以一个好的API设计,应该能够使用少量的解读和示例就 ...
- spring mybatis circular reference
摘要: Error creating bean with name 'XXX': Requested bean is currently in creation: Is there an unreso ...
- LeetCode: Generate Parentheses [021]
[称号] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...
- python 整数中1出现的次数
把整数转换为字符串 用count计数 # -*- coding:utf- -*- class Solution: def NumberOf1Between1AndN_Solution(self, n) ...
- 4.生产者 消费者模式的RabbitMQ
1.生产者: using RabbitMQ.Client; using System; using System.Text; namespace Publisher1 { class Program ...
- sklearn 文本处理
from sklearn.feature_extraction.text import ** 1. 向量的统计.tf-idf 的计算 考虑如下预料,三行 ⇒ 三个文档,不重复的单词共有 8 个, co ...