Fwd: Sockets & acceptors
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_ptrtcp::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::asio::read(serverSocket, boost::asio::buffer((char *) message, messageSize)); <...> 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.
Are you sure read() returns successfully and doesn't raise exception? Try to initialize the memory buffer (fill with zeros or something), and see if it really changes.
participants (2)
-
Igor R
-
Wes Alvaro