CppServer  1.0.0.0
C++ Server Library
nanomsg_pair_client.cpp

Nanomsg pair client example

#include "threads/thread.h"
#include <iostream>
#include <memory>
class ExamplePairClient : public CppServer::Nanomsg::PairClient
{
public:
protected:
void onConnected() override
{
std::cout << "Nanomsg pair client connected" << std::endl;
}
void onDisconnected() override
{
std::cout << "Nanomsg pair client disconnected" << std::endl;
// Wait for a while...
CppCommon::Thread::Sleep(1000);
// Try to connect again
}
void onReceived(CppServer::Nanomsg::Message& message) override
{
std::cout << "Incoming: " << message << std::endl;
}
void onError(int error, const std::string& message) override
{
std::cout << "Nanomsg pair client caught an error with code " << error << "': " << message << std::endl;
}
};
int main(int argc, char** argv)
{
// Nanomsg pair server address
std::string address = "tcp://127.0.0.1:6667";
if (argc > 1)
address = argv[1];
std::cout << "Nanomsg pair server address: " << address << std::endl;
// Create a new Nanomsg pair client
auto client = std::make_shared<ExamplePairClient>(address);
// Connect the client
std::cout << "Client connecting...";
client->Connect();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the client or '!' to reconnect the client..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Disconnect the client
if (line == "!")
{
std::cout << "Client disconnecting...";
client->Disconnect();
std::cout << "Done!" << std::endl;
continue;
}
// Send the entered text to the server
client->Send(line);
}
// Disconnect the client
std::cout << "Client disconnecting...";
client->Disconnect();
std::cout << "Done!" << std::endl;
return 0;
}