程序不能重复打开以及TCP server

CigaretteSH
Flamingo 9 months ago
parent 9dcfd050a6
commit 2284ed6032

@ -8,6 +8,7 @@
//#define __DEBUG //debug信息输出功能 //#define __DEBUG //debug信息输出功能
//#define __UDPSend //网络发送功能 //#define __UDPSend //网络发送功能
//#define __TCPSend // TCP发送 //#define __TCPSend // TCP发送
//#define __TCPServer // TCP服务器
#define __ExportData // FTP发送 #define __ExportData // FTP发送
#define USB_BASLER_NEW_FW //使用basler定制固件 #define USB_BASLER_NEW_FW //使用basler定制固件
//#define IMM_PROCESS //拍照后立马处理,不等校验信号 //#define IMM_PROCESS //拍照后立马处理,不等校验信号

@ -2,6 +2,8 @@
#include <QtWidgets/QApplication> #include <QtWidgets/QApplication>
#include <QPixmap> #include <QPixmap>
#include <QSplashScreen> #include <QSplashScreen>
#include <Windows.h>
#include <qmessagebox.h>
#if defined LICENSE_VERIFY #if defined LICENSE_VERIFY
#pragma comment(lib,"CryptoToolLib.lib") #pragma comment(lib,"CryptoToolLib.lib")
#include "CryptoToolLib.h" #include "CryptoToolLib.h"
@ -9,6 +11,18 @@
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
QApplication a(argc, argv);
HANDLE hMutex = NULL;
hMutex = CreateMutex(nullptr, TRUE, L"CigratteShanghai");
if ((GetLastError() == ERROR_ALREADY_EXISTS) || (hMutex == NULL)) {
QMessageBox::warning(nullptr, "Error", "An instance of the application is already running.");
CloseHandle(hMutex);
hMutex = NULL;
a.closeAllWindows();
return 0;
}
#if defined LICENSE_VERIFY #if defined LICENSE_VERIFY
if (!VerifyLicense()) if (!VerifyLicense())
{ {
@ -16,7 +30,6 @@ int main(int argc, char* argv[])
} }
#endif #endif
qRegisterMetaType<cv::Mat>("cv::Mat"); qRegisterMetaType<cv::Mat>("cv::Mat");
QApplication a(argc, argv);
QPixmap pixmap("D:/Release/splash.jpg"); QPixmap pixmap("D:/Release/splash.jpg");
QSplashScreen splash(pixmap); QSplashScreen splash(pixmap);
splash.show(); splash.show();
@ -25,5 +38,12 @@ int main(int argc, char* argv[])
w.show(); w.show();
//w.showFullScreen(); //w.showFullScreen();
splash.finish(&w); splash.finish(&w);
return a.exec(); a.exec();
if (hMutex != NULL) {
CloseHandle (hMutex);
hMutex = NULL;
}
return 0;
} }

@ -11,6 +11,13 @@ void threadSendTCP::init(SyncQueue<_TCPSendInfo>* p_TCP_Info_queue, std::string
qDebug() << "tcp ip:" << ip << "| tcp port:" << port; qDebug() << "tcp ip:" << ip << "| tcp port:" << port;
Local_TCP_Info_queue = p_TCP_Info_queue; Local_TCP_Info_queue = p_TCP_Info_queue;
#ifdef __TCPServer
tcpServer = new QTcpServer(this);
tcpServer->listen(QHostAddress::Any, port);// Equivalent to QHostAddress("127.0.0.1").
connect(tcpServer,SIGNAL(newConnection()),this,SLOT(onNewConnection()));
connect(mySocket, SIGNAL(connected()), this, SLOT(onClientConnected()));
connect(mySocket, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
#endif
} }
void threadSendTCP::start_work() void threadSendTCP::start_work()
@ -21,13 +28,21 @@ void threadSendTCP::start_work()
void threadSendTCP::stop() void threadSendTCP::stop()
{ {
#ifdef __TCPServer
if (tcpServer->isListening())
tcpServer->close();
if(tcpServer) delete tcpServer;
#endif
isLoop = false; isLoop = false;
// wait(); // wait();
delete mySocket; mySocket->deleteLater();
if (mySocket) delete mySocket;
} }
#ifndef __TCPServer
bool threadSendTCP::connectTCP() { bool threadSendTCP::connectTCP() {
mySocket = new QTcpSocket(); mySocket = new QTcpSocket();
//mySocket = new QTcpServer();
// 取消已有的连接 // 取消已有的连接
mySocket->abort(); mySocket->abort();
// 连接服务器 // 连接服务器
@ -39,26 +54,61 @@ bool threadSendTCP::connectTCP() {
qDebug() << "connect successfully!"; qDebug() << "connect successfully!";
return true; return true;
} }
#endif
void threadSendTCP::run() void threadSendTCP::run()
{ {
#ifndef __TCPServer
if (!connectTCP()) if (!connectTCP())
{ {
qDebug() << "TCP connect error!"; qDebug() << "TCP connect error!";
} }
#endif
while (isLoop) { while (isLoop) {
_TCPSendInfo TCPSendInfo; _TCPSendInfo TCPSendInfo;
Local_TCP_Info_queue->take(TCPSendInfo); Local_TCP_Info_queue->take(TCPSendInfo);
num++; num++;
sendData(&TCPSendInfo, num); #ifdef __TCPServer
if (ClientStatus == QAbstractSocket::ConnectedState)
{
sendData(&TCPSendInfo, num);
}
#else
sendData(&TCPSendInfo, num);
#endif
//mySocket->write("Hello! here is tcp client!\n"); //mySocket->write("Hello! here is tcp client!\n");
//mySocket->flush(); //mySocket->flush();
} }
} }
void threadSendTCP::sendData(_TCPSendInfo* TCPSendInfo, int Num) { void threadSendTCP::sendData(_TCPSendInfo* TCPSendInfo, int Num) {
std::string fileName = TCPSendInfo->pics_name + ", " + QString::number(Num).toStdString(); //std::string fileName = TCPSendInfo->pics_name + ", " + QString::number(Num).toStdString();
mySocket->write(fileName.c_str()); //mySocket->write(fileName.c_str());
mySocket->write("\n"); char temp = num % 10;
mySocket->write((char*)&temp,sizeof(char));
//mySocket->write("\n");
mySocket->flush(); mySocket->flush();
} }
#ifdef __TCPServer
void threadSendTCP::onNewConnection()
{
mySocket = tcpServer->nextPendingConnection(); //创建socket
qDebug() << "NewConnectionConnected";
ClientStatus = QAbstractSocket::ConnectedState;
}
void threadSendTCP::onClientConnected()
{
qDebug() << "ClientConnected";
}
void threadSendTCP::onClientDisconnected()
{
ClientStatus = QAbstractSocket::UnconnectedState;
qDebug() << "ClientDisconnected";
mySocket->deleteLater();
}
#endif

@ -1,4 +1,5 @@
#pragma once #pragma once
#include <QWidget>
#include <QDebug> #include <QDebug>
#include <QDateTime> #include <QDateTime>
#include <iostream> #include <iostream>
@ -6,6 +7,7 @@
#include <qtcpsocket.h> #include <qtcpsocket.h>
#include "common.h" #include "common.h"
#include "SyncQueue.h" #include "SyncQueue.h"
#include <QTcpServer>
class _TCPSendInfo class _TCPSendInfo
{ {
@ -43,8 +45,9 @@ public:
wait(); wait();
} }
void stop(); void stop();
#ifndef __TCPServer
bool connectTCP(); bool connectTCP();
#endif
protected: protected:
void run(); void run();
@ -52,10 +55,18 @@ public:
void init(SyncQueue<_TCPSendInfo>* p_TCP_Info_queue, std::string ip_, int port_); void init(SyncQueue<_TCPSendInfo>* p_TCP_Info_queue, std::string ip_, int port_);
void start_work(); void start_work();
void sendData(_TCPSendInfo* TCPSendInfo, int Num); void sendData(_TCPSendInfo* TCPSendInfo, int Num);
#ifdef __TCPServer
private slots:
void onNewConnection();
void onClientConnected();
void onClientDisconnected();
#endif
public: public:
SyncQueue<_TCPSendInfo>* Local_TCP_Info_queue; SyncQueue<_TCPSendInfo>* Local_TCP_Info_queue;
std::atomic_bool isLoop = { 0 }; std::atomic_bool isLoop = { 0 };
QTcpSocket* mySocket; QTcpSocket* mySocket = NULL;
#ifdef __TCPServer
QTcpServer *tcpServer = NULL;
QAbstractSocket::SocketState ClientStatus;
#endif
}; };

Loading…
Cancel
Save