0

I just can't understand what i did. How it works? It works just by half, but there is identical pieces of code(almost). I have client-server application. It sends any requests, and getting response, either list, that i will transform to vector, or string that contains HTML code. So, I will try to explain, but you are welcome to ask as much questions as you want. 1) I request file from server, by sending command "2" and path. Here is code

void Connection::requestFile(string path)
{
//string cookedPath = "./" + path;
string reply = this->sendCommand("2\n" + path);
vector<string> response = this->divideString(reply);

// set as files list we got. First is a helper, so we will not add it to files list.
if (response[0] == "directories") {
    // remove "directories" entry so it will not be listed then
    response.erase(response.begin());
    this->files = response;
    this->displayHtml = false;
} else {
    //else server sent string with html
    this->html = reply;
    this->displayHtml = true;
}
}

displayHtml here is kinda switch that will help to determine what to do.

So in this method i've used sendCommand() method, which by my opinion is origin of all troubles. Here it comes

string Connection::sendCommand(std::string command)
{
// send command
zmq::message_t request(command.length());
memcpy (request.data(), command.c_str(), command.length());
Connection::socket.send(request);

// get reply

zmq::message_t reply;
Connection::socket.recv(&reply);
// make string out of reply
std::string rpl = std::string(static_cast<char*>(reply.data()), reply.size());
return rpl;
}

Sorry about this formatting.

Then i use these methods here

void MainWindow::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
// set our current folder to ./folder/ + selected file
connection->currentPath = connection->currentPath + item->text().toStdString();
// update file list. as argument we give path we just got
// std::cout << connection->currentPath << endl;
ui->listWidget->clear();
connection->requestFile(connection->currentPath);
if (connection->displayHtml == true) {
// webview->updateHtml(connection->html);
    cout << "html";
} else {
    this->updateFilesList(connection->files);
    cout << "fileslist";
}
}

This method will be called when i double click on item in Qt widget list. Say i've double clicked on item. It partially works fine, but it isnt prints it. But if i close application, it prints. So i guess problem is in blocking on 'recv' zmq function, but why it works for a half then? Maybe rewrite it anyhow? Thanks.

2
  • Connection::socket.recv(&reply); will block until it receives a message. Where is the code for the other side of the connection? Where is the server? Nov 30, 2016 at 19:57
  • Yes, to me it seems that the other side of this connection is not REPLYing to the REQUEST. In that case the socket.recv(&reply); will block until an exception is thrown by exiting the program. Nov 30, 2016 at 20:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.