c++ - Multi clients not working properly on Qt4 Multi thread server? -




friends! have written code multi threaded tcp server in qt4. server listening remote client connection requests. whenever client connects server, takes ip, name , port number , verifies if recognized (authorized) client (that can checked through pre-stored text file having names, ips , port numbers of registered clients). if is, communication proceed. otherwise, server terminates connection.

edit1: here excerpt mythread.cpp

void mythread::run() {     // thread starts here     qdebug() << socketdescriptor << ("starting thread...");     socket = new qtcpsocket();      if(!socket->setsocketdescriptor(this->socketdescriptor))     {         emit error(socket->error());         return;     }      connect(socket, signal(readyread()), this, slot(readyread()), qt::directconnection);     connect(socket, signal(disconnected()), this, slot(disconnected()), qt::directconnection);      qdebug() << socketdescriptor << ("client connected!");      exec(); } 

issue: problem when run program , connects first client works fine. 2nd client connected (testing through telnet, now) starts behaving strange (no messages receiving client 2 or client 2 freezing etc). can test code in machine?

myserver.cpp

#include "myserver.h" #include "ioprogram.h" #include <qtcpsocket> #include <string> #include <iostream>  using namespace std;  myserver::myserver(qobject *parent): qtcpserver(parent) {     qstringlist accepted_ip_list;   //list of remote ips can accepted in qstring list format     accepted_ip_list.append("127.0.0.1");   // ipv4 local address     accepted_ip_list.append("::1");         // ipv6 local address      accepted_ip_list.append("172.18.48.139");      // convert qstring integer format, generating new list     foreach (const qstring &ip, accepted_ip_list)     {         qhostaddress host_address(ip);         my_accepted_ip_list.append(host_address);     } /*     myserver = new qtcpserver(this);      connect(myserver, &qtcpserver::incomingconnection, this, &myserver::incomingconnection);      myserver->listen(qhostaddress::any, 1234); */ }  void myserver::startserver() {     if(!this->listen(qhostaddress::any,1234))     {         qdebug() << "could not start server.";     }     else     {         qdebug() << "listening...";     } }  void myserver::incomingconnection(qintptr socketdescriptor) {     qtcpsocket *socket = new qtcpsocket(this);     socket->setsocketdescriptor(socketdescriptor);      qdebug() << socketdescriptor << "connecting...";      qhostaddress host_address = socket->peeraddress();      //quint32 ipv4 = host_address.toipv4address();     std::string ipv4 = host_address.tostring().tostdstring();      qbytearray ipv6 = qbytearray((char*)host_address.toipv6address().c, 16);      ioprogram test;      bool status= test.checkauthenticity(ipv4);      if(status == false){         /*     while (myserver->haspendingconnections())     {         qtcpsocket *socket = myserver->nextpendingconnection();         qhostaddress host_address = socket->peeraddress();          bool contains = false; */         for(int i=0; < my_accepted_ip_list.size(); i++)         {             //quint32 accepted_ipv4 = my_accepted_ip_list[i].toipv4address();             std::string accepted_ipv4 = my_accepted_ip_list[i].tostring().tostdstring();              if(accepted_ipv4 == ipv4)             {                 status = true;                 qdebug() << "testing...";                 break;             }        /*     else             {                 qdebug() << "rejected!";                 socket->abort();        // reject peer disconnecting                 socket->deletelater();  // schedule socket removal memory             }         */             /*if(my_accepted_ip_list[i].isequal(host_address,qhostaddress::convertv4mappedtoipv4))             {                 contains = true;                 break;             }*/         }          if(!socket->qtcpsocket::connectedstate){             socket->abort();        // reject peer disconnecting             socket->deletelater();  // schedule socket removal memory         }          if(status)         {             qdebug() << "accepted!";             mythread *thread = new mythread(socketdescriptor, this);              connect(thread, signal(finished()), thread, slot(deletelater()));             thread->start();         }     } } 

ioprogram.cpp

#include "ioprogram.h" #include <qfile> #include <qdebug> #include <qtextstream> #include <iostream> #include <fstream> #include <stdlib.h> #include <string> #include <vector> #include <sstream>  using namespace std;      struct data {         string name;         string ip;         string port;     };      vector<data> alldata;  ioprogram::ioprogram() {  }  void read() {     qfile file("c:/users/shahwani/documents/bremen/sem3/qt/multiserveraugust/nodeslist.txt");      if(file.open(qiodevice::readwrite | qiodevice::text))     {         qtextstream stream(&file);          qstring line;                 {             line = stream.readline();             qdebug() << line;         }while(!line.isnull());          file.close();     } }  string ioprogram::inpop() {     ifstream infile;     infile.open("nodeslist.txt"); //    infile.clear();      // check error     if(infile.fail()){         cerr << "error opening file" << endl;         exit(1);     }      int count = 0;     string line;     char delim = '\t';     getline (infile,line);      // read file until end     while(!infile.eof())     {         getline (infile,line);          string token;         stringstream stream(line);          // splitting string using delimiter         vector<string> vect;         while(getline(stream, token, delim) )         {             vect.push_back(token);         }         data d;         d.name = vect[0];         d.ip = vect[1];         d.port = vect[2];         vect.clear();         alldata.push_back(d);         count++;     }      cout << count << " nodes found!" << endl;     infile.close();      // check authenticity , allow recognised ips       // write active nodes in separate file on disk      ofstream outfile;     outfile.open("sample.txt");     data activelist;     for(int = 0; < alldata.size(); i++)     {         activelist = alldata[i];         outfile << "name: " << activelist.name << "\tip: " << activelist.ip << "\tport: " << activelist.port <<endl;     }      outfile.close();      return activelist.ip; }  bool ioprogram::checkauthenticity(string ip) {     for(int i=0;i<alldata.size();i++)     {         data check;         check = alldata[i];         if(ip == check.ip)         {             return true;         }         else         {             return false;         }     } } 

i guess cannot put lot of code in 1 question. let me know in case needs rest of files mythread.cpp or header files understand issue , how can share them.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -