为TCP客户端提升库

时间:2014-09-25 14:32:45

标签: c++ macos boost tcp boost-asio

我在stackoverflow中运行了一个boost TCP客户端示例find。操作系统是Mac OS,boost lib是通过brew安装的。我是boost lib和mac os的初学者。 客户端使用以下编译:

g++ example_client.cpp -o client -I/usr/local/Cellar/boost/1.55.0_2/include -L/usr/local/Cellar/boost/1.55.0_2/lib  -lgsl -lgslcblas

我收到以下错误:

./ecen_client.h:32:45: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
    boost::shared_ptr<ip::tcp::socket> sock = boost::make_shared<ip::tcp::socket>(io_service);
                                            ^
example_client.cpp:20:20: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
    while (request != "shutdown\n") {
                   ^  ~~~~~~~~~~~~
In file included from example_client.cpp:6:
In file included from ./ecen_client.h:11:
In file included from /usr/local/Cellar/boost/1.55.0_2/include/boost/make_shared.hpp:15:
In file included from /usr/local/Cellar/boost/1.55.0_2/include/boost/smart_ptr/make_shared.hpp:15:
/usr/local/Cellar/boost/1.55.0_2/include/boost/smart_ptr/make_shared_object.hpp:711:17: error: no matching constructor for initialization of
      'boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >'
    ::new( pv ) T( a1 );
                ^  ~~
./ecen_client.h:32:54: note: in instantiation of function template specialization
      'boost::make_shared<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >,
      boost::asio::io_service>' requested here
    boost::shared_ptr<ip::tcp::socket> sock = boost::make_shared<ip::tcp::socket>(io_service);
                                                     ^
/usr/local/Cellar/boost/1.55.0_2/include/boost/asio/basic_stream_socket.hpp:46:7: note: candidate constructor (the implicit copy constructor)
      not viable: no known conversion from 'const boost::asio::io_service' to 'const boost::asio::basic_stream_socket<boost::asio::ip::tcp,
      boost::asio::stream_socket_service<boost::asio::ip::tcp> >' for 1st argument
class basic_stream_socket
      ^
/usr/local/Cellar/boost/1.55.0_2/include/boost/asio/basic_stream_socket.hpp:72:12: note: candidate constructor not viable: 1st argument
      ('const boost::asio::io_service') would lose const qualifier
  explicit basic_stream_socket(boost::asio::io_service& io_service)
           ^
/usr/local/Cellar/boost/1.55.0_2/include/boost/asio/basic_stream_socket.hpp:89:3: note: candidate constructor not viable: requires 2 arguments,
      but 1 was provided
  basic_stream_socket(boost::asio::io_service& io_service,
  ^
/usr/local/Cellar/boost/1.55.0_2/include/boost/asio/basic_stream_socket.hpp:110:3: note: candidate constructor not viable: requires 2
      arguments, but 1 was provided
  basic_stream_socket(boost::asio::io_service& io_service,
  ^
/usr/local/Cellar/boost/1.55.0_2/include/boost/asio/basic_stream_socket.hpp:130:3: note: candidate constructor not viable: requires 3
      arguments, but 1 was provided
  basic_stream_socket(boost::asio::io_service& io_service,
  ^
2 warnings and 1 error generated.

粘贴了ecen_client.h。     / *      *界面初稿。      *      *这些功能尚未设置。如果你想到更好的东西,改变它们      * /

#ifndef ECEN_CLIENT_H
#define ECEN_CLIENT_H

#include <boost/asio.hpp>
#include <boost/make_shared.hpp>
#include <memory>
using namespace boost::asio;

class ecen_client {
  public:
    //initialize and connect
    ecen_client(char *addr, char *port);

    //get a certain number of bytes
    int recieve(char *msg, int bytes);

    //return number of bytes transmitted, so we can compare with "bytes"
    int transmit(char *msg, int bytes);

    //close everything
    void close();

  private:
//    typedef ip::tcp::socket  my_sock;
    io_service io_service; //io_service object provides core I/O functionality
    boost::shared_ptr<ip::tcp::socket> sock = boost::make_shared<ip::tcp::socket>(io_service);
};

#endif

example_client.cpp

//#include "stdafx.h"  uncomment for visualstudio
#include <cstdlib>
#include <cstring>
#include <iostream>

#include "ecen_client.h"

enum { max_length = 1024 }; //specify the buffer size to send across

int main(int argc, char* argv[]) {
  if (argc != 3) {
    std::cerr << "Usage: blocking_tcp_echo_client <host> <port>\n";
    return 1;
  }

  try {
    ecen_client client(argv[1], argv[2]);
    char request[max_length], reply[max_length];

    while (request != "shutdown\n") {
      using namespace std; // For strlen.
      std::cout << "Enter message: ";
      std::cin.getline(request, max_length);
      size_t request_length = strlen(request);

      client.transmit(request, max_length);
      client.recieve(reply, max_length);

      std::cout << "Reply is: " << reply << std::endl;
    }

    client.close();
  } catch (std::exception& e) {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}

0 个答案:

没有答案