passing socket descriptor to a function
Hi, i have the code below, i want to separate the open socket code from the writting fucntions, i want to call a escribir() function from the main but i¡m not clear the value i have to pass the function, inthe way i have below it has not work. thanks #include <ctime> #include <iostream> #include <string> #include <boost/asio.hpp> using boost::asio::ip::tcp; void escribir( tcp::socket socket){ std::string message = "mensaje"; boost::system::error_code ignored_error; boost::asio::write(socket, boost::asio::buffer(message), boost::asio::transfer_all(), ignored_error); } int main() { int contador=0; try { boost::asio::io_service io_service; tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 5544)); for (;;) { tcp::socket socket(io_service); acceptor.accept(socket); escribir(socket); } } catch (std::exception& e) { std::cerr << e.what() << std::endl; } return 0; } -- Ing Pedro Alonso Chaparro Valero Ciudad Politecnica de la Innovación iTEAM - Mobile Communications Group Polytechnic University of Valencia C\ Camino de Vera S/N, Edificio 8G 46022 Valencia, Spain
hi for the fast answer, i have other question respect to this, what about if i wanna a function return the socket descriptor value? what must be the type of value retuned? an int value? thanks 2010/1/27 Igor R <boost.lists@gmail.com>
void escribir( tcp::socket socket)
should be:
void escribir( tcp::socket &socket) _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Ing Pedro Alonso Chaparro Valero Ciudad Politecnica de la Innovación iTEAM - Mobile Communications Group Polytechnic University of Valencia C\ Camino de Vera S/N, Edificio 8G 46022 Valencia, Spain
hi for the fast answer, i have other question respect to this, what about if i wanna a function return the socket descriptor value? what must be the type of value retuned? an int value?
You mean the low-level socket descriptor, which can be used in recv/send etc.? asio::ip::tcp::socket::native_type socketDescriptor(asio::ip::tcp::socket &socket) { return socket.native(); }
from the main code i call a function to start a server on a port X, already this server started , i back to main fucntion, make some calculus and after that i have to write some data through the socket, so i call a writing fuction, but to this fuction i have to pass to it the data i want transmit and something to indicate it what's the socket i'm using. I'm not an expert in programming so i dont know if this is the best way to do it. I just want to access to the variable socket from anywhere. thanks 2010/1/27 Igor R <boost.lists@gmail.com>
hi for the fast answer, i have other question respect to this, what about if i wanna a function return the socket descriptor value? what must be the type of value retuned? an int value?
You mean the low-level socket descriptor, which can be used in recv/send etc.?
asio::ip::tcp::socket::native_type socketDescriptor(asio::ip::tcp::socket &socket) { return socket.native(); } _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Ing Pedro Alonso Chaparro Valero Ciudad Politecnica de la Innovación iTEAM - Mobile Communications Group Polytechnic University of Valencia C\ Camino de Vera S/N, Edificio 8G 46022 Valencia, Spain
I'm not an expert in programming so i dont know if this is the best way to do it. I just want to access to the variable socket from anywhere.
Then study the examples coming with asio: http://www.boost.org/doc/libs/1_41_0/doc/html/boost_asio/examples.html You will find there good design proposal to use with synchronous and asynchronous approaches.
participants (2)
-
Igor R
-
pedro chaparro