client.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <client.hpp>
  2. #include <arpa/inet.h>
  3. #include <netdb.h>
  4. #include <string.h>
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8. #include <thread>
  9. #include <cstring>
  10. #include <QDebug>
  11. #include <string>
  12. #include <iostream>
  13. void input_loop(char buffer[]) {
  14. qDebug() << "Enter message: ";
  15. for (;;) {
  16. std::cin >> buffer;
  17. if (buffer[0] != 0) {
  18. return;
  19. }
  20. }
  21. }
  22. //hostCombo(new QComboBox),
  23. // portLineEdit(new QLineEdit),
  24. // getFortuneButton(new QPushButton(tr("Get Fortune"))),
  25. // tcpSocket(new QTcpSocket(this)) {
  26. // setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  27. // in.setDevice(tcpSocket);
  28. // in.setVersion(QDataStream::Qt_4_0);
  29. // QPushButton *button = new QPushButton("Push");
  30. // parent->layout()->addWidget(button);
  31. // connect(button, &QPushButton::clicked, this, &Client::clicked);
  32. // connect(tcpSocket, &QIODevice::readyRead, this, &Client::readFortune);
  33. //}
  34. Client::Client(QWidget *parent, int count, char** arguments) : QDialog(parent), argc(count), argv(arguments) {}
  35. int Client::start() {
  36. int client_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  37. if (client_socket_fd != -1) {
  38. sockaddr_in server_socket;
  39. char* end;
  40. server_socket.sin_family = AF_INET;
  41. auto port_value = strtol(argv[2], &end, 10);
  42. if (port_value < 0 || end == argv[2]) {
  43. return 1;
  44. }
  45. int socket_option = 1;
  46. // Free up the port to begin listening again
  47. setsockopt(client_socket_fd, SOL_SOCKET, SO_REUSEADDR, &socket_option,
  48. sizeof(socket_option));
  49. server_socket.sin_port = htons(port_value);
  50. inet_pton(AF_INET, argv[1], &server_socket.sin_addr.s_addr);
  51. if (::connect(client_socket_fd, (sockaddr*)&server_socket,
  52. sizeof(server_socket)) != -1) {
  53. char send_buffer[2048];
  54. char receive_buffer[2048];
  55. for (;;) {
  56. memset(send_buffer, 0, 2048);
  57. memset(receive_buffer, 0, 2048);
  58. int bytes_received = 0;
  59. std::thread user_input_loop(input_loop, send_buffer);
  60. if (user_input_loop.joinable()) {
  61. user_input_loop.join();
  62. send(client_socket_fd, send_buffer, 2048, 0);
  63. bytes_received = recv(client_socket_fd, receive_buffer, 2048 - 2, 0);
  64. receive_buffer[2047] = 0;
  65. if (bytes_received == 0) {
  66. break;
  67. }
  68. }
  69. }
  70. memset(send_buffer, 0, 2048);
  71. memset(receive_buffer, 0, 2048);
  72. }
  73. qDebug() << errno;
  74. ::close(client_socket_fd);
  75. return 0;
  76. }
  77. }
  78. void Client::requestNewFortune() {
  79. QMessageBox::information(this, tr("Jigga Client"),
  80. tr("Gibs me dat fortune, punk!"));
  81. }