<commit_before>#include "ImageSender.hpp"

#include <unistd.h> // close
#include <sys/types.h>
#include <sys/stat.h>

#include <opencv2/opencv.hpp>
//#include <opencv2/imgcodecs/imgcodecs.hpp>

ImageSender::ImageSender(int port) :
    image_dims_(cv::Size2i(0, 0)),
    out_path_("D:\\"),
    client_len_(0),
    server_addr_size_(sizeof(server_addr_)),
    port_(port),
    pic_count_(0),
    sock_fdesc_init_(0),
    sock_fdesc_conn_(0) {
  client_len_ = server_addr_size_;
    wrapper = new DataWrapper();
}

void ImageSender::ConnectToNetwork() {

  // Initialize Socket
  sock_fdesc_init_ = socket(AF_INET, SOCK_STREAM, 0);
  if (sock_fdesc_init_ == -1) {
    close(sock_fdesc_init_);
    perror("Couldn't create socket!\n");
    exit(1);
  }

  // Zero out server address struct
  memset((char*)&server_addr_, 0, server_addr_size_);

  // Set server address struct
  server_addr_.sin_family = AF_INET;
  server_addr_.sin_addr.s_addr = INADDR_ANY;
  server_addr_.sin_port = htons(port_);

  // Assign server address to initial socket file descriptor
  if (bind(sock_fdesc_init_, (struct sockaddr*)&server_addr_,
           server_addr_size_) == -1) {
    perror("Couldn't bind initial socket file descriptor!");
    printf("Trying again after killing existing process on port %d...\n",
           port_);
    close(sock_fdesc_init_);
    if (bind(sock_fdesc_init_, (struct sockaddr*)&server_addr_,
             server_addr_size_) == -1) {
      perror("Couldn't bind initial socket file descriptor after retry!");
      exit(1);
    }
    printf("Successful bind to port %d after killing previous process\n",
           port_);
  }

  // Enable listening on initial socket file descriptor
  listen(sock_fdesc_init_, 5);

  // Block process until connection with client has been established.
  // 'client_fdesc' set as new file descriptor to be used for communication
  sock_fdesc_conn_ = accept(sock_fdesc_init_, (struct sockaddr*)&client_addr_,
                            &client_len_);
  if (sock_fdesc_conn_ == -1) {
    perror("ERROR! Client couldn't connect!");
    exit(1);
  }
}

void ImageSender::ConnectToNetwork(char * hostname, int port){
	struct addrinfo addrinfo_hints;
	struct addrinfo* addrinfo_resp;

	  // Specify criteria for address structs to be returned by getAddrinfo
	memset(&addrinfo_hints, 0, sizeof(addrinfo_hints));
	  addrinfo_hints.ai_socktype = SOCK_STREAM;
	  addrinfo_hints.ai_family = AF_INET;

	  // Populate addr_info_resp with address responses matching hints
	  if (getaddrinfo(hostname_, std::to_string(port_).c_str(),
	                  &addrinfo_hints, &addrinfo_resp) != 0) {
	    perror("Couldn't connect to host!");
	    exit(1);
	  }

	  // Create socket file descriptor for server
	  socket_fdesc_ = socket(addrinfo_resp->ai_family, addrinfo_resp->ai_socktype,
	                        addrinfo_resp->ai_protocol);
	  if (socket_fdesc_ == -1) {<commit_msg>Fix a but in ConnectToNetwork func.<commit_after>#include "ImageSender.hpp"

#include <unistd.h> // close
#include <sys/types.h>
#include <sys/stat.h>

#include <opencv2/opencv.hpp>
//#include <opencv2/imgcodecs/imgcodecs.hpp>

ImageSender::ImageSender(int port) :
    image_dims_(cv::Size2i(0, 0)),
    out_path_("D:\\"),
    client_len_(0),
    server_addr_size_(sizeof(server_addr_)),
    port_(port),
    pic_count_(0),
    sock_fdesc_init_(0),
    sock_fdesc_conn_(0) {
  client_len_ = server_addr_size_;
    wrapper = new DataWrapper();
}

void ImageSender::ConnectToNetwork() {

  // Initialize Socket
  sock_fdesc_init_ = socket(AF_INET, SOCK_STREAM, 0);
  if (sock_fdesc_init_ == -1) {
    close(sock_fdesc_init_);
    perror("Couldn't create socket!\n");
    exit(1);
  }

  // Zero out server address struct
  memset((char*)&server_addr_, 0, server_addr_size_);

  // Set server address struct
  server_addr_.sin_family = AF_INET;
  server_addr_.sin_addr.s_addr = INADDR_ANY;
  server_addr_.sin_port = htons(port_);

  // Assign server address to initial socket file descriptor
  if (bind(sock_fdesc_init_, (struct sockaddr*)&server_addr_,
           server_addr_size_) == -1) {
    perror("Couldn't bind initial socket file descriptor!");
    printf("Trying again after killing existing process on port %d...\n",
           port_);
    close(sock_fdesc_init_);
    if (bind(sock_fdesc_init_, (struct sockaddr*)&server_addr_,
             server_addr_size_) == -1) {
      perror("Couldn't bind initial socket file descriptor after retry!");
      exit(1);
    }
    printf("Successful bind to port %d after killing previous process\n",
           port_);
  }

  // Enable listening on initial socket file descriptor
  listen(sock_fdesc_init_, 5);

  // Block process until connection with client has been established.
  // 'client_fdesc' set as new file descriptor to be used for communication
  sock_fdesc_conn_ = accept(sock_fdesc_init_, (struct sockaddr*)&client_addr_,
                            &client_len_);
  if (sock_fdesc_conn_ == -1) {
    perror("ERROR! Client couldn't connect!");
    exit(1);
  }
}

void ImageSender::ConnectToNetwork(char * hostname, int port){
	struct addrinfo addrinfo_hints;
	struct addrinfo* addrinfo_resp;

	hostname_ = hostname;
	port_ = port;

	  // Specify criteria for address structs to be returned by getAddrinfo
	memset(&addrinfo_hints, 0, sizeof(addrinfo_hints));
	  addrinfo_hints.ai_socktype = SOCK_STREAM;
	  addrinfo_hints.ai_family = AF_INET;

	  // Populate addr_info_resp with address responses matching hints
	  if (getaddrinfo(hostname_, std::to_string(port_).c_str(),
	                  &addrinfo_hints, &addrinfo_resp) != 0) {
	    perror("Couldn't connect to host!");
	    exit(1);
	  }

	  // Create socket file descriptor for server
	  socket_fdesc_ = socket(addrinfo_resp->ai_family, addrinfo_resp->ai_socktype,
	                        addrinfo_resp->ai_protocol);
	  if (socket_fdesc_ == -1) {
{'fraction_non_alphanumeric': 0.1148936170212766, 'fraction_numerical': 0.007829787234042554, 'mean_word_length': 3.619496855345912, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1636081', 'n_tokens_mistral': 1990, 'n_tokens_neox': 1925, 'n_words': 449}