C ++ IRC Bot Buffer错误

时间:2015-06-09 11:37:33

标签: c++ c++11 bots irc

#include <iostream>
#include <fstream>
#include <boost/asio.hpp>
#include <cstdlib>
#include <fstream>
#include <pthread.h>
#include <ostream>
#include <unistd.h>
#include <boost/array.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <array>
#define NUM_THREADS 3

void *Bot(void *threadid)
{
 while(1)
 {
  using boost::asio::ip::tcp;
  try {
   std::ofstream store;
   boost::asio::io_service io_service;
   tcp::resolver resolver(io_service);
   tcp::resolver::query query("###########", "6667");
   tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
   tcp::resolver::iterator end;
   tcp::socket socket(io_service);
   boost::system::error_code error = boost::asio::error::host_not_found;
   while (error && endpoint_iterator != end)
   {
    socket.close();
    socket.connect(*endpoint_iterator++, error);
   }
   if (error)
    throw boost::system::system_error(error);
   sleep(5);
   boost::system::error_code ignored_error;
   boost::asio::streambuf request;
   std::ostream send_stream(&request);
   send_stream << "USER Cribot * * :Cribot\r\nNICK Cribot\r\nJOIN #hide\r\nPRIVMSG #hide :
              Hello master, your wish is my command.\r\n";
   boost::asio::write(socket, request, ignored_error);
   while(1)
   {
    store.open("IRC.log", std::ios::app);
    static std::string lel;
    boost::system::error_code error;
    if (error == boost::asio::error::eof)
     break;
    else if (error)
     throw boost::system::system_error(error);
    boost::array<char, 128> buf;
    size_t len = socket.read_some(boost::asio::buffer(buf), error);
    if(lel == "PING")
    {
     boost::system::error_code ignored_error;
     boost::asio::streambuf request;
     std::ostream send_stream(&request);
     send_stream << "PONG\r\n";
     boost::asio::write(socket, request, ignored_error);
    }
    store.write(buf.data(), len); # THIS ENTERS NOTHING IN MY FILE
    std::cout.write(buf.data(), len); #THIS WORKS
    store.close();
   }
  } catch (std::exception &e) {}
 }
}

int main()
{
 pthread_t threads[NUM_THREADS];
 pthread_create(&threads[1], NULL, Bot, (void *)1);
 pthread_join(threads[1], NULL);
 return 0;
}

你好,基本上我正在制作一个简单的IRC机器人,它位于我的频道并记录所发生的一切,我做了,它可以工作,但我唯一的问题是我无法将聊天写入文件&#34;存储&#34;但是使用相同的代码,我能够打印来自IRC服务器的内容

0 个答案:

没有答案