0

(I'm not a pro)

I'd like some tip on what is going wrong in my code.

The connexion is always with the same computer once the first file is received.

Just after receiving the file I want to open it to modify some variable in my main program. But the file is considered empty in the program. If I stop the program and open the file directly it contain all the data that should be there (not empty). Should I do something more when closing the output file?

modele_vao, and modele_count are variable shared with the main program (that's the reason for the mutex)

Here's the code for the thread:

    void threadservfct()
{

    while (1)
    {

        {
            boost::mutex::scoped_lock lk(m);

            std::wcout << "Server_thread START " << std::endl;
            boost::array<char, 1024> buf;
            size_t file_size = 0;
            boost::asio::io_service io_service;
            boost::asio::ip::tcp::acceptor acceptor(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 13));
            boost::system::error_code error;
            boost::asio::ip::tcp::socket socket(io_service);
            acceptor.accept(socket);
            boost::asio::streambuf request_buf;
            boost::asio::read_until(socket, request_buf, "\n\n");
            std::wcout << "request size:" << request_buf.size() << std::endl;
            std::istream request_stream(&request_buf);
            std::string file_path;
            request_stream >> file_path;
            request_stream >> file_size;
            request_stream.read(buf.c_array(), 2); // eat the "\n\n" 
            size_t pos = file_path.find_last_of("\\");
            if (pos != std::string::npos)
                file_path = file_path.substr(pos + 1);
            std::ofstream output_file(file_path.c_str(), std::ios_base::binary);


            if (!output_file)
            {
                std::wcout << "failed to open " << std::endl;
            }

            // write extra bytes to file
            do
            {
                request_stream.read(buf.c_array(), (std::streamsize)buf.size());
                std::wcout << __FUNCTION__ << " write " << request_stream.gcount() << " bytes" << std::endl;
                output_file.write(buf.c_array(), request_stream.gcount());
            } while (request_stream.gcount()>0);

            for (;;)
            {
                size_t len = socket.read_some(boost::asio::buffer(buf), error);
                if (len>0)
                    output_file.write(buf.c_array(), (std::streamsize)len);
                if (output_file.tellp() == (std::fstream::pos_type)(std::streamsize)file_size)
                    break; // file was received
                if (error)
                {
                    std::wcout << error << std::endl;
                    break;
                }
            }
            std::wcout << "received " << output_file.tellp() << " bytes" << std::endl;


            output_file.close();

            std::wcout << "END TRANSMISSION. Reiceived:" << file_path.c_str() << std::endl;


            std::wcout << "import new mesh" << std::endl;
            assert(load_mesh(file_path.c_str(), &modele_vao, &modele_count));
            std::wcout << "end mesh import" << std::endl;
        }

    }
}
user3544665
  • 55
  • 10

2 Answers2

0

You're listening on port 13. This requires administrative privileges.

Also, it's likely not what you want to do because it's a "Well known service port" http://en.wikipedia.org/wiki/Daytime_Protocol

Also, it seems very strange (to say the least) that you call threadservfct(); unconditionally in the thread. It would seem that this results in infinite recursion in the best case, and indefinite waiting in the most likely scenario.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thx for your answer. I don't know a lot about differences beetween ports. Which one should I choose then? I don't get what you mean about calling `threadservfct();` in the thread. `threadservfct();` is the thread fct -> I begin it like that `boost::thread thrd1(&threadservfct);` This thread is supposed to wait all the time for new file coming and to put the waiting part off of the main program. – user3544665 Jun 17 '14 at 06:59
  • @user3544665 just remove the self-call then. Here's a simple modification that was able to receive a small file correctly for me: http://paste.ubuntu.com/7656987/ And read a book :) – sehe Jun 17 '14 at 07:01
  • there's no self call in my fct :s you just added it but i'll look for the port 6767 plus my code works, i can receive file without problem, it's just that if I open them in the program with the `assert(load_mesh(file_path.c_str(), &modele_vao, &modele_count));` function it consider the file empty but when i close the program all the files downloaded are present with all the data – user3544665 Jun 17 '14 at 07:14
  • @user3544665 Oops. It appears I had a silent copy/paste error somewhere. Well, forget about the self call :) If the file exists with the data, it's obviously not empty, which means that `load_mesh` is failing for a different reason. Good luck with the rest! – sehe Jun 17 '14 at 07:36
  • Oh, also: http://stackoverflow.com/questions/133879/how-should-one-go-about-choosing-a-default-tcp-ip-port-for-a-new-service – sehe Jun 17 '14 at 07:37
0

Found the problem, the file I was opening with load_mesh was a .obj that was refering to a mtl file. The mtl file wasnt sent so that's why it didnt worked.

pretty stupid error

user3544665
  • 55
  • 10