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

CigaretteSH
Flamingo 9 months ago
parent 9dcfd050a6
commit 2284ed6032

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

@ -2,6 +2,8 @@
#include <QtWidgets/QApplication>
#include <QPixmap>
#include <QSplashScreen>
#include <Windows.h>
#include <qmessagebox.h>
#if defined LICENSE_VERIFY
#pragma comment(lib,"CryptoToolLib.lib")
#include "CryptoToolLib.h"
@ -9,6 +11,18 @@
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 (!VerifyLicense())
{
@ -16,7 +30,6 @@ int main(int argc, char* argv[])
}
#endif
qRegisterMetaType<cv::Mat>("cv::Mat");
QApplication a(argc, argv);
QPixmap pixmap("D:/Release/splash.jpg");
QSplashScreen splash(pixmap);
splash.show();
@ -25,5 +38,12 @@ int main(int argc, char* argv[])
w.show();
//w.showFullScreen();
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;
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()
@ -21,13 +28,21 @@ void threadSendTCP::start_work()
void threadSendTCP::stop()
{
#ifdef __TCPServer
if (tcpServer->isListening())
tcpServer->close();
if(tcpServer) delete tcpServer;
#endif
isLoop = false;
// wait();
delete mySocket;
mySocket->deleteLater();
if (mySocket) delete mySocket;
}
#ifndef __TCPServer
bool threadSendTCP::connectTCP() {
mySocket = new QTcpSocket();
//mySocket = new QTcpServer();
// 取消已有的连接
mySocket->abort();
// 连接服务器
@ -39,26 +54,61 @@ bool threadSendTCP::connectTCP() {
qDebug() << "connect successfully!";
return true;
}
#endif
void threadSendTCP::run()
{
#ifndef __TCPServer
if (!connectTCP())
{
qDebug() << "TCP connect error!";
}
#endif
while (isLoop) {
_TCPSendInfo TCPSendInfo;
Local_TCP_Info_queue->take(TCPSendInfo);
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->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");
//std::string fileName = TCPSendInfo->pics_name + ", " + QString::number(Num).toStdString();
//mySocket->write(fileName.c_str());
char temp = num % 10;
mySocket->write((char*)&temp,sizeof(char));
//mySocket->write("\n");
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
#include <QWidget>
#include <QDebug>
#include <QDateTime>
#include <iostream>
@ -6,6 +7,7 @@
#include <qtcpsocket.h>
#include "common.h"
#include "SyncQueue.h"
#include <QTcpServer>
class _TCPSendInfo
{
@ -43,8 +45,9 @@ public:
wait();
}
void stop();
#ifndef __TCPServer
bool connectTCP();
#endif
protected:
void run();
@ -52,10 +55,18 @@ public:
void init(SyncQueue<_TCPSendInfo>* p_TCP_Info_queue, std::string ip_, int port_);
void start_work();
void sendData(_TCPSendInfo* TCPSendInfo, int Num);
#ifdef __TCPServer
private slots:
void onNewConnection();
void onClientConnected();
void onClientDisconnected();
#endif
public:
SyncQueue<_TCPSendInfo>* Local_TCP_Info_queue;
std::atomic_bool isLoop = { 0 };
QTcpSocket* mySocket;
QTcpSocket* mySocket = NULL;
#ifdef __TCPServer
QTcpServer *tcpServer = NULL;
QAbstractSocket::SocketState ClientStatus;
#endif
};

Loading…
Cancel
Save