<commit_before>
#include <libclientserver.h>

ServerTCPPolledListener::ServerTCPPolledListener(ServerTCPPolled *Server)
{
	m_Server = Server;
}

ServerTCPPolledListener::~ServerTCPPolledListener()
{
	if (close(m_fd) < 0)
		abort();
}

void ServerTCPPolledListener::Init(int port, const std::string &address)
{
	m_port = port;
	m_addr = address;

	struct sockaddr_in addr;
	size_t addr_len = sizeof(addr);

	m_fd = socket(AF_INET, SOCK_STREAM, 0);
	if (m_fd < 0) {
		std::string err = strerror(errno);
		throw(err);
	}

	memset(&addr, 0, addr_len);

	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = inet_addr(address.c_str());
	addr.sin_port = htons(port);

	if(bind(m_fd, (struct sockaddr *) &addr, addr_len) < 0)
	{
		std::string err = strerror(errno);
		if (close(m_fd) < 0)
			abort();
		throw(err);
	}
    
	if(listen(m_fd, 512) < 0) {
		std::string err = strerror(errno);
		if (close(m_fd) < 0)
			abort();
		throw(err);
	}
}

bool ServerTCPPolledListener::CanRead(const Poller *)
{
	return true;
}<commit_msg>Add setsockopet SO_REUSEADDR to TCP Server and improve error handling<commit_after>
#include <libclientserver.h>

ServerTCPPolledListener::ServerTCPPolledListener(ServerTCPPolled *Server)
{
	m_Server = Server;
}

ServerTCPPolledListener::~ServerTCPPolledListener()
{
	if (close(m_fd) < 0)
		abort();
}

void ServerTCPPolledListener::Init(int port, const std::string &address)
{
	m_port = port;
	m_addr = address;

	struct sockaddr_in addr;
	size_t addr_len = sizeof(addr);

	m_fd = socket(AF_INET, SOCK_STREAM, 0);
	if (m_fd < 0) {
		std::string err = strerror(errno);
		throw(err);
	}

	int enable = 1;
	if (setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
		std::string err = "setsockopt(SO_REUSEADDR): " + std::string(strerror(errno));
		throw(std::runtime_error("setsockopt(SO_REUSEADDR) failed"));
	}

	memset(&addr, 0, addr_len);

	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = inet_addr(address.c_str());
	addr.sin_port = htons(port);

	if(bind(m_fd, (struct sockaddr *) &addr, addr_len) < 0)
	{
		std::string err = "bind: " + std::string(strerror(errno));
		if (close(m_fd) < 0)
			abort();
		throw(std::runtime_error(err));
	}
    
	if(listen(m_fd, 512) < 0) {
		std::string err = "listen: " + std::string(strerror(errno));
		if (close(m_fd) < 0)
			abort();
		throw(std::runtime_error(err));
	}
}

bool ServerTCPPolledListener::CanRead(const Poller *)
{
	return true;
}
{'fraction_non_alphanumeric': 0.15628865979381443, 'fraction_numerical': 0.009896907216494845, 'mean_word_length': 5.173027989821883, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 5, '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': '7035602', 'n_tokens_mistral': 1038, 'n_tokens_neox': 984, 'n_words': 183}