Results 1 to 8 of 8

Thread: threaded server for multiple clients writing to the client only first time

  1. #1
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default threaded server for multiple clients writing to the client only first time

    Hello

    I am writing the Threaded server where I can connect multiple clients, my server has DB(SQLite) connection.
    Working: 1. First time whatever Server has written to the socket, Client is reading.
    2. Server is reading whatever the client has written
    Not Working: 1 As soon as i connect client to the Server, Message at server:
    "QObject: Cannot create children for a parent that is in a different thread.
    (Parent is FortuneThread(0x23bb7a0), parent's thread is QThread(0x1b166a0), current thread is FortuneThread(0x23bb7a0)"
    2 Second time onwards whatever the server is writing client is not able to read.

    what is wrong with the server code.. i feel no problem with the client , as it worked as desired with the Server without thread please guide..

    Thanks
    Last edited by meena; 7th May 2010 at 13:07.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: threaded server for multiple clients writing to the client only first time

    Please post your code which causes the warning to appear. And please explain why you want to use threads for handling the network.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: threaded server for multiple clients writing to the client only first time

    Thanks for replying..
    Reason to use threads for Networking is .. I need to connect Multiple clients (100 to 1000) with the server.
    Now I am able to connect to the DB & able to get the desired answer but only for once.. second time The server is not reading the request sent by the client. every time i need to press the get connected button of the client. but i am getting the message

    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed." + the original tread creation problem..

    I am giving the code segment for Server & Client as well so that it will help u to understand. thanks for taking time to look in to the code...

    Qt Code:
    1. Server::
    2. void FortuneThread::run()
    3. {
    4. tcpSocket = new QTcpSocket(this);
    5.  
    6. if (!tcpSocket->setSocketDescriptor(socketDescriptor)) {
    7. emit error(tcpSocket->error());
    8. return;
    9. }
    10. qDebug()<<"Here Connection is established.. as newConnection signal is emitted";
    11.  
    12. createConnection();
    13. model = new QSqlTableModel(this);
    14. model->setTable("Users");
    15. model->select();
    16.  
    17. tcpSocket->write("Hello from Server to Client");
    18. tcpSocket->waitForBytesWritten(10000);
    19. connect(this->tcpSocket, SIGNAL(readyRead()), this, SLOT(ready1()),Qt::DirectConnection);
    20. tcpSocket->waitForReadyRead(10000);
    21. }
    22.  
    23. void FortuneThread::ready1()
    24. {
    25.  
    26. char buf[1024];
    27. qint64 lineLength = tcpSocket->read(buf, sizeof(buf));
    28. if (lineLength != -1) {
    29. // the line is available in buf
    30. }
    31. printf("The rec string in threaded Server = %s\n", buf);
    32. fflush(0);
    33. QString s1 = buf;
    34. if(s1 == "TotalUsers")
    35. {
    36. /* total Users */
    37. qDebug()<<"I am in ready1";
    38. QSqlQuery q1("select COUNT(UserID) as totalcount from Users");
    39. QSqlRecord rec1 = q1.record();
    40. int nameColl = rec1.indexOf("totalcount"); // index of the field "name"
    41. QString j1;//int j1;
    42. while (q1.next())
    43. j1 = q1.value(nameColl).toString();//j1 = q1.value(nameColl).toInt();
    44. /* total Users */
    45.  
    46. qDebug()<<"Total Number of Users are: j1 = :"<<j1;
    47.  
    48. /*char buf[30] = "Total Number of Users are: j1";
    49.   tcpSocket->write(buf, sizeof(buf));*/
    50.  
    51. tcpSocket->write(("Total Number of Users are :" + j1 ).toAscii().data());
    52. tcpSocket->waitForBytesWritten(10000);
    53.  
    54. }else if(s1 == "UserInfo")
    55. {
    56. static int a;
    57. /* total Users */
    58. QSqlQuery q1("select COUNT(UserID) as totalcount from Users");
    59. QSqlRecord rec1 = q1.record();
    60. int nameColl = rec1.indexOf("totalcount"); // index of the field "name"
    61. int j1;
    62. while (q1.next())
    63. j1 = q1.value(nameColl).toInt();
    64. /* total Users */
    65. QString j, k, l, m, jarr[j1], karr[j1], larr[j1], marr[j1], all_data;
    66.  
    67. QSqlQuery q("Select * from Users");
    68. QSqlRecord rec = q.record();
    69. int nameCol = rec.indexOf("UserID"); // index of the field "UserID"
    70. int nameCol1 = rec.indexOf("UserName");
    71. int nameCol2 = rec.indexOf("Verification");
    72. int nameCol3 = rec.indexOf("PIN");
    73.  
    74. int i = 0;
    75. while (q.next())
    76. {
    77. j = q.value(nameCol).toString();
    78. k = q.value(nameCol1).toString();
    79. l = q.value(nameCol2).toString();
    80. m = q.value(nameCol3).toString();
    81.  
    82. qDebug() << "USerID = : " << j; // output number of records//qDebug consol o/p
    83. qDebug() << "UserName = : " << k;
    84. qDebug() << "Verification Method: " << l;
    85. qDebug() << "PIN = : " << m;
    86.  
    87. jarr[i] = j;
    88. karr[i] = k;
    89. larr[i] = l;
    90. marr[i] = m;
    91. i++;
    92.  
    93. }
    94.  
    95. for( i = 0; i < j1; i++)
    96. {
    97. all_data += jarr[i] + "\t" + karr[i] + "\t" + larr[i] + "\t" + marr[i] + "\n";
    98. }
    99. tcpSocket->write((all_data).toAscii().data());//this statement writes the string to the socket
    100. tcpSocket->waitForBytesWritten(10000);
    101.  
    102. }else if(s1 == "adduser/117/Manoj/FC/weqeqwe")
    103. {
    104. bool i;
    105. int m=0;
    106. QSqlQuery query;
    107. /*QString userid = "117";
    108.   QString username = "Manoj";
    109.   QString verification = "FC";
    110.   QString pin = "weqeqwe";*/
    111.  
    112. QString userid = s1.section('/', 1,1);
    113. QString username = s1.section('/', 2,2);
    114. QString verification = s1.section('/', 3,3);
    115. QString pin = s1.section('/', 4,4);
    116.  
    117. qDebug()<< "userid in server.cpp = " << userid;
    118. qDebug()<< "username in server.cpp = " << username;
    119. qDebug()<< "verification in server.cpp = " << verification;
    120. qDebug()<< "pin in server.cpp = " << pin;
    121.  
    122. QString stat = QString("insert into Users values('%0', '%1' ,' %2',' %3')").arg(userid).arg(username).arg(verification).arg(pin);
    123. i = query.exec(stat);
    124.  
    125. m = query.size();
    126. printf("\n number of rows = %d\n", m);
    127. if(!i)
    128. {
    129. QString message(tr("User is not addedd 1:User already exists 2:user id should be a number"));
    130. qDebug()<<"User Not Added";
    131.  
    132. }else
    133. {
    134.  
    135. QString message(tr("User added Successfully. "));
    136. qDebug()<<"User Added";
    137. }
    138.  
    139. }else if(s1 == "edit user 117 Manoj FC ManojPIN")
    140. {
    141. int i;
    142. QSqlQuery query;
    143. QString userid = "117";
    144. QString username = "Manoj";
    145. QString verification = "FC";
    146. QString pin = "ManojPIN";
    147. query.prepare("update Users set UserName = ?,Verification = ?,PIN = ? where UserID = ?");
    148. query.addBindValue(username);
    149. query.addBindValue(verification);
    150. query.addBindValue(pin);
    151. query.addBindValue(userid);
    152. query.exec();
    153.  
    154. i = query.numRowsAffected();
    155. if(i)
    156. {
    157. QString message(tr("User updated Successfully. "));
    158. }else
    159. {
    160. qDebug()<<"UserID does not exist, cannot be edited.";
    161. }
    162.  
    163. }else if(s1 == "delete user 117 Manoj FC ManojPIN")
    164. {
    165. QString userid = "117";
    166. QString message(tr("User Deleted Successfully"));
    167. QSqlQuery query;
    168. QString string = QString("delete from Users where UserID= "+ userid);
    169. query.exec(string);
    170. tcpSocket->write("UserID 117 Deleted Successfully");
    171. qDebug()<<"User Deleted";
    172. }
    173.  
    174. }
    175.  
    176. client.cpp
    177.  
    178.  
    179.  
    180. {
    181. ui->setupUi(this);
    182.  
    183. tcpsocket = new QTcpSocket(this);
    184.  
    185. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(requestNewFortune()));
    186. connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(close()));
    187. this->setWindowTitle("Client");
    188. }
    189.  
    190.  
    191. void Client::requestNewFortune()
    192. {
    193. blockSize = 0;
    194. tcpsocket->abort();
    195. tcpsocket->connectToHost(ui->lineEdit->text(), ui->lineEdit_2->text().toInt());
    196. connect(this->tcpsocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
    197. connect(this->tcpsocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
    198. connect(ui->AddUserBtn, SIGNAL(clicked()), SLOT(sendUserDetails()));
    199. connect(ui->EditUserBtn, SIGNAL(clicked()), SLOT(sendUserDetailstoEdit()));
    200. connect(ui->DeleteUserBtn, SIGNAL(clicked()), SLOT(sendUserDetailstoDelete()));
    201.  
    202. connect(ui->TotalUserBtn, SIGNAL(clicked()), SLOT(GetTotalNumberofUsers()));
    203. connect(ui->GetUserInfoBtn, SIGNAL(clicked()), SLOT(GetUserInfo()));
    204. connect(ui->disconnect_Btn, SIGNAL(clicked()), SLOT(DisconnectFromServer()));
    205. }
    206.  
    207. void Client::readFortune()
    208. {
    209. char cbuf[1024];
    210. qint64 lineLength = tcpsocket->read(cbuf, sizeof(cbuf));
    211. if (lineLength != -1) {
    212. // the line is available in buf
    213. }
    214.  
    215.  
    216. ui->label_3->setText(cbuf);
    217. }
    218. void Client::displayError(QAbstractSocket::SocketError socketError)
    219. {
    220. switch (socketError) {
    221. .....
    222. }
    223.  
    224. }
    225. void Client::sendUserDetails()
    226. {
    227. AddUser *idadduser = new AddUser(tcpsocket);
    228. idadduser->exec();
    229. }
    230.  
    231. void Client::GetTotalNumberofUsers()
    232. {
    233. char buf[12] = "TotalUsers";
    234. qint64 lineLength = tcpsocket->write(buf, sizeof(buf));
    235. }
    236.  
    237. void Client::GetUserInfo()
    238. {
    239. char buf[12] = "UserInfo";
    240. tcpsocket->write(buf, sizeof(buf));
    241. }
    242.  
    243. void Client::sendUserDetailstoEdit()
    244. {
    245. ......
    246. }
    247.  
    248. void Client::sendUserDetailstoDelete()
    249. {
    250. ........
    251. }
    252.  
    253. void Client::DisconnectFromServer()
    254. {
    255.  
    256. tcpsocket->close();
    257. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 10th May 2010 at 09:20. Reason: missing [code] tags

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: threaded server for multiple clients writing to the client only first time

    Quote Originally Posted by meena View Post
    Reason to use threads for Networking is .. I need to connect Multiple clients (100 to 1000) with the server.
    This doesn't explain the use of threads.

    Do you create a new database connection with a unique name for each thread?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: threaded server for multiple clients writing to the client only first time

    ok at this place the thread is created..

    void FortuneServer::incomingConnection(int socketDescriptor)
    {
    FortuneThread *thread = new FortuneThread(socketDescriptor, fortune,this);
    thread->start();
    }

    my understanding is whenever the new connection happens (a new client connects to the server)new thread is connected..

    I am just trying to build my requirements on existing Threaded Fortune Server example.
    yes currently creating a new database connection for each thread. any suggestion on DB connectivity please correct me.
    Last edited by meena; 10th May 2010 at 11:06.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: threaded server for multiple clients writing to the client only first time

    Please post the contents of your createConnection() method.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: threaded server for multiple clients writing to the client only first time

    createConnection() is in database.h file & the method is exactly look like this

    static bool createConnection()
    {
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(" ... ");
    if (!db.open()) {
    QMessageBox::critical(0, qApp->tr("Cannot open database"),
    qApp->tr("Unable to establish a database connection.\n"
    "This example needs SQLite support. Please read "
    "the Qt SQL driver documentation for information how "
    "to build it.\n\n"
    "Click Cancel to exit."), QMessageBox::Cancel);
    return false;
    }

    return true;
    }
    Last edited by meena; 10th May 2010 at 12:47.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: threaded server for multiple clients writing to the client only first time

    So you are not creating a new connection for each new thread, you are reusing the same connection name each time so only one thread at once is able to communicate with the database. And that's what Qt already told you, you even quoted the message in one of the first posts.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 4
    Last Post: 30th November 2010, 22:09
  2. Extend QTcpServer to handle multiple clients
    By DiamonDogX in forum Qt Programming
    Replies: 5
    Last Post: 24th February 2010, 20:49
  3. Multi Threaded Client Server application
    By live_07 in forum Qt Programming
    Replies: 0
    Last Post: 27th August 2009, 17:32
  4. Threaded TCP server
    By Sysace in forum Newbie
    Replies: 4
    Last Post: 21st February 2008, 13:37
  5. Threaded server.
    By nithinin2001 in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2007, 17:37

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.