void TCPSocket::onReady(){ while(socket->bytesAvailable()){ QByteArray data=socket->readAll(); qDebug()<<"Данные"; }}void TCPSocket::run(){ connect(socket,SIGNAL(disconnected()),this,SLOT(_disconnected())); connect(socket,SIGNAL(readyRead()),this,SLOT(onReady())); exec();}
C++ (Qt)while(!asleep()) sheep++;
C++ (Qt)#include "tcpsocket.h" TCPSocket::TCPSocket(QObject *parent) :QThread(parent){} bool TCPSocket::connectTo(QString hostName, int port){ socket = new QTcpSocket(); connect(socket,SIGNAL(disconnected()),this,SLOT(onDisconnect())); socket->connectToHost(hostName, port); if (!socket->waitForConnected(5000)) { emit error(socket->errorString()); return false; }else{ if(!isRunning()) start(); return true; }} void TCPSocket::killConnection(){ socket->disconnectFromHost();} void TCPSocket::sendData(const QByteArray &packet){ socket->write(packet,packet.length()); socket->flush();} void TCPSocket::onDataReceive(){ while(socket->bytesAvailable()){ QByteArray data=socket->readAll(); qDebug()<<"Данные"; }} void TCPSocket::onDisconnect(){ emit disconnected(); exit(0);} void TCPSocket::run(){ QTcpSocket tcpSocket; connect(&tcpSocket,SIGNAL(readyRead()), this, SLOT(onDataReceive())); if (!tcpSocket.setSocketDescriptor(socket->socketDescriptor())) { emit error(tcpSocket.errorString()); return; } exec();}
C++ (Qt)#ifndef MAINWINDOW_H#define MAINWINDOW_H #include <QMainWindow>#include <tcpsocket.h>//#include <hubsession.h> namespace Ui {class MainWindow;} class MainWindow : public QMainWindow{ Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow();signals: void sendData(const QByteArray &packet);private slots: void on_connectButton_clicked(); void on_sendButton_clicked(); void disconnected(); void connected(); void error(QString error); void on_pushButton_clicked();private: Ui::MainWindow *ui; TCPSocket *socket;}; #endif // MAINWINDOW_H
C++ (Qt)#include "mainwindow.h"#include "ui_mainwindow.h"#include "QMessageBox" MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow){ ui->setupUi(this); socket = new TCPSocket(); connect(socket,SIGNAL(connected()),this,SLOT(connected())); connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected())); connect(socket,SIGNAL(error(QString)),this,SLOT(error(QString))); connect(this,SIGNAL(sendData(QByteArray)),socket,SLOT(sendData(QByteArray)));} void MainWindow::disconnected(){ QMessageBox::information(this,"Внимание","Отключились");} void MainWindow::connected(){ QMessageBox::information(this,"Внимание","Подключились!");} void MainWindow::error(QString error){ QMessageBox::information(this,"Внимание",error);} MainWindow::~MainWindow(){ delete ui;} void MainWindow::on_connectButton_clicked(){ socket->connectTo("192.168.0.3",3478);} void MainWindow::on_sendButton_clicked(){ emit sendData(ui->textToSend->toPlainText().toUtf8().data());} void MainWindow::on_pushButton_clicked(){ socket->killConnection();}
C++ (Qt)#ifndef TCPSOCKET_H#define TCPSOCKET_H #include <QThread>#include <QtNetwork> typedef unsigned char BYTE; class TCPSocket : public QThread{ Q_OBJECTpublic: TCPSocket(QObject *parent = 0); void run(); void connectTo(QString hostName, int port); void killConnection();private: QTcpSocket *socket; QString hostName; int port;private slots: void onDataReceive(); void onDisconnect();public slots: void sendData(const QByteArray &packet);signals: void disconnected(); void connected(); void error(QString error);}; #endif // TCPSOCKET_H
C++ (Qt)#include "tcpsocket.h" TCPSocket::TCPSocket(QObject *parent) :QThread(parent){} void TCPSocket::connectTo(QString hostName, int port){ this->hostName=hostName; this->port=port; if(!isRunning()) start();} void TCPSocket::killConnection(){ socket->disconnectFromHost();} void TCPSocket::sendData(const QByteArray &packet){ socket->write(packet,packet.length()); socket->flush();} void TCPSocket::onDataReceive(){ while(socket->bytesAvailable()){ QByteArray data=socket->readAll(); qDebug()<<"Данные"; }} void TCPSocket::onDisconnect(){ emit disconnected(); exit(0);} void TCPSocket::run(){ socket = new QTcpSocket(); connect(socket,SIGNAL(readyRead()),this,SLOT(onDataReceive())); connect(socket,SIGNAL(disconnected()),this,SLOT(onDisconnect())); socket->connectToHost(hostName, port); if (!socket->waitForConnected(5000)) { emit error(socket->errorString()); return; } emit connected(); exec();}
C++ (Qt)TCPSocket::TCPSocket(QObject *parent) :QThread(parent){ moveToThread( this );}