Qt之QTcpServer/QTcpSocket简单收发信息(1)
2016-09-27 14:24
344 查看
用QT包装好的东西做socket类东西,我只能说啥这么简单呢。
waitForConnected() 等待链接的建立
waitForReadyRead() 等待新数据的到来
waitForBytesWritten() 等待数据写入socket
waitForDisconnected() 等待链接断开
开始前在项目.pro里面添加如下:
QT += core guinetwork用QT包装好的东西做socket类东西,我只能说啥这么简单呢。
waitForConnected() 等待链接的建立
waitForReadyRead() 等待新数据的到来
waitForBytesWritten() 等待数据写入socket
waitForDisconnected() 等待链接断开
开始前在项目.pro里面添加如下:
QT += core guinetwork
=========server==============
[cpp] view plaincopyprint?
#include "testnet.h"
#include "ui_testnet.h"
#include <QtGui>
Testnet::Testnet(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Testnet)
{
ui->setupUi(this);
this->connect(ui->pushButton_start,SIGNAL(clicked()),this,SLOT(startTcpserver()));
this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMessage()));
}
Testnet::~Testnet()
{
delete ui;
}
void Testnet::startTcpserver()
{
m_tcpServer = new QTcpServer(this);
m_tcpServer->listen(QHostAddress::Any,19999); //监听任何连上19999端口的ip
connect(m_tcpServer,SIGNAL(newConnection()),this,SLOT(newConnect())); //新连接信号触发,调用newConnect()槽函数,这个跟信号函数一样,其实你可以随便取。
}
void Testnet::newConnect()
{
m_tcpSocket = m_tcpServer->nextPendingConnection(); //得到每个连进来的socket
connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMessage())); //有可读的信息,触发读函数槽
}
void Testnet::readMessage() //读取信息
{
// ui->textEdit_rec->te
QByteArray qba= m_tcpSocket->readAll(); //读取
qDebug()<<qba;
QString ss=QVariant(qba).toString();
ui->textEdit_rec->setText(ss);
}
void Testnet::sendMessage() //发送信息
{
QString strMesg= ui->lineEdit_sendmessage->text();
qDebug()<<strMesg;
m_tcpSocket->write(strMesg.toStdString().c_str(),strlen(strMesg.toStdString().c_str())); //发送
}
=======client========
[cpp] view plaincopyprint?
#include "testnet_c.h"
#include "ui_testnet_c.h"
testnet_c::testnet_c(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::testnet_c)
{
ui->setupUi(this);
this->connect(ui->pushButton_con,SIGNAL(clicked()),this,SLOT(connectServer()));
this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMesg()));
}
testnet_c::~testnet_c()
{
delete ui;
}
void testnet_c::connectServer()
{
m_tcpSocket = new QTcpSocket(this);
m_tcpSocket->abort();
m_tcpSocket->connectToHost("192.168.1.178",19999);
connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMesg()));
}
void testnet_c::readMesg() //读取信息
{
QByteArray qba = m_tcpSocket->readAll();
ui->textEdit_recmesg->clear();
qDebug()<<qba;
QString ss=QVariant(qba).toString();
ui->textEdit_recmesg->setText(ss);
}
void testnet_c::sendMesg() //发送信息
{
QString ss= ui->lineEdit_sendmesg->text();
m_tcpSocket->write(ss.toStdString().c_str(),strlen(ss.toStdString().c_str()));
ui->lineEdit_sendmesg->clear();
}
实验结果如下:
=========server==============
[cpp] view plaincopyprint?
#include "testnet.h"
#include "ui_testnet.h"
#include <QtGui>
Testnet::Testnet(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Testnet)
{
ui->setupUi(this);
this->connect(ui->pushButton_start,SIGNAL(clicked()),this,SLOT(startTcpserver()));
this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMessage()));
}
Testnet::~Testnet()
{
delete ui;
}
void Testnet::startTcpserver()
{
m_tcpServer = new QTcpServer(this);
m_tcpServer->listen(QHostAddress::Any,19999); //监听任何连上19999端口的ip
connect(m_tcpServer,SIGNAL(newConnection()),this,SLOT(newConnect())); //新连接信号触发,调用newConnect()槽函数,这个跟信号函数一样,其实你可以随便取。
}
void Testnet::newConnect()
{
m_tcpSocket = m_tcpServer->nextPendingConnection(); //得到每个连进来的socket
connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMessage())); //有可读的信息,触发读函数槽
}
void Testnet::readMessage() //读取信息
{
// ui->textEdit_rec->te
QByteArray qba= m_tcpSocket->readAll(); //读取
qDebug()<<qba;
QString ss=QVariant(qba).toString();
ui->textEdit_rec->setText(ss);
}
void Testnet::sendMessage() //发送信息
{
QString strMesg= ui->lineEdit_sendmessage->text();
qDebug()<<strMesg;
m_tcpSocket->write(strMesg.toStdString().c_str(),strlen(strMesg.toStdString().c_str())); //发送
}
=======client========
[cpp] view plaincopyprint?
#include "testnet_c.h"
#include "ui_testnet_c.h"
testnet_c::testnet_c(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::testnet_c)
{
ui->setupUi(this);
this->connect(ui->pushButton_con,SIGNAL(clicked()),this,SLOT(connectServer()));
this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMesg()));
}
testnet_c::~testnet_c()
{
delete ui;
}
void testnet_c::connectServer()
{
m_tcpSocket = new QTcpSocket(this);
m_tcpSocket->abort();
m_tcpSocket->connectToHost("192.168.1.178",19999);
connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMesg()));
}
void testnet_c::readMesg() //读取信息
{
QByteArray qba = m_tcpSocket->readAll();
ui->textEdit_recmesg->clear();
qDebug()<<qba;
QString ss=QVariant(qba).toString();
ui->textEdit_recmesg->setText(ss);
}
void testnet_c::sendMesg() //发送信息
{
QString ss= ui->lineEdit_sendmesg->text();
m_tcpSocket->write(ss.toStdString().c_str(),strlen(ss.toStdString().c_str()));
ui->lineEdit_sendmesg->clear();
}
实验结果如下:
waitForConnected() 等待链接的建立
waitForReadyRead() 等待新数据的到来
waitForBytesWritten() 等待数据写入socket
waitForDisconnected() 等待链接断开
开始前在项目.pro里面添加如下:
QT += core guinetwork用QT包装好的东西做socket类东西,我只能说啥这么简单呢。
waitForConnected() 等待链接的建立
waitForReadyRead() 等待新数据的到来
waitForBytesWritten() 等待数据写入socket
waitForDisconnected() 等待链接断开
开始前在项目.pro里面添加如下:
QT += core guinetwork
=========server==============
[cpp] view plaincopyprint?
#include "testnet.h"
#include "ui_testnet.h"
#include <QtGui>
Testnet::Testnet(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Testnet)
{
ui->setupUi(this);
this->connect(ui->pushButton_start,SIGNAL(clicked()),this,SLOT(startTcpserver()));
this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMessage()));
}
Testnet::~Testnet()
{
delete ui;
}
void Testnet::startTcpserver()
{
m_tcpServer = new QTcpServer(this);
m_tcpServer->listen(QHostAddress::Any,19999); //监听任何连上19999端口的ip
connect(m_tcpServer,SIGNAL(newConnection()),this,SLOT(newConnect())); //新连接信号触发,调用newConnect()槽函数,这个跟信号函数一样,其实你可以随便取。
}
void Testnet::newConnect()
{
m_tcpSocket = m_tcpServer->nextPendingConnection(); //得到每个连进来的socket
connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMessage())); //有可读的信息,触发读函数槽
}
void Testnet::readMessage() //读取信息
{
// ui->textEdit_rec->te
QByteArray qba= m_tcpSocket->readAll(); //读取
qDebug()<<qba;
QString ss=QVariant(qba).toString();
ui->textEdit_rec->setText(ss);
}
void Testnet::sendMessage() //发送信息
{
QString strMesg= ui->lineEdit_sendmessage->text();
qDebug()<<strMesg;
m_tcpSocket->write(strMesg.toStdString().c_str(),strlen(strMesg.toStdString().c_str())); //发送
}
=======client========
[cpp] view plaincopyprint?
#include "testnet_c.h"
#include "ui_testnet_c.h"
testnet_c::testnet_c(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::testnet_c)
{
ui->setupUi(this);
this->connect(ui->pushButton_con,SIGNAL(clicked()),this,SLOT(connectServer()));
this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMesg()));
}
testnet_c::~testnet_c()
{
delete ui;
}
void testnet_c::connectServer()
{
m_tcpSocket = new QTcpSocket(this);
m_tcpSocket->abort();
m_tcpSocket->connectToHost("192.168.1.178",19999);
connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMesg()));
}
void testnet_c::readMesg() //读取信息
{
QByteArray qba = m_tcpSocket->readAll();
ui->textEdit_recmesg->clear();
qDebug()<<qba;
QString ss=QVariant(qba).toString();
ui->textEdit_recmesg->setText(ss);
}
void testnet_c::sendMesg() //发送信息
{
QString ss= ui->lineEdit_sendmesg->text();
m_tcpSocket->write(ss.toStdString().c_str(),strlen(ss.toStdString().c_str()));
ui->lineEdit_sendmesg->clear();
}
实验结果如下:
=========server==============
[cpp] view plaincopyprint?
#include "testnet.h"
#include "ui_testnet.h"
#include <QtGui>
Testnet::Testnet(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Testnet)
{
ui->setupUi(this);
this->connect(ui->pushButton_start,SIGNAL(clicked()),this,SLOT(startTcpserver()));
this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMessage()));
}
Testnet::~Testnet()
{
delete ui;
}
void Testnet::startTcpserver()
{
m_tcpServer = new QTcpServer(this);
m_tcpServer->listen(QHostAddress::Any,19999); //监听任何连上19999端口的ip
connect(m_tcpServer,SIGNAL(newConnection()),this,SLOT(newConnect())); //新连接信号触发,调用newConnect()槽函数,这个跟信号函数一样,其实你可以随便取。
}
void Testnet::newConnect()
{
m_tcpSocket = m_tcpServer->nextPendingConnection(); //得到每个连进来的socket
connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMessage())); //有可读的信息,触发读函数槽
}
void Testnet::readMessage() //读取信息
{
// ui->textEdit_rec->te
QByteArray qba= m_tcpSocket->readAll(); //读取
qDebug()<<qba;
QString ss=QVariant(qba).toString();
ui->textEdit_rec->setText(ss);
}
void Testnet::sendMessage() //发送信息
{
QString strMesg= ui->lineEdit_sendmessage->text();
qDebug()<<strMesg;
m_tcpSocket->write(strMesg.toStdString().c_str(),strlen(strMesg.toStdString().c_str())); //发送
}
=======client========
[cpp] view plaincopyprint?
#include "testnet_c.h"
#include "ui_testnet_c.h"
testnet_c::testnet_c(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::testnet_c)
{
ui->setupUi(this);
this->connect(ui->pushButton_con,SIGNAL(clicked()),this,SLOT(connectServer()));
this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMesg()));
}
testnet_c::~testnet_c()
{
delete ui;
}
void testnet_c::connectServer()
{
m_tcpSocket = new QTcpSocket(this);
m_tcpSocket->abort();
m_tcpSocket->connectToHost("192.168.1.178",19999);
connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMesg()));
}
void testnet_c::readMesg() //读取信息
{
QByteArray qba = m_tcpSocket->readAll();
ui->textEdit_recmesg->clear();
qDebug()<<qba;
QString ss=QVariant(qba).toString();
ui->textEdit_recmesg->setText(ss);
}
void testnet_c::sendMesg() //发送信息
{
QString ss= ui->lineEdit_sendmesg->text();
m_tcpSocket->write(ss.toStdString().c_str(),strlen(ss.toStdString().c_str()));
ui->lineEdit_sendmesg->clear();
}
实验结果如下:
相关文章推荐
- Qt之QTcpServer/QTcpSocket简单收发信息(1)
- Qt之QTcpServer/QTcpSocket简单收发信息(1)
- Qt之QTcpServer/QTcpSocket简单收发信息(2)
- Qt之QTcpServer/QTcpSocket简单收发信息(1)
- Qt之QTcpServer/QTcpSocket简单收发信息
- 运用Java中socket编程实现简单的服务器客户端收发信息程序
- Qt之QTcpServer/QTcpSocket简单收发信息
- Qt之QTcpServer/QTcpSocket简单收发信息(1)
- Qt之QTcpServer/QTcpSocket简单收发信息(2)
- socket收发简单示例
- Silverlight Socket 实现收发信息
- 基于linux socket 简单学生信息管理系统server程序
- [转贴]Silverlight Socket 实现收发信息
- Socket在阻塞模式下的信息收发和文件接收
- 通过Socket实现简单web服务器功能(接收页面请求信息)不使用Tomcat!
- Python实现socket信息发送和监听的简单代码示例
- Silverlight Socket 实现收发信息
- Silverlight Socket 实现收发信息
- Silverlight Socket 实现收发信息
- 简单socket 文件收发(tcp)