与其他程序互动;c 中的bot
interacting with other programms; bot in c++
我已经学习了一段时间了,我一直在想彼此之间的互动方式以及如何编码这种行为。
我的目标是学习如何为简单游戏(扫雷...)和启动中的汽车行为编码机器人。
我想知道那里有什么工具和库,以及您是否可以推荐一些有关该主题的好教程/书籍。我更喜欢一种高级方法。
我正在使用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个项目。
第一个项目内容:您称之为首都?
相关的问题程序如何相互通信?
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- .cpp和.h文件中的模板专用化声明
- 反向给定链表中的K节点
- 正在查找文档以获得PS4平台的C++中的设备信息
- enum是C++中的宏变量还是整数变量
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 将字符串存储在c++中的稳定内存中
- 文本文件中的单词链表
- 递归函数计算序列中的平方和(并输出过程)
- 如何从C++中的依赖类型中获得它所依赖的类型
- C++中的"inline"关键字
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 用C++中的一个变量定义一个常量
- vector.resize()中的分配错误
- 使用指针从C++中的数组中获取最大值
- arr[-1]在c++中的奇怪行为
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 将值指定给向量(2D)的向量中的某个位置
- 与其他程序互动;c 中的bot