Boost logo

Boost Users :

Subject: [Boost-users] Fwd: Sockets & acceptors
From: Wes Alvaro (alvaro_at_[hidden])
Date: 2009-09-22 14:53:49


I've looked at the examples and tutorials and have destroyed my code trying
to get this to work:

* I want to connect to a server on a shared port and get a new port number
and a unique ID.
* The server should accept connections on a shared port and then open a new
port to continue communication.
* This happens synchronously.

Currently, I can connect to the server:

        tcp::resolver resolver(io);
        tcp::resolver::query query(server, port);
        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        tcp::resolver::iterator end;
        boost::system::error_code error =
boost::asio::error::host_not_found;

        while (error && endpoint_iterator != end) {
            serverSocket.close();
            serverSocket.connect(*endpoint_iterator++, error);
        }
        if (error) {
            throw boost::system::system_error(error);
        }

        int messageSize = sizeof(ident) * 2 + sizeof(int); // ID and port
        void *message = malloc(messageSize);

        boost::asio::read(serverSocket, boost::asio::buffer((char *)
message, messageSize));

The server code, ripped from the example:

    tcp::endpoint endpoint( tcp::v4(), this->listenPort );
    tcp::acceptor a( io, endpoint );

    do {
        boost::shared_ptr<tcp::socket> sock(new tcp::socket( io ));
        this->listenSocket = sock;
        a.accept( *this->listenSocket );
    } while(true);

As you can see, the server does nothing with the client connection.
However, the client reads the 12 bytes as requested in the last line with
boost::asio::read. The information received here is junk (to my program)
and causes it to crash.

What am I doing wrong? What should I be doing?

Any help is much appreciated!

Thanks,
Wes



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net