Home > Back-end >  How to write a multithreaded DLL using qt?
How to write a multithreaded DLL using qt?

Time:09-28


Recently I'm learning the socket programming, want to write a DLL, call to communicate with the server will be finished, but don't know why, to complete the task of threads run in a DLL is abnormal exit, below is my code:
Aclient. H
 
# # ifndef ACLIENT_H
# define ACLIENT_H

# include & lt; QObject>
# include & lt; QHostAddress>
# include & lt; QTcpSocket>

The class AClient: public QObject
{
Q_OBJECT
Public:
Explicit AClient (parent QObject *=0);
//thread startup initialization
Bool start ();
//set the server IP
Void setIP_Address QString (IP);
//set the server port
Void setPort (quint16 port);
//break line reconnection
Bool ReConnectOnLoss (QHostAddress address);
//send the string
Qint64 writeString (QString STR);
//send bytes
Qint64 writeByteArray QByteArray (block);
Private:
//server IP
QString IP_ADDRESS;
//server port
Quint16 PORT;
//socket
QTcpSocket * socket;

};

# endif//ACLIENT_H

Athreah. H
 
# # ifndef ATHREAD_H
# define ATHREAD_H

# include & lt; QThread>
# include "aclient. H"

The class AThread: public QThread
{
Q_OBJECT
Public:
AThread (quint16 QString IP, port);
~ AThread ();
//send the string
Qint64 writeStr QString (STR);
//send bytes
Qint64 writeByteArray QByteArray (block);
Protected:
void run();
Private:
//is responsible for the communication of receiving and sending

AClient * myclient;
//the server IP
QString IP_Address;
//the server port
Quint16 Port;

};

# endif//ATHREAD_H


Dllaurorasocket. H
 
# # ifndef DLLAURORASOCKET_H
# define DLLAURORASOCKET_H

# include "mythread. H"
Extern "C"
{
Q_DECL_EXPORT IAuroraSocket * createSocketThread ();
Q_DECL_EXPORT void RealeaseThread (IAuroraSocket * asocket);
}

# endif//DLLAURORASOCKET_H


Interface
Iaurorasocket. H
 
# # ifndef IAURORASOCKET_H
# define IAURORASOCKET_H

# include "iaurorasocket_global. H"

The class IAURORASOCKETSHARED_EXPORT IAuroraSocket
{

Public:
IAuroraSocket ();
Virtual ~ IAuroraSocket ();

Virtual void dosomething ()=0;
};

# endif//IAURORASOCKET_H


Interface implementation
Mythread. H
 
# # ifndef MYTHREAD_H
# define MYTHREAD_H
# include "iaurorasocket. H"
# include & lt; QDebug>
# include & lt; QObject>
# include & lt; Athread. H>
The class MyThread: public IAuroraSocket, public QObject
{

Public:
MyThread ();
Virtual ~ MyThread ();
Virtual void dosomething ();
//private:
AThread * AThread;
int i;
};

# endif//MYTHREAD_H


Mythread. CPP
 
# include "mythread. H"

MyThread: : MyThread ()
{
//this - & gt; Athread=new athread (" 192.168.1.17 ", 2048);
//this - & gt; Athread - & gt; start();
i=0;
}
MyThread: : ~ MyThread ()
{
}
Void MyThread: : dosomething () {
//i++;
//qDebug () & lt; & lt;" 1234 ";
This - & gt; Athread=new athread (" 192.168.1.17 ", 2048);
This - & gt; Athread - & gt; start();
}



Using DLL
 
QLibrary lib (" D: \ \ Codes \ \ qt \ \ build - DllAuroraSocket - Desktop_Qt_5_3_MinGW_32bit - Debug \ \ Debug \ \ DllAuroraSocket DLL ");
If (lib) the load ())
{
QDebug () & lt; & lt;" Load success ";
//define two export function prototype of plug-in
CreateSocketFunction typedef IAuroraSocket * (*) ();
Typedef void (* ReleaseSocketFunction) (IAuroraSocket * mysocket);

//parsing export function
CreateSocketFunction createASocket=
(CreateSocketFunction) lib. Resolve (" createSocketThread ");
ReleaseSocketFunction releaseASocket=
(ReleaseSocketFunction) lib. Resolve (" RealeaseThread ");

If (createASocket & amp; & ReleaseASocket)
{
QDebug () & lt; & lt;" Export success ";
//create Animal object
IAuroraSocket * ASocket=createASocket ();
//IAuroraSocket * ASocket1=createASocket ();
If (ASocket)
{
//use plug-in feature
ASocket - & gt; Dosomething ();

//the plugin to use, delete
//releaseASocket (ASocket);
}
}
The else
{
QDebug () & lt; & lt;" Export failure ";
}
//uninstall plugin
Lib. Unload ();
}
The else
{
QDebug () & lt; & lt;" Loading failure ";
}



Excuse me, how do I modify, implementation within the DLL using the socket multithreaded programming?
  • Related