Note that there are some explanatory texts on larger screens.

plurals
  1. POServer thread inside a qt class (need mutex?)
    primarykey
    data
    text
    <p>I made this server class that starts a thread when new connection comes in. It works ok with some cases, but it's not very stable. I am trying to solve where it breaks. My debugger tells me something about qmutex. If anyone can spot the problem. ty </p> <p>It connects with parent with signal&amp;slots and gets data back also. </p> <p>Here is the header:</p> <pre><code>#ifndef FORTUNESERVER_H #define FORTUNESERVER_H #include &lt;QStringList&gt; #include &lt;QTcpServer&gt; #include &lt;QThread&gt; #include &lt;QTcpSocket&gt; #include &lt;string&gt; using namespace std; class FortuneServer : public QTcpServer { Q_OBJECT public: FortuneServer(QObject *parent = 0); public slots: void procesServerString(string serverString); void getStringToThread(string serverString); protected: void incomingConnection(int socketDescriptor); private: QStringList fortunes; signals: void procesServerStringToParent(string serverString); void getStringToThreadSignal(string serverString); }; class FortuneThread : public QObject { Q_OBJECT public: FortuneThread(int socketDescriptor, QObject *parent); public slots: void getString(); void sendString(string sendoutString); signals: void error(QTcpSocket::SocketError socketError); void fromThreadString(string serverString); void finished(); private: int socketDescriptor; QString text; QTcpSocket tcpSocket; }; #endif </code></pre> <p>and cc:</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;QtNetwork&gt; #include "MeshServer.hh" #include &lt;iostream&gt; #include "TableView.hh" using namespace std; FortuneServer::FortuneServer(QObject *parent) : QTcpServer(parent) { } void FortuneServer::procesServerString(string serverString){ emit procesServerStringToParent(serverString); } void FortuneServer::getStringToThread(string serverString){ emit getStringToThreadSignal(serverString); } void FortuneServer::incomingConnection(int socketDescriptor) { FortuneThread *serverthread = new FortuneThread(socketDescriptor, this); //connect(&amp;serverthread, SIGNAL(finished()), &amp;serverthread, SLOT(deleteLater())); QThread* thread = new QThread; serverthread-&gt;moveToThread(thread); connect(thread, SIGNAL(started()), serverthread, SLOT(getString())); connect(serverthread, SIGNAL(fromThreadString(string)), this, SLOT(procesServerString(string))); connect(this, SIGNAL(getStringToThreadSignal(string)), serverthread, SLOT(sendString(string))); connect(serverthread, SIGNAL(finished()), thread, SLOT(quit())); connect(serverthread, SIGNAL(finished()), serverthread, SLOT(deleteLater())); connect(serverthread, SIGNAL(finished()), thread, SLOT(deleteLater())); thread-&gt;start(); } FortuneThread::FortuneThread(int socketDescriptor, QObject *parent) : QObject(parent), socketDescriptor(socketDescriptor) { } void FortuneThread::getString() { if (!tcpSocket.setSocketDescriptor(socketDescriptor)) { emit error(tcpSocket.error()); cout&lt;&lt;"socket error"&lt;&lt;endl; return; } //in part if(!tcpSocket.waitForReadyRead(10000)){ emit finished(); return; } int joj = tcpSocket.bytesAvailable(); char inbuffer[1024]; tcpSocket.read(inbuffer,1024); string instring; instring = inbuffer; instring.resize(joj); emit fromThreadString(instring); } void FortuneThread::sendString(string sendoutString) { //out part char buffer[1024]; int buffer_len = 1024; int bytecount; memset(buffer, '\0', buffer_len); string outstring = sendoutString; int TempNumOne= (int)outstring.size(); for (int a=0;a&lt;TempNumOne;a++) { buffer[a]=outstring[a]; } QByteArray block; block = buffer; tcpSocket.write(block); tcpSocket.disconnectFromHost(); tcpSocket.waitForDisconnected(); emit finished(); } </code></pre> <p>this is from parent:</p> <pre><code>//server start QHostAddress adr; adr.setAddress( QString("127.0.0.1") ); adr.toIPv4Address(); quint16 port = 1101; if (!server.listen( adr, port)) { QMessageBox::critical(this, tr("CR_bypasser"), tr("Unable to start the server: %1.") .arg(server.errorString())); close(); return; } QString ipAddress; ipAddress = server.serverAddress().toString(); statusLabel-&gt;setText(tr("The server is running on\n\nIP: %1\nport: %2\n\n" "Run the Fortune Client example now.") .arg(ipAddress).arg(server.serverPort())); connect (&amp;server, SIGNAL(procesServerStringToParent(string)), this, SLOT(procesServerString(string))); connect (this, SIGNAL(StringToServer(string)), &amp;server, SLOT(getStringToThread(string))); </code></pre> <p>edit: what I am trying to do:</p> <p>I have a client (part of a game engine(Cryengine)) that I made to send string of ingame coordinates and some other things with a socket like its done in a link I gave before. This works ok. I get data on "127.0.0.1" port 1101. Now I just need this data to be evaluated in my own program, that has this TableView class, inside which I can collect coordinates I get from the string, callculate some data from coordinates and then return this new string back through the server to gameengine. In game I will click on objects get their coor., make a string out of that (containing coor,entityid, etc..), send this string to server, that returns callculated info from TableView. I just need this one way flow only one client that is sending strings. I am not sure about recv(hsock, buffer, buffer_len, 0), I guess node that is responsible for string sending in game will wait for return string? This is one of my first programs atm I am realy confused...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload