与其他程序互动;c 中的bot

interacting with other programms; bot in c++

本文关键字:中的 bot 其他 程序      更新时间:2023-10-16

我已经学习了一段时间了,我一直在想彼此之间的互动方式以及如何编码这种行为。

我的目标是学习如何为简单游戏(扫雷...)和启动中的汽车行为编码机器人。

我想知道那里有什么工具和库,以及您是否可以推荐一些有关该主题的好教程/书籍。我更喜欢一种高级方法。

我正在使用Windows和Linux。

有一个查看过程间通信(IPC)。

Linux IPC

Windows IPC

boost.interprocess将有助于抽象每个平台之间的某些差异。

示例过程glasgow

#include <cstdio>
#include <string>
#include <thread>
#include <chrono>
#include <cstdlib>
#include <boost/interprocess/creation_tags.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
using std::string;
using std::char_traits;
using std::chrono::seconds;
using boost::interprocess::allocator;
using boost::interprocess::create_only;
using boost::interprocess::basic_string;
using boost::interprocess::shared_memory_object;
using boost::interprocess::managed_shared_memory;
typedef allocator< char, managed_shared_memory::segment_manager > shared_string_allocator;
typedef basic_string< char, char_traits< char >, shared_string_allocator > shared_string;
int main( int, char** )
{
    const char* TAG = "IPC-Example";
    const size_t LIMIT = 1024;
    shared_memory_object::remove( TAG );
    managed_shared_memory wormhole( create_only, TAG, LIMIT );
    wormhole.construct< shared_string >( "developer" )( "You call that a capital city?", wormhole.get_segment_manager( ) );
    std::this_thread::sleep_for( seconds( 10 * 60 ) );
    return EXIT_SUCCESS;
}

示例过程爱丁堡

#include <cstdio>
#include <string>
#include <cstdlib>
#include <boost/interprocess/creation_tags.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
using std::pair;
using std::string;
using std::char_traits;
using boost::interprocess::allocator;
using boost::interprocess::basic_string;
using boost::interprocess::open_or_create;
using boost::interprocess::shared_memory_object;
using boost::interprocess::managed_shared_memory;
typedef allocator< char, managed_shared_memory::segment_manager > shared_string_allocator;
typedef basic_string< char, char_traits< char >, shared_string_allocator > shared_string;
int main( int, char** )
{
    const char* TAG = "IPC-Example";
    const size_t LIMIT = 1024;
    managed_shared_memory wormhole( open_or_create, TAG, LIMIT );
    auto item = wormhole.find< shared_string >( "developer" );
    auto content = item.first;
    auto count = item.second;
    printf( "Received %lu item.n", count );
    printf( "First items content: %s.n", content->data( ) );
    return EXIT_SUCCESS;
}

构建

g -std = c 11 -o glasgow glasgow.cpp -lpthread -lrt

g -std = C 11 -O爱丁堡Edinburgh.cpp -lpthread -lrt

执行

./爱丁堡

./格拉斯哥

输出

收到1个项目。

第一个项目内容:您称之为首都?

相关的问题程序如何相互通信?