starting to add new Socket interface

This commit is contained in:
Erik Frojdh
2019-01-23 11:47:13 +01:00
parent 3eca4c0535
commit 99a1c609f1
10 changed files with 212 additions and 21 deletions

View File

@ -0,0 +1,35 @@
#include "ClientSocket.h"
#include <arpa/inet.h>
#include <cstring>
#include <iostream>
#include <unistd.h>
namespace sls {
ClientSocket::ClientSocket(const std::string &host, uint16_t port) : DataSocket(socket(AF_INET, SOCK_STREAM, 0)) {
struct addrinfo hints, *result;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;
if (getaddrinfo(host.c_str(), NULL, &hints, &result) != 0) {
throw std::runtime_error("ClientSocket ERROR: cannot decode host\n");
}
//TODO! Erik, results could have multiple entries do we need to loop through them?
struct sockaddr_in serverAddr {};
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
memcpy((char *)&serverAddr.sin_addr.s_addr,
&((struct sockaddr_in *)result->ai_addr)->sin_addr, sizeof(in_addr_t));
if (connect(getSocketId(), (struct sockaddr *)&serverAddr, sizeof(serverAddr)) != 0){
freeaddrinfo(result);
throw std::runtime_error("ClientSocket ERROR: cannot connect to host\n");
}
freeaddrinfo(result);
}
}; //namespace sls