running a server "in background mode" ??

View: New views
5 Messages — Rating Filter:   Alert me  

running a server "in background mode" ??

by pedro chaparro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi, i need to use a server inside of other code application, i take the example from
http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/tutorial/tutdaytime2/src.html
and it works but the server stay in listening mode and there is other code after that i'd like continue executing, is possible tu run the server let's say like in a background mode?

thanks

--
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

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: running a server "in background mode" ??

by Maxime van Noppen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

pedro chaparro wrote:
> Hi, i need to use a server inside of other code application, i take the
> example from
> http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/tutorial/tutdaytime2/src.html
> and it works but the server stay in listening mode and there is other
> code after that i'd like continue executing, is possible tu run the
> server let's say like in a background mode?

Hi,

You should look to how asynchronous operations work :

http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/tutorial/tutdaytime3.html

--
Maxime
_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: running a server "in background mode" ??

by pedro chaparro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

thanks very much for your support!!! i've understood that code, but however when i run it , i don't really see some difference, the server keeps in listening mode, blocking the execution of the rest of the code : i've added two simple line to print something on the console, and what i want is execute the server,that it keeps open in background but inmediatly execute too the printf code lines. main fuction would be :
.
.
.
.

int main()
{
  try
  {
    boost::asio::io_service io_service;
    tcp_server server(io_service);
    io_service.run();
    printf("Writting a line 1 \n");
    sleep(5);
    printf("Writting a line 2 \n");
   
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}



2009/11/9 Maxime van Noppen <maxime@...>
pedro chaparro wrote:
> Hi, i need to use a server inside of other code application, i take the
> example from
> http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/tutorial/tutdaytime2/src.html
> and it works but the server stay in listening mode and there is other
> code after that i'd like continue executing, is possible tu run the
> server let's say like in a background mode?

Hi,

You should look to how asynchronous operations work :

http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/tutorial/tutdaytime3.html

--
Maxime
_______________________________________________
Boost-users mailing list
Boost-users@...
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

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: running a server "in background mode" ??

by Igor R :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>     boost::asio::io_service io_service;
>     tcp_server server(io_service);
>     io_service.run();

io_service::run() does not return until there's no work for io_service.
But you can run io_service in another thread:

new boost::thread(&asio::io_service::run, &io_service); // note that
io_service object can't be local here, as it must outlive the thread!
_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: running a server "in background mode" ??

by Ryan McConnehey :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


>     io_service.run();
>     printf("Writting a line 1 \n");

The io_service can't exit the run command until all the work is
complete.  You need to put the io_service.run() into its own thread if
you want to still execute things in main.

boost::thread runningService(boost::bind(&boost::asio::io_service::run,
io_service));
printf("Writting a line 1 \n");
sleep(5);
printf("Writting a line 2 \n");
runningService.join();

This will allow you to continue and execute code in your main function.  
The problem you'll run into is trying to close the thread properly.  I
used the HTTP Server 2 example to create a class that ran my process but
responded to an outside event to signal the class to stop processing.

http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/example/http/server2/win_main.cpp

Ryan
_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users