Boost logo

Boost Users :

Subject: Re: [Boost-users] invalid argument?
From: pedro chaparro (pdro07_at_[hidden])
Date: 2009-10-20 05:43:19


here, all the code , in bold i put the part where i call the client part tha
have to connect to a server that is already listening open the same port, u
will see some functions that are use for the simulator. I really aprecciate
ur help. The strange thins is that i can connect to the same server using a
external script, i mean, other code where simulator code is not included.
thanks

#include <stdio.h>
#include <stdlib.h>
#include "myNull.h"
#include "ip.h"
#include "udp.h"
#include "rtp.h"

#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/array.hpp>
#include <boost/numeric/conversion/cast.hpp>

#include <vector>
#include "connection.hpp" // Must come before boost/serialization headers.
#include <boost/serialization/vector.hpp>

#include <boost/lexical_cast.hpp>
#include <boost/thread/thread.hpp>
#include "stock.hpp"

static class myNullClass : public TclClass {
public:
        myNullClass() : TclClass("Agent/myNull") {}
        TclObject* create(int, const char*const*) {
                return (new myNull);
        }
}class_myNull;

namespace s11n_example {

/// Downloads stock quote information from a server.
class client
{
public:
  /// Constructor starts the asynchronous connect operation.
  client(boost::asio::io_service& io_service,
      const std::string& host, const std::string& service)
    : connection_(io_service)
  {
    // Resolve the host name into an IP address.
    boost::asio::ip::tcp::resolver resolver(io_service);
    boost::asio::ip::tcp::resolver::query query(host, service);
    boost::asio::ip::tcp::resolver::iterator endpoint_iterator =
      resolver.resolve(query);
    boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;

    // Start an asynchronous connect operation.
    connection_.socket().async_connect(endpoint,
        boost::bind(&client::handle_connect, this,
          boost::asio::placeholders::error, ++endpoint_iterator));
  }

  /// Handle completion of a connect operation.
  void handle_connect(const boost::system::error_code& e,
      boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
  {
    if (!e)
    {
      // Successfully established connection. Start operation to read the
list
      // of stocks. The connection::async_read() function will automatically
      // decode the data that is read from the underlying socket.
//for(;;){
connection_.async_read(stocks2_,
          boost::bind(&client::handle_read, this,
            boost::asio::placeholders::error));
    }
//}//cierra for
    else if (endpoint_iterator !=
boost::asio::ip::tcp::resolver::iterator())
    {
      // Try the next endpoint.
      connection_.socket().close();
      boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
      connection_.socket().async_connect(endpoint,
          boost::bind(&client::handle_connect, this,
            boost::asio::placeholders::error, ++endpoint_iterator));
    }
    else
    {
      // An error occurred. Log it and return. Since we are not starting a
new
      // operation the io_service will run out of work to do and the client
will
      // exit.
      std::cerr << e.message() << std::endl;
printf("error seccion cliente mynull \n");
    }
  }

  /// Handle completion of a read operation.
  void handle_read(const boost::system::error_code& e)
  {
    if (!e)
    {
      // Print out the data that was received.
printf("Cliente myNULL printing structure \n");
      for (std::size_t i = 0; i < stocks2_.size(); ++i)
      {printf(" jaja \n");
        std::cout << "Structura " << i << "\n";
        std::cout << " packet type: " << stocks2_[i].pkt_type << "\n";
        std::cout << " packet id: " << stocks2_[i].id_pkt << "\n";
        std::cout << " packet size: " << stocks2_[i].size_pkt << "\n";
        std::cout << " IP origen: " << stocks2_[i].ip_src_addr << "\n";
        std::cout << " IP destino: " << stocks2_[i].ip_dst_addr << "\n";
        std::cout << " TTL: " << stocks2_[i].ttl << "\n";
        std::cout << " Tiempo d envio: " << stocks2_[i].time << "\n";

      }
    }
   else
    {
      // An error occurred.
      std::cerr << e.message() << std::endl;
printf("error seccion cliente mynull ESTRUCTURA \n");
    }

    // Since we are not starting a new operation the io_service will run out
of
    // work to do and the client will exit.
  }

private:
  /// The connection to the server.
  connection connection_;

  /// The data received from the server.
  std::vector<ns_pkt> stocks2_;
};

} // namespace s11n_example

void myNull::recv(Packet* pkt, Handler*)
{

//int i, j;
    printf("Entramos en myNull \n");
//sleep(4);
        //hdr_ip* iph=hdr_ip::access(pkt);

*try
  {

//for(int i=1;i<8;i++){
    boost::asio::io_service io_service;
    s11n_example::client client(io_service, "localhost", "9999");
    io_service.run();
sleep(7);
  }

//}//end for
  catch (std::exception& e)
  {
    printf("Error myNull main-try \n");
std::cerr << e.what() << std::endl;
  }*

    hdr_cmn* hdr=hdr_cmn::access(pkt);
    //hdr_rtp* rtp = hdr_rtp::access(pkt);

    pkt_received+=1;

    fprintf(tFile,"%-16f id %-16d udp %-16d\n",
Scheduler::instance().clock(), hdr->uid_, hdr->size()-28);

    if (app_)
               app_->recv(hdr_cmn::access(pkt)->size());

        Packet::free(pkt);
}

int myNull::command(int argc, const char*const* argv)
{
    //Tcl& tcl = Tcl::instance();

    if (strcmp(argv[1], "set_filename") == 0) {
        strcpy(tbuf, argv[2]);
        tFile = fopen(tbuf, "w");
        return (TCL_OK);
    }

    if (strcmp(argv[1], "closefile") == 0) {
        fclose(tFile);
        return (TCL_OK);
    }

    if(strcmp(argv[1],"printstatus")==0) {
        print_status();
        return (TCL_OK);
    }

    return (Agent::command(argc, argv));
}

void myNull::print_status()
{
    printf("myEvalvid_Sink)Total packets received:%ld\n", pkt_received);
}

2009/10/20 Igor R <boost.lists_at_[hidden]>

> yes, it is from a exception, i'm re-using the code on
>> http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/example/serialization/client.cppto implement a co-simulation framework using a famous simulator called
>> network simulator 2, anyway , the runtime error comes when i try to do the
>> connection with the server, the server is open in listening mode but i can
>> not achieved establish the connection.
>>
>
>
> Can you post the exact code that you use to "establish the connection"?
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> 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 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