Qt 网络协议——大鸟勿进啦
主要原理:socket通信 转http://www.cnblogs.com/dolphinX/p/3460545.html,这篇博客有简单的socket介绍
主要实现:实现非阻塞式的socket
两种方法:
1.利用winsock 类库中的 Socket协议,使用select()函数,但是我没搞太懂,之前使用创建线程实现了accept()的非阻塞连接,
但是,感觉太麻烦了,所以中途放弃了
2.利用qtcpsocket qtcpsocket 类库中的协议,这里边非常简单就可以实现非阻塞Socket,主要是通过connect()建立槽,我是这么理解的
转http://blog.csdn.net/ac_huang/article/details/23870323,这篇博客中有介绍
附上我自己的源码,仅仅实现了连接,发送接收数据:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <qtcpsocket.h>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void initClient();
private slots:
void disconnectedSlot();
void readyReadSlot();
void on_pushButton_clicked();
private:
Ui::Widget *ui;
QTcpSocket * m_tcpSocket;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::initClient()
{
m_tcpSocket = new QTcpSocket(this);
QString ipAddress = tr("192.168.13.14");
quint16 port = 6000;
m_tcpSocket->connectToHost("192.168.13.14",port,QTcpSocket::ReadWrite);
connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
connect(m_tcpSocket, SIGNAL(disconnected()), this, SLOT(disconnectedSlot()));
}
void Widget::disconnectedSlot()
{
m_tcpSocket->close();
}
void Widget::on_pushButton_clicked()
{
QString str = ui->textEdit->toPlainText();
m_tcpSocket->write(str.toLatin1().data(), str.length());
}
void Widget::readyReadSlot()
{
QTcpSocket * newtcpsocket = static_cast<QTcpSocket*>(this->sender());
QByteArray message = newtcpsocket->readAll();
ui->textEdit_2->append(message);
}
主要原理:socket通信 转http://www.cnblogs.com/dolphinX/p/3460545.html,这篇博客有简单的socket介绍
主要实现:实现非阻塞式的socket
两种方法:
1.利用winsock 类库中的 Socket协议,使用select()函数,但是我没搞太懂,之前使用创建线程实现了accept()的非阻塞连接,
但是,感觉太麻烦了,所以中途放弃了
2.利用qtcpsocket qtcpsocket 类库中的协议,这里边非常简单就可以实现非阻塞Socket,主要是通过connect()建立槽,我是这么理解的
转http://blog.csdn.net/ac_huang/article/details/23870323,这篇博客中有介绍
附上我自己的源码,仅仅实现了连接,发送接收数据:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <qtcpsocket.h>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void initClient();
private slots:
void disconnectedSlot();
void readyReadSlot();
void on_pushButton_clicked();
private:
Ui::Widget *ui;
QTcpSocket * m_tcpSocket;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::initClient()
{
m_tcpSocket = new QTcpSocket(this);
QString ipAddress = tr("192.168.13.14");
quint16 port = 6000;
m_tcpSocket->connectToHost("192.168.13.14",port,QTcpSocket::ReadWrite);
connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
connect(m_tcpSocket, SIGNAL(disconnected()), this, SLOT(disconnectedSlot()));
}
void Widget::disconnectedSlot()
{
m_tcpSocket->close();
}
void Widget::on_pushButton_clicked()
{
QString str = ui->textEdit->toPlainText();
m_tcpSocket->write(str.toLatin1().data(), str.length());
}
void Widget::readyReadSlot()
{
QTcpSocket * newtcpsocket = static_cast<QTcpSocket*>(this->sender());
QByteArray message = newtcpsocket->readAll();
ui->textEdit_2->append(message);
}
