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

类别:编程语言 点击:0 评论:0 推荐:

 

 

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

 

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

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

3.  编译socket库,http://www.boost.org/more/getting_started.html,csdn的blog里也有中文指南,搜一下。编译的时候如果缺少某些文件就从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控制台程序(从sandbox的example里copy来的,原来的似乎有些小问题,改了一点点)。

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

 

#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

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

   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<

本文地址:http://com.8s8s.com/it/it28162.htm