tcpclient.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Name: tcpclient
  3. * Version: 0.1.1
  4. * Description: A simple program to send arbitrary packets
  5. * on a TCP connection.
  6. */
  7. #include "balzebub.h"
  8. using namespace bzbterm;
  9. using namespace bzbnet;
  10. using namespace bzbio;
  11. int main(int argc, char const *argv[]) {
  12. std::string hostname;
  13. std::string service;
  14. std::string packet;
  15. println(fg::bright_yellow, "Hostname:");
  16. getline(std::cin, hostname);
  17. println(fg::bright_yellow, "Port or Service:");
  18. getline(std::cin, service);
  19. println(fg::bright_yellow, "Message (QUIT to end):");
  20. std::string line;
  21. do {
  22. getline(std::cin, line);
  23. if(line != "QUIT") {
  24. packet.append(line);
  25. packet.append("\n");
  26. }
  27. } while(line != "QUIT");
  28. TCPSocket socket;
  29. println(fg::green, "Connecting to ", hostname, "...");
  30. socket.connect(hostname, service);
  31. println(fg::green, "Sending message...");
  32. socket.send(packet);
  33. println(fg::green, "Waiting for response..");
  34. auto data = socket.recv();
  35. println(fg::bright_yellow, "Response:");
  36. println(fg::cyan, std::string(&data[0]));
  37. socket.close();
  38. return 0;
  39. }