I'm currently developping a tcp server with boost asio with vs2010 on win 7.
My server class:
Header file:
class Server
{
private:
boost::asio::io_service &_io;
boost::asio::ip::tcp::acceptor *acceptor;
bool connected;
boost::asio::ip::tcp::socket *_client;
};
Source file:
Server::Server(boost::asio::io_service& io) : _io(io)
{
connected = false;
}
void Server::receiving()
{
boost::system::error_code error;
acceptor = new tcp::acceptor(_io, tcp::endpoint(tcp::v4(), 14242));
_client = new tcp::socket(_io);
connected = false;
try {
acceptor->async_accept(*_client, boost::bind(&Server::handle_accept, this, boost::asio::placeholders::error));
} catch(...) {
}
}
void Server::handle_accept(const boost::system::error_code& error)
{
// problem appears here
}
In main function, i'm running io.run();
I'm using in another class boost asio as a tcp client (which work properly) with the same io_service.
When a tcp client connect to boost asio tcp server (Class Server), handle_accept is called. error has the following values:
m_val = 1;
m_cat = 0x00ab8d00 instance;
| [boost::asio::error::detail::misc_category] = {...}
| boost::noncopyable_::noncopyable = {...}
| __vfptr = 0x00a8bf18 const boost::asio::error::detail::misc_category::`vftable'
| [0] = 0x0098aaaa boost::asio::error::detail::misc_category::`scalar deleting destructor'(unsigned int)
| [1] = 0x0098b4e1 boost::asio::error::detail::misc_category::name(void)
| [2] = 0x0098b603 boost::asio::error::detail::misc_category::message(int)
| [3] = 0x0098e39e boost::system::error_category::default_error_condition(int)
| [4] = 0x0098bef0 boost::system::error_category::equivalent(class boost::system::error_code const &,int)
| [5] = 0x0098cb61 boost::system::error_category::equivalent(int,class boost::system::error_condition const &)
Do anyone know what does it mean?
Can you help me to fix it?
Thank you !
Cyprien DIOT
France
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Use message() member function of error_code to see the error description.
IIUC, in your case it should be "Already open", i.e. you're trying to
accept new connection to a socket which was already open.
What is "already open" ? _client ? It has just been allocated before calling async_accept.
_io is properly initialized.
If this error is about acceptor, how can I use it with async_accept ? I've seen http://www.boost.org/doc/libs/1_37_0/doc/html/boost_asio/reference/basic_socket_acceptor/async_accept/overload1.html but i don't see any line where the listen port is set to acceptor.
I'm sorry for my english which is not very good.
Thank you !
----- Mail original -----
De: "Igor R" <boost...@gmail.com>
À: boost...@lists.boost.org
Envoyé: Jeudi 10 Mars 2011 00:42:24
Objet: Re: [Boost-users] [ASIO] async_accept : Error m_val 1.
Server.h:
#pragma once
#include <boost/thread/thread.hpp>
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <iostream>
class Server
{
public:
AssistGui(boost::asio::io_service &);
~AssistGui(void);
void accept(const boost::system::error_code& e);
void sendMsg(const char *msg, unsigned int size);
private:
boost::asio::io_service &io;
boost::asio::ip::tcp::socket _client;
boost::asio::ip::tcp::acceptor _acceptor;
};
Server.cpp:
#include "Server.h"
using boost::asio::ip::tcp;
Server::Server(boost::asio::io_service &ios) : io(ios), _client(ios), _acceptor(ios, tcp::endpoint(tcp::v4(), 14242))
{
_acceptor.async_accept(_client, boost::bind(&Server::accept, this, boost::asio::placeholders::error));
}
void Server::sendMsg(const char *msg, unsigned int size)
{
}
Server::~Server(void)
{
}
void Server::accept(const boost::system::error_code& e)
{
// same error :-( : "Already open"
}
int main()
{
boost::asio::io_service ios;
Server s(ios);
ios.run();
}
The error is still the same "Already open".
_WIN32_WINNT=0x0501 is defined.
I tried to change port.
No warnings at program compilation.
Can you help me?
Thank you
Cyprien DIOT
Hi Cyprien,
as far as I remember from socket programming, if you open a socket without
REUSE option, it is marked busy even after releasing for a some time
afterwards depending on your kernel implementation. Probably if you reboot
or wait some time, your testcase will work but again only once. Try
setting REUSE flag for your socket to check if it helps.
-- Slava
Like Viatcheslav mentioned, set reuse_address option:
http://think-async.com/Asio/boost_asio_1_5_2/doc/html/boost_asio/reference/socket_base/reuse_address.html
After few hours working on this problem, i've fix it:
In a new VS2010 project, with the same source code, this code works without any error in async_accept callback.
Finally, i find out that it didn't work because i was using pragma lib in one of my file for managing windows services:
#pragma comment (lib, "advapi32.lib")
#pragma comment (lib, "user32.lib")
When I removed this file, error disappears.
Then, I put theses lib in project's configuration instead of using pragma and everything works!
Although I don't understand the problem, it's now working.
Thank you for your help
Cyprien DIOT
----- Mail original -----
De: "Igor R" <boost...@gmail.com>
À: boost...@lists.boost.org
Envoyé: Jeudi 10 Mars 2011 15:58:17
Objet: Re: [Boost-users] [ASIO] async_accept : Error m_val 1.
> What is "already open" ? _client ? It has just been allocated before calling async_accept.