server.cpp:
#include
#include
#include
#include
using namespace std::chrono;
#include "libipc/ipc.h"
using socket_handle_t = ipc::chan<ipc::relat::single, ipc::relat::single, ipc::trans::unicast>;
FILE* filp = NULL;
bool running = true;
int main(int argc, char* argv[])
{
filp = fopen("data.txt", "w");
socket_handle_t rcv("ipc-chn", ipc::receiver);
socket_handle_t snd("ipc-chn2", ipc::sender);
while (true)
{
auto msg = rcv.recv();
if (msg.empty())
{
printf("recv failed\n");
continue;
}
printf("rcv success\n");
if (!snd.send(msg))
{
printf("response failed\n");
continue;
}
continue;
size_t len = fwrite(msg.data(), 1, msg.size(), filp);
if (msg.size() != len)
{
std::cout << "write failed: " << len << std::endl;
}
}
return 0;
}
client.cpp:
#include
#include
#include
#include
#include
#include
using namespace std::chrono;
#include "libipc/ipc.h"
using socket_handle_t = ipc::chan<ipc::relat::single, ipc::relat::single, ipc::trans::unicast>;
long long PKG_NUM = 1024;
long long PKG_SIZE = 64 * 1024LL; //102410244
FILE* filp = NULL;
bool running = true;
void printTimeInfo(const std::string& name, const time_point<high_resolution_clock>& startTime, const time_point<high_resolution_clock>& endTime, const std::vector& timestamps)
{
auto us = duration_cast(endTime.time_since_epoch() - startTime.time_since_epoch())
.count();
std::cout << name << std::endl;
std::cout << "-start: " << duration_cast(startTime.time_since_epoch()).count() << " us" << std::endl;
std::cout << "-end: " << duration_cast(endTime.time_since_epoch()).count() << " us" << std::endl;
std::cout << "-duration: " << us << " us" << std::endl;
std::cout << "ave: " << us / PKG_NUM << " us" << std::endl;
std::cout << "min: " << *std::min_element(timestamps.begin(), timestamps.end()) << " us" << std::endl;
std::cout << "max: " << *std::max_element(timestamps.begin(), timestamps.end()) << " us" << std::endl;
std::cout << "speed: " << PKG_NUM * PKG_SIZE / 1024LL / 1024 / (us / 1000'000.0) << " MB/s" << std::endl;
std::cout << std::endl;
}
int main(int argc, char* argv[])
{
if (argc > 2)
{
PKG_NUM = atoll(argv[1]);
PKG_SIZE = atoll(argv[2]);
}
const char* sz = "a";
const int* pv = &argc;
if (argc > 2)
{
sz = "b";
pv = &argc;
}
socket_handle_t snd("ipc-chn", ipc::sender);
socket_handle_t rcv("ipc-chn2", ipc::receiver);
char* buff = (char*)malloc(PKG_SIZE + 1);
long long i = 0;
for (i = 0; i < PKG_SIZE; ++i)
{
buff[i] = 'A';
}
buff[i] = '\0';
std::vector<long long> timestamps(PKG_NUM);
time_point<high_resolution_clock> s, e;
time_point<high_resolution_clock> startTime = high_resolution_clock::now();
for (i = 0; i < PKG_NUM; ++i)
{
s = high_resolution_clock::now();
if (!snd.send(buff, strlen(buff)))
{
std::cout << "send failed" << std::endl;
}
auto msg = rcv.recv(1000); // timeout ms
e = high_resolution_clock::now();
timestamps[i] = duration_cast<microseconds>(
e.time_since_epoch() - s.time_since_epoch())
.count();
}
time_point<high_resolution_clock> endTime = high_resolution_clock::now();
printTimeInfo("cpp-ipc test", startTime, endTime, timestamps);
return 0;
}
然后我启动server, client我是用vs2019调试,接收完第一条消息,我直接停止调试,后面再运行就出错了,如标题所示
server.cpp:
#include
#include
#include
#include
using namespace std::chrono;
#include "libipc/ipc.h"
using socket_handle_t = ipc::chan<ipc::relat::single, ipc::relat::single, ipc::trans::unicast>;
FILE* filp = NULL;
bool running = true;
int main(int argc, char* argv[])
{
filp = fopen("data.txt", "w");
}
client.cpp:
#include
#include
#include
#include
#include
#include
using namespace std::chrono;
#include "libipc/ipc.h"
using socket_handle_t = ipc::chan<ipc::relat::single, ipc::relat::single, ipc::trans::unicast>;
long long PKG_NUM = 1024;
long long PKG_SIZE = 64 * 1024LL; //102410244
FILE* filp = NULL;
bool running = true;
void printTimeInfo(const std::string& name, const time_point<high_resolution_clock>& startTime, const time_point<high_resolution_clock>& endTime, const std::vector& timestamps)
{
auto us = duration_cast(endTime.time_since_epoch() - startTime.time_since_epoch())
.count();
std::cout << name << std::endl;
std::cout << "-start: " << duration_cast(startTime.time_since_epoch()).count() << " us" << std::endl;
std::cout << "-end: " << duration_cast(endTime.time_since_epoch()).count() << " us" << std::endl;
std::cout << "-duration: " << us << " us" << std::endl;
std::cout << "ave: " << us / PKG_NUM << " us" << std::endl;
std::cout << "min: " << *std::min_element(timestamps.begin(), timestamps.end()) << " us" << std::endl;
std::cout << "max: " << *std::max_element(timestamps.begin(), timestamps.end()) << " us" << std::endl;
std::cout << "speed: " << PKG_NUM * PKG_SIZE / 1024LL / 1024 / (us / 1000'000.0) << " MB/s" << std::endl;
std::cout << std::endl;
}
int main(int argc, char* argv[])
{
if (argc > 2)
{
PKG_NUM = atoll(argv[1]);
PKG_SIZE = atoll(argv[2]);
}
const char* sz = "a";
const int* pv = &argc;
if (argc > 2)
{
sz = "b";
pv = &argc;
}
socket_handle_t snd("ipc-chn", ipc::sender);
socket_handle_t rcv("ipc-chn2", ipc::receiver);
char* buff = (char*)malloc(PKG_SIZE + 1);
long long i = 0;
for (i = 0; i < PKG_SIZE; ++i)
{
buff[i] = 'A';
}
buff[i] = '\0';
}
然后我启动server, client我是用vs2019调试,接收完第一条消息,我直接停止调试,后面再运行就出错了,如标题所示