" cannot send events to objects owned by another thread". exception in Qt visual studio 2019
-
I have made a client and a server. In server I have done multi threading. Now I am making a UI for my server program using Qt in visual studio 2019. But now here in my server code after running it when I run my client the server program gives me an exception " cannot send events to objects owned by another thread". I want to make a different thread in server for handling all the clients and my main thread will handle the GUI since the GUI freezes so I need a different thread to handle my clients. I have attached my server and client code below . can anyone tell me what I am doing wrong here?
server2.h
#pragma once #include <QtWidgets/QMainWindow> #include "ui_server2.h" #include <iostream> #include <WS2tcpip.h> #include <string> #include<sstream> #include<WinSock2.h> #include <cstdlib> #include <fstream> class server2 : public QMainWindow { Q_OBJECT public: server2(QWidget *parent = Q_NULLPTR); static DWORD WINAPI receive_req(LPVOID lpParam); void handle_gui(SOCKET &listening); private slots: void on_connect_clicked(); private: Ui::server2Class ui; };
server2.cpp
#include "server2.h" #include "ui_server2.h" #pragma once #include <iostream> #include <WS2tcpip.h> #include <string> #include<sstream> #include<WinSock2.h> #include <cstdlib> #include <fstream> #include<thread> #pragma comment (lib, "ws2_32.lib") QString sqt; server2::server2(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); } DWORD WINAPI server2::receive_req(LPVOID lpParam) { std::string input; input = "<Trancode> msgcode"; SOCKET clientsock = (SOCKET)lpParam; char buf[4096]; while (true) { ZeroMemory(buf, 4096); int recbyte = recv(clientsock, buf, 4096, 0); std::string str = std::string(buf, 0, recbyte); QString qstr = QString::fromStdString(str); sqt += qstr; sqt += '\n'; //ui.label->append(sqt); if (recbyte == 0 || std::string(buf, 0, recbyte) == "") { //cout << "Client disconnected " << endl; break; } if (recbyte == SOCKET_ERROR) { //cerr << "Error in recv(). Quitting" << endl; break; } send(clientsock, input.c_str(), input.size() + 1, 0); // } closesocket(clientsock); ExitThread(0); } void server2::handle_gui(SOCKET &listening) { int cnt = 0; DWORD threadd; SOCKET clientsock; sockaddr_in from; int fromlen = sizeof(from); while (cnt != 3) { clientsock = accept(listening, (struct sockaddr*)&from, &fromlen); /*char buf[4096]; int recbyte = recv(clientsock, buf, 4096, 0); std::string str = std::string(buf, 0, recbyte); QString qstr = QString::fromStdString(str);*/ ui.label->setText("hello world"); cnt++; // created a recv_cmds thread CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)server2::receive_req, (LPVOID)clientsock, 0, &threadd); ui.label->setText(sqt); } } void server2::on_connect_clicked() { //ui.label->setText("hello world"); WSADATA data; WORD ver = MAKEWORD(2, 2); int wres = WSAStartup(ver, &data); if (wres != 0) { //cerr << "unable to initialize winsock" << endl; return; } SOCKET listening = socket(AF_INET, SOCK_STREAM, 0); if (listening == INVALID_SOCKET) { //cerr << "unable to create a socket" << endl; return; } sockaddr_in sdata; sdata.sin_family = AF_INET; sdata.sin_port = htons(54000); sdata.sin_addr.S_un.S_addr = INADDR_ANY; bind(listening, (sockaddr*)&sdata, sizeof(sdata)); listen(listening, SOMAXCONN); server2 ser; std::thread th1(&server2::handle_gui,&ser,std::ref(listening)); ui.label->append(sqt); th1.join(); //function gui closesocket(listening); //closesocket(clientsock); WSACleanup(); system("pause"); }
client.cpp
#include <iostream> #include <string> #include <WS2tcpip.h> #pragma comment(lib, "ws2_32.lib") using namespace std; void main() { string ipadd = "127.0.0.1"; int port = 54000; WSAData wd; WORD ver = MAKEWORD(2, 2); int wres = WSAStartup(ver, &wd); if (wres != 0) { cerr << "Winsock Error" << wres << endl; return; } SOCKET sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == INVALID_SOCKET) { cerr << "Socket not created" << WSAGetLastError() << endl; WSACleanup(); return; } sockaddr_in data; data.sin_family = AF_INET; data.sin_port = htons(port); inet_pton(AF_INET, ipadd.c_str(), &data.sin_addr); int conn = connect(sock, (sockaddr*)&data, sizeof(data)); if (conn == SOCKET_ERROR) { cerr << "Unable to connect to the server" << WSAGetLastError() << endl; closesocket(sock); WSACleanup(); return; } char buf[4096]; string Input; do { cout << "> "; getline(cin, Input); if (Input.size() > 0) { int sres = send(sock, Input.c_str(),Input.size() + 1, 0); if (sres != SOCKET_ERROR) { ZeroMemory(buf, 4096); int recbyte = recv(sock, buf, 4096, 0); if (recbyte > 0) { cout << "Server> " << string(buf, 0, recbyte) << endl; } } } } while (Input.size() > 0); closesocket(sock); WSACleanup(); }
-
@fari35
I don't know from all your code, other than what the message tells you.But why in the world are you using Qt and then all these Windows and C++ library calls to do everything? You are asking for problems, and I'd rather use Qt threading than C++ threading in a Qt program. If you used
QSocket
s properly you might not need any threads (Qt socket stuff is asynchronous) and then you wouldn't have the issue in the first place.... -
@fari35 said in " cannot send events to objects owned by another thread". exception in Qt visual studio 2019:
QTcpserver
It's called QTcpServer
-
@fari35 said in " cannot send events to objects owned by another thread". exception in Qt visual studio 2019:
So how can I include this file
Like any other Qt header file.
Did you install qt at all? -
@jsulm I have this zip file downloaded "qt-everywhere-src-5.15.0" and qtcpserver directory is there in this zip file but I don't know how to include it as a header file?
And I've downloaded Qt from this link "https://marketplace.visualstudio.com/items?itemName=TheQtCompany.QtVisualStudioTools2019" -
@fari35 This is an archive containing source code!
You do not need it!
As already said many times: the header file is part of a Qt installation.
I repeat this simple question: did you install Qt?
The link you posted is to download Qt Tools for Visual Studio, it is not to download Qt!
Please go to https://www.qt.io/download download Open Source Qt installer and install Qt for Visual Studio using it...