1
votes

I wrote a program to synchronize files between two computers in the same local area network, just like the DropBox. It works perfectly to synchronize files from one folder to another folder in the same computer, but when I want to test the program between two computers, it fails to connect to another computer.

my router's IP is 192.168.1.1, one host's IP is 192.168.1.101(host A), another's is 192.168.1.107(host B), they are all in the same local area network. host A runs the client program, host B runs the server program. when host A tries to connect to host B, it fails and displays the message as below:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

I just don't know what's the problem in connecting to another computer in my local area network, and I display the connection code.

client:

bool Send::CheckConnect(boost::asio::ip::tcp::socket& socket)
{
    boost::asio::io_service io_connect;
    boost::asio::ip::tcp::resolver resolver(io_connect);
    boost::asio::ip::tcp::resolver::query query("192.168.1.107", "6873");
    boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query), end;
    boost::system::error_code error_connect;

    unsigned short count = 0;
    while( count < 3 )
    {
        ++count;
        if( boost::asio::connect(socket, endpoint_iterator, error_connect) != end )
            return true;                         
    }
    std::cout<<boost::system::system_error(error_connect).what()<<std::endl;
    return false;
}

server:

boost::asio::io_service io_sev;
boost::asio::ip::tcp::acceptor accept_server( io_sev, boost::asio::ip::tcp::endpoint
    (boost::asio::ip::tcp::v4(), 6873) );
unsigned count = 0;
boost::asio::ip::tcp::socket socket_server(io_sev);
1

1 Answers

1
votes

On Server side, I dont see any accept or async_accept ! you need something like:

void ConnectionServer::creatSocketAndAccept()
{
    //Accept the next connection.
    acceptor.async_accept(socket,
        boost::bind(&ConnectionServer::handle_accept, this, boost::asio::placeholders::error)
    );
}

in case of accept (since you used connect not async_connect) you dont need to provide the boost::bind for accept handler.

hope that helps