Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. " cannot send events to objects owned by another thread". exception in Qt visual studio 2019
Forum Updated to NodeBB v4.3 + New Features

" cannot send events to objects owned by another thread". exception in Qt visual studio 2019

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
8 Posts 3 Posters 692 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fari35
    wrote on 15 Dec 2020, 13:45 last edited by
    #1

    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();
    }
    
    
    JonBJ 1 Reply Last reply 15 Dec 2020, 14:11
    0
    • F fari35
      15 Dec 2020, 13:45

      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();
      }
      
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on 15 Dec 2020, 14:11 last edited by JonB
      #2

      @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 QSockets properly you might not need any threads (Qt socket stuff is asynchronous) and then you wouldn't have the issue in the first place....

      F 1 Reply Last reply 17 Dec 2020, 07:45
      1
      • JonBJ JonB
        15 Dec 2020, 14:11

        @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 QSockets properly you might not need any threads (Qt socket stuff is asynchronous) and then you wouldn't have the issue in the first place....

        F Offline
        F Offline
        fari35
        wrote on 17 Dec 2020, 07:45 last edited by fari35
        #3

        @JonB I'm not able to include <QTcpserver> class file in my project in visual studio 2019. How can I do that? it's not there in VS 2019 packages.

        jsulmJ 1 Reply Last reply 17 Dec 2020, 07:50
        0
        • F fari35
          17 Dec 2020, 07:45

          @JonB I'm not able to include <QTcpserver> class file in my project in visual studio 2019. How can I do that? it's not there in VS 2019 packages.

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on 17 Dec 2020, 07:50 last edited by
          #4

          @fari35 said in " cannot send events to objects owned by another thread". exception in Qt visual studio 2019:

          QTcpserver

          It's called QTcpServer

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          F 1 Reply Last reply 17 Dec 2020, 08:03
          0
          • jsulmJ jsulm
            17 Dec 2020, 07:50

            @fari35 said in " cannot send events to objects owned by another thread". exception in Qt visual studio 2019:

            QTcpserver

            It's called QTcpServer

            F Offline
            F Offline
            fari35
            wrote on 17 Dec 2020, 08:03 last edited by fari35
            #5

            @jsulm Yeah I'm not able to include this header file in my visual studio 2019. I'm using Qt therre. So how can I include this file. I have searched in extensions online but it's not even there.

            jsulmJ 1 Reply Last reply 17 Dec 2020, 08:27
            0
            • F fari35
              17 Dec 2020, 08:03

              @jsulm Yeah I'm not able to include this header file in my visual studio 2019. I'm using Qt therre. So how can I include this file. I have searched in extensions online but it's not even there.

              jsulmJ Online
              jsulmJ Online
              jsulm
              Lifetime Qt Champion
              wrote on 17 Dec 2020, 08:27 last edited by
              #6

              @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?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              F 1 Reply Last reply 17 Dec 2020, 09:05
              0
              • jsulmJ jsulm
                17 Dec 2020, 08:27

                @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?

                F Offline
                F Offline
                fari35
                wrote on 17 Dec 2020, 09:05 last edited by
                #7

                @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"

                jsulmJ 1 Reply Last reply 17 Dec 2020, 09:06
                0
                • F fari35
                  17 Dec 2020, 09:05

                  @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"

                  jsulmJ Online
                  jsulmJ Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on 17 Dec 2020, 09:06 last edited by jsulm
                  #8

                  @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...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2

                  1/8

                  15 Dec 2020, 13:45

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved