|
@@ -9,6 +9,7 @@
|
|
|
#include <thread>
|
|
|
#include <cstring>
|
|
|
#include <QDebug>
|
|
|
+#include <QByteArray>
|
|
|
#include <string>
|
|
|
#include <iostream>
|
|
|
|
|
@@ -37,11 +38,27 @@ void input_loop(char buffer[]) {
|
|
|
//}
|
|
|
|
|
|
|
|
|
-Client::Client(QWidget *parent, int count, char** arguments) : QDialog(parent), argc(count), argv(arguments) {}
|
|
|
+Client::Client(QWidget *parent, int count, char** arguments) : QDialog(parent), argc(count), argv(arguments), m_client_socket_fd(-1) {}
|
|
|
+
|
|
|
+ void Client::handleMessages() {
|
|
|
+ char receive_buffer[2048];
|
|
|
+ for (;;) {
|
|
|
+ memset(receive_buffer, 0, 2048);
|
|
|
+ int bytes_received = 0;
|
|
|
+ bytes_received = recv(m_client_socket_fd, receive_buffer, 2048 - 2, 0);
|
|
|
+ receive_buffer[2047] = 0;
|
|
|
+ if (bytes_received == 0) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ emit Client::messageReceived(QString::fromUtf8(receive_buffer, 2048));
|
|
|
+ }
|
|
|
+ memset(receive_buffer, 0, 2048);
|
|
|
+ ::close(m_client_socket_fd);
|
|
|
+ }
|
|
|
|
|
|
int Client::start() {
|
|
|
- int client_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
- if (client_socket_fd != -1) {
|
|
|
+ m_client_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
+ if (m_client_socket_fd != -1) {
|
|
|
sockaddr_in server_socket;
|
|
|
char* end;
|
|
|
server_socket.sin_family = AF_INET;
|
|
@@ -52,37 +69,19 @@ Client::Client(QWidget *parent, int count, char** arguments) : QDialog(parent),
|
|
|
|
|
|
int socket_option = 1;
|
|
|
// Free up the port to begin listening again
|
|
|
- setsockopt(client_socket_fd, SOL_SOCKET, SO_REUSEADDR, &socket_option,
|
|
|
+ setsockopt(m_client_socket_fd, SOL_SOCKET, SO_REUSEADDR, &socket_option,
|
|
|
sizeof(socket_option));
|
|
|
|
|
|
server_socket.sin_port = htons(port_value);
|
|
|
inet_pton(AF_INET, argv[1], &server_socket.sin_addr.s_addr);
|
|
|
|
|
|
- if (::connect(client_socket_fd, (sockaddr*)&server_socket,
|
|
|
+ if (::connect(m_client_socket_fd, (sockaddr*)&server_socket,
|
|
|
sizeof(server_socket)) != -1) {
|
|
|
- char send_buffer[2048];
|
|
|
- char receive_buffer[2048];
|
|
|
- for (;;) {
|
|
|
- memset(send_buffer, 0, 2048);
|
|
|
- memset(receive_buffer, 0, 2048);
|
|
|
- int bytes_received = 0;
|
|
|
- std::thread user_input_loop(input_loop, send_buffer);
|
|
|
-
|
|
|
- if (user_input_loop.joinable()) {
|
|
|
- user_input_loop.join();
|
|
|
- send(client_socket_fd, send_buffer, 2048, 0);
|
|
|
- bytes_received = recv(client_socket_fd, receive_buffer, 2048 - 2, 0);
|
|
|
- receive_buffer[2047] = 0;
|
|
|
- if (bytes_received == 0) {
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- memset(send_buffer, 0, 2048);
|
|
|
- memset(receive_buffer, 0, 2048);
|
|
|
+// std::thread t(handleMessages);
|
|
|
+ } else {
|
|
|
+ qDebug() << errno;
|
|
|
+ ::close(m_client_socket_fd);
|
|
|
}
|
|
|
- qDebug() << errno;
|
|
|
- ::close(client_socket_fd);
|
|
|
return 0;
|
|
|
}
|
|
|
}
|
|
@@ -92,4 +91,11 @@ void Client::requestNewFortune() {
|
|
|
tr("Gibs me dat fortune, punk!"));
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+void Client::sendMessage(const QString& s) {
|
|
|
+ if (m_client_socket_fd != -1) {
|
|
|
+ QByteArray byte_array = s.toLocal8Bit();
|
|
|
+ ::send(m_client_socket_fd, byte_array.data(), byte_array.size(), 0);
|
|
|
+ } else {
|
|
|
+ qDebug() << "You must first open a connection";
|
|
|
+ }
|
|
|
+}
|