DEV Community

Cover image for Connect to Tru peers using unix socket
Kirill Scherba
Kirill Scherba

Posted on • Edited on

Connect to Tru peers using unix socket

If you can't link the Tru package to your application than use this standalone unix socket server to communicate with any Tru peers.

The Unix socket Server created on golang and can run under Linux or Windows.

The Unix socket Cliens examples created on Go, C, C++ and build-in sample Visual Studio project.

See the part of C++ code to connect to the Unix socket Server:

int main(int argc, char *argv[]) {

  // Get unix socket path
  std::string socket_path = std::string("/tmp/trugw.sock");

  // Get tru peer address
  std::string tru_addr = ":7070";
  if (argc >= 2) {
    tru_addr = std::string(argv[1]);
  }

  std::cout << "Trugw C++ client, "
            << "sock path: " << socket_path << "tru peer: " << socket_path
            << std::endl;

  // Connect to teogw server
  std::cout << "trying to connect...\n";
  Trugw tgw(socket_path, tru_addr);
  if (!tgw.connected()) {
    std::cout << "can't connect\n";
    return 1;
  }
  std::cout << "connected \n";

  // Send messages
  for (int i = 0; i < 50000; i++) {
    std::string msg = "Hello " + std::to_string(i);
    std::cout << "send " << msg << std::endl;
    tgw.send(msg);

    uint8_t buf[1024];
    auto n = tgw.recv((const char *)buf, sizeof(buf), 0);
    std::string s((const char *)buf, n);
    std::cout << "receive " << s << std::endl;
  }

  return 0;
}
Enter fullscreen mode Exit fullscreen mode

See the code and more descriptions and example at: https://github.com/teonet-go/trugw

Top comments (0)