tcp传图片名
parent
215acc558b
commit
fc8f54c7d3
@ -0,0 +1,61 @@
|
||||
#include "threadSendTCP.h"
|
||||
#include <WinSock2.h>
|
||||
#include <WS2tcpip.h>
|
||||
#include <stdio.h>
|
||||
#include <qtcpsocket.h>
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
|
||||
void threadSendTCP::init(SyncQueue<_TCPSendInfo>* p_TCP_Info_queue, std::string ip_, int port_) {
|
||||
|
||||
ip = QString::fromStdString(ip_);
|
||||
port = port_;
|
||||
qDebug() << "tcp ip:" << ip << "| tcp port:" << port;
|
||||
Local_TCP_Info_queue = p_TCP_Info_queue;
|
||||
}
|
||||
void threadSendTCP::start_work()
|
||||
{
|
||||
//start(HighestPriority);
|
||||
start();
|
||||
}
|
||||
|
||||
void threadSendTCP::stop()
|
||||
{
|
||||
isLoop = false;
|
||||
// wait();
|
||||
delete mySocket;
|
||||
}
|
||||
|
||||
bool threadSendTCP::connectTCP() {
|
||||
mySocket = new QTcpSocket();
|
||||
// 取消已有的连接
|
||||
mySocket->abort();
|
||||
// 连接服务器
|
||||
mySocket->connectToHost(ip, port);
|
||||
if (!mySocket->waitForConnected(30000)) {
|
||||
qDebug() << "connect failed!";
|
||||
return false;
|
||||
}
|
||||
qDebug() << "connect successfully!";
|
||||
return true;
|
||||
}
|
||||
|
||||
void threadSendTCP::run()
|
||||
{
|
||||
if (!connectTCP())
|
||||
qDebug() << "TCP connect error!";
|
||||
while (isLoop) {
|
||||
_TCPSendInfo TCPSendInfo;
|
||||
Local_TCP_Info_queue->take(TCPSendInfo);
|
||||
num++;
|
||||
sendData(&TCPSendInfo, num);
|
||||
//mySocket->write("Hello! here is tcp client!\n");
|
||||
//mySocket->flush();
|
||||
}
|
||||
}
|
||||
|
||||
void threadSendTCP::sendData(_TCPSendInfo* TCPSendInfo, int Num) {
|
||||
std::string fileName = TCPSendInfo->pics_name + ", " + QString::number(Num).toStdString();
|
||||
mySocket->write(fileName.c_str());
|
||||
mySocket->write("\n");
|
||||
mySocket->flush();
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
#include <QDebug>
|
||||
#include <QDateTime>
|
||||
#include <iostream>
|
||||
#include <QThread>
|
||||
#include <qtcpsocket.h>
|
||||
#include "common.h"
|
||||
#include "SyncQueue.h"
|
||||
|
||||
class _TCPSendInfo
|
||||
{
|
||||
public:
|
||||
std::string pics_name;
|
||||
|
||||
_TCPSendInfo()
|
||||
{
|
||||
pics_name = "";
|
||||
}
|
||||
};
|
||||
|
||||
class threadSendTCP : public QThread {
|
||||
public:
|
||||
QString ip;
|
||||
int port;
|
||||
int num = 0;
|
||||
|
||||
threadSendTCP(QObject* o = nullptr) :QThread(o)
|
||||
{
|
||||
isLoop = true;
|
||||
}
|
||||
|
||||
~threadSendTCP()
|
||||
{
|
||||
stop();
|
||||
_TCPSendInfo TCPSendInfo;
|
||||
Local_TCP_Info_queue->put(TCPSendInfo);
|
||||
quit();
|
||||
wait();
|
||||
}
|
||||
void stop();
|
||||
bool connectTCP();
|
||||
|
||||
protected:
|
||||
void run();
|
||||
|
||||
public:
|
||||
void init(SyncQueue<_TCPSendInfo>* p_TCP_Info_queue, std::string ip_, int port_);
|
||||
void start_work();
|
||||
void sendData(_TCPSendInfo* TCPSendInfo, int Num);
|
||||
|
||||
public:
|
||||
SyncQueue<_TCPSendInfo>* Local_TCP_Info_queue;
|
||||
std::atomic_bool isLoop = { 0 };
|
||||
QTcpSocket *mySocket;
|
||||
};
|
Loading…
Reference in New Issue