Boost::socket终于可以编译通过了。

 

 

在网上搜MailList,有人说Boostsocket已经差不多可以用了(盼了好长时间了)。折腾了一上午,现在终于编译通过了。

 

1.  BoostSandbox CVS(http://cvs.sourceforge.net/viewcvs.py/boost-sandbox/boost-sandbox/) 上下载socket库和其它一些文件(包括io库,最好全down)。

2.  socketcopyboost_1_30_0/boost/目录下(俺先copyboost_1_31_0但编译时问题好多,放弃了),编译时看缺少哪些库或者文件,再从boost-sandbox/拷过来。

3.  编译socket库,http://www.boost.org/more/getting_started.htmlcsdnblog里也有中文指南,搜一下。编译的时候如果缺少某些文件就从Sandbox拷过来。俺用的是VC7,怕VC6不能通过。

4.  写一个client测试程序和一个server测试程序。编译应该没什么大问题,link的时候要加上Ws2_32.lib libboost_socket.lib

 

 

 

 

 

客户端测试程序,VC7控制台程序。

#include "stdafx.h"

#include "boost/socket/ip4.hpp"
#include "boost/socket/socketstream.hpp"
#include "boost/socket/socket_exception.hpp"
#include "boost/socket/connector_socket.hpp"


using namespace boost::socket;


int _tmain(int argc, _TCHAR* argv[])
{
 try
 {
  ip4::address addr("127.0.0.1", 3234);

  typedef connector<> connector_t;
  typedef connector_t::data_connection_t data_connection_t;

  connector_t connector;
  data_connection_t socket;
  connector.connect(socket, ip4::tcp_protocol(), addr);
  basic_socket_stream s(socket);
   s << "Hello! Hello!Hello!Hello! Hello!" << std::endl;  
   std::string response;
   std::getline(s, response);
   std::cout << response;
   socket.close();
 }
 catch (const socket_exception& s)
 {
   std::cerr << s.message() << std::endl;
 }

 return 0;
}

 

 

 

/*************************************************************************************************************************

服务器端测试程序,VC7控制台程序(从sandboxexamplecopy来的,原来的似乎有些小问题,改了一点点)。

**************************************************************************************************************************/

 

#include "stdafx.h"
#include "ssteam.h"


#include "boost/socket/ip4.hpp"
#include "boost/socket/socketstream.hpp"
#include "boost/socket/acceptor_socket.hpp"
#include "boost/socket/socket_set.hpp"
#include "boost/socket/address_info.hpp"
#include "boost/lexical_cast.hpp"

#include "boost/shared_ptr.hpp"
#include "boost/bind.hpp"

 

#include "map"
#include "algorithm"
#include "vector"

//#include "boost/test/unit_test.hpp"

 

using namespace boost::socket;

 

typedef boost::shared_ptr <> > SocketPtr;
typedef std::map Clients;

struct socket_equal
 : public std::binary_function
 socket_base<>::socket_t,
 bool>
{
 bool operator()(const Clients::value_type& p,
   socket_base<>::socket_t s) const
 {
   return p.first->socket()==s;
 }
};

void server_test()
{
 try
 {
  std::cout<<"starting"< < P>

  Clients clients;

  ip4::address addr;
  addr.port(3234);
  addr.ip("127.0.0.1");

  ip4::tcp_protocol protocol;

  option::non_blocking non_block(false);

  typedef acceptor_socket<> acceptor_socket_t;
  typedef acceptor_socket_t::data_connection_t data_connection_t;

  acceptor_socket_t listening_socket;
  listening_socket.open(protocol,addr,6);

  boost::socket::socket_set master_set;
  master_set.insert(listening_socket.socket());

  while (true)
  {
   boost::socket::socket_set active_set;
   active_set=master_set;

   std::cout<<"Selecting"< < P>

   if (select(active_set.width(),
    active_set.fdset(),
    0,
    0,
    0)==-1)
   {
    throw "unexpected select result";
   }

   const boost::socket::socket_set::iterator
    i_end=active_set.end();

   for (boost::socket::socket_set::iterator i=active_set.begin();
    i!=i_end; ++i)
   {
    if (*i==listening_socket.socket())
    {
     //! this is our acceptor socket
     while (true)
     {
      ip4::address client;
      SocketPtr accepted_socket(new data_connection_t);
      int ret=listening_socket.accept(*accepted_socket,client);
      if (ret==Success)
      {
       accepted_socket->ioctl(non_block);
       master_set.insert(accepted_socket->socket());
       std::cout << "Accepted client " << client.ip()
        << ":" << client.port()
        << std::endl;
       std::string port;//=boost::lexical_cast (client.port());
             std::stringstream ss;
             ss<
             port=ss.str();

       address_info_list addr_info(
        client.ip().c_str(),
        port.c_str(),
        address_info_list::canonname);
       address_info_list::iterator iend=addr_info.end();
       for (address_info_list::iterator i=addr_info.begin();
        i!=iend; ++i)
        std::cout << i->hostname() << std::endl;

       std::cout << accepted_socket->socket() <
             clients.insert(std::make_pair(accepted_socket, client));

      }
      else
       break;
     }
    }
    else
    {
     // these are our data socket
     std::cout<<"Receiving data"< < P>

     std::string str;

     Clients::iterator client=
      std::find_if( clients.begin(), clients.end(),
      boost::bind (socket_equal(),_1,*i));

     ip4::address& client_addr=client->second;
     std::cout << client_addr.to_string();
     boost::socket::basic_socket_stream ss(*client->first);

     while (!ss.eof() && !ss.fail())
     {
      ss >> str;
      ss <<"server: i have recieved your hello!"<
           if (!ss.fail())
             std::cout< <
         }
         if (!client->first->is_open())
           std::cout<<"Socket closed"< < P>

     if (ss.eof())
     {
      // reading from socket should have forced it closed
      master_set.erase(*i);
      clients.erase(client);
     }
    }
   }

  }
 }
 catch (const socket_exception& e)
 {
  std::cerr << e.message()<< std::endl;
  throw;
 }
}

int _tmain(int argc, _TCHAR* argv[])
{
 server_test();
 return 0;
}

<><

<>
<><>
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值