<commit_before>#include "client_socket.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>

#include <sstream>
#include <stdexcept>

namespace sockets
{
    Client_socket::Client_socket ( std::string address, int port )
    {
        struct sockaddr_in server_address;
        struct hostent* resolved_server;

        // Create a socket on the internet using the tcp
        socket_address = socket(AF_INET, SOCK_STREAM, 0);
        if ( socket_address < 0 )
            error ( "Client_socket : Couldn't open socket" );


        // Resolve server
        resolved_server = gethostbyname ( address.c_str () );
        if ( resolved_server == NULL )
            error ( "Client_socket : Host not found: \"" + address + "\"" );

        // Empty server_address
        bzero ( ( char* ) &server_address, sizeof ( server_address ) );
        // Use the internet
        server_address.sin_family = AF_INET;
        // Use address of resolved server
        bcopy ( ( char* ) resolved_server->h_addr, ( char* ) &server_address.sin_addr.s_addr, resolved_server->h_length );
        // sin_port has to be in network byte order
        server_address.sin_port = htons ( port );

        // Enable socket address reuse
        int yes = 1;
        int success = setsockopt ( socket_address, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof ( int ) );
        if ( success < 0 )
            error ( "Client_socket : setsockopt failed" );

        // server_address has to be converted to sockaddr* from sockadd_in
        success = connect ( socket_address, ( struct sockaddr* ) &server_address, sizeof ( server_address ) );
        if ( success < 0 )
            error ( "Client_socket : Connecting failed");
    }

    Client_socket::Client_socket ( int socket_address ) :
        socket_address ( socket_address )
    {}

    std::string Client_socket::read_line ()
    {
        // Read until we have at least one line
        while ( next_lines.empty () )
        {
            char buffer [ 1024 ];
            int success = read ( socket_address, buffer, sizeof ( buffer ) - 1 );
            if ( success < 0 )
                error ( "read_line : Reading from socket failed");
            else if ( success == 0 )
                error ( "read_line : Connection closed");

            // Create actual string from vector
            std::string data_string ( buffer, strlen ( buffer ) );<commit_msg>Fix bug in sockets::Client_socket<commit_after>#include "client_socket.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>

#include <sstream>
#include <stdexcept>

namespace sockets
{
    Client_socket::Client_socket ( std::string address, int port )
    {
        struct sockaddr_in server_address;
        struct hostent* resolved_server;

        // Create a socket on the internet using the tcp
        socket_address = socket(AF_INET, SOCK_STREAM, 0);
        if ( socket_address < 0 )
            error ( "Client_socket : Couldn't open socket" );


        // Resolve server
        resolved_server = gethostbyname ( address.c_str () );
        if ( resolved_server == NULL )
            error ( "Client_socket : Host not found: \"" + address + "\"" );

        // Empty server_address
        bzero ( ( char* ) &server_address, sizeof ( server_address ) );
        // Use the internet
        server_address.sin_family = AF_INET;
        // Use address of resolved server
        bcopy ( ( char* ) resolved_server->h_addr, ( char* ) &server_address.sin_addr.s_addr, resolved_server->h_length );
        // sin_port has to be in network byte order
        server_address.sin_port = htons ( port );

        // Enable socket address reuse
        int yes = 1;
        int success = setsockopt ( socket_address, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof ( int ) );
        if ( success < 0 )
            error ( "Client_socket : setsockopt failed" );

        // server_address has to be converted to sockaddr* from sockadd_in
        success = connect ( socket_address, ( struct sockaddr* ) &server_address, sizeof ( server_address ) );
        if ( success < 0 )
            error ( "Client_socket : Connecting failed");
    }

    Client_socket::Client_socket ( int socket_address ) :
        socket_address ( socket_address )
    {}

    std::string Client_socket::read_line ()
    {
        // Read until we have at least one line
        while ( next_lines.empty () )
        {
            char buffer [ 1024 ];
            int success = read ( socket_address, buffer, sizeof ( buffer ) - 1 );
            if ( success < 0 )
                error ( "read_line : Reading from socket failed");
            else if ( success == 0 )
                error ( "read_line : Connection closed");
            buffer [ success ] = '\0';

            // Create actual string from vector
            std::string data_string ( buffer, strlen ( buffer ) );
{'fraction_non_alphanumeric': 0.10493949613171989, 'fraction_numerical': 0.004959333465582225, 'mean_word_length': 2.2550032278889605, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 25, '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': '4317907', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1405, 'n_words': 414}