<commit_before>// clock2: TCP server that periodically writes the time.
// This variation handles connections concurrently.
// See chapter 8 in The Go Programming Language.

#include <stdio.h>
#include <assert.h>
#include <time.h>

#include <libdill.h>

#include "log.h"

coroutine void handleConn(int s)
{
	assert(s >= 0);
	printf("Connection on socket %d\n", s);

	int rc;
	time_t dt;
	char buf[128];
	int written;
	
	while (1) {
		time(&dt);
		written = snprintf(buf, sizeof(buf), "%s",
				asctime(localtime(&dt)));
		assert(written > 0 && written < sizeof(buf));

		rc = bsend(s, buf, written, -1);
		if (rc != 0) break;

		rc = msleep(now() + 1000);
		assert(rc == 0);
	}

	printf("Disonnecting from socket: %d\n", s);

	rc = tcp_close(s, -1);
	if (rc != 0 && errno != ECONNRESET)
		perror("tcp_close");
}

int main(int argc, char *argv[])
{
	int port = 8000;

	if (argc > 1)
		port = atoi(argv[1]);

	struct ipaddr addr;
	int rc = ipaddr_local(&addr, NULL, port, 0);
	if (rc < 0)
		log_fatal("Could build localhost address on port %d", port);

	int ls = tcp_listen(&addr, 10);
	if (ls < 0)
		log_fatal("Could not listen on port %d", port);<commit_msg>Better error messages.<commit_after>// clock2: TCP server that periodically writes the time.
// This variation handles connections concurrently.
// See chapter 8 in The Go Programming Language.

#include <stdio.h>
#include <assert.h>
#include <time.h>

#include <libdill.h>

#include "log.h"

coroutine void handleConn(int s)
{
	assert(s >= 0);
	printf("Connection on socket %d\n", s);

	int rc;
	time_t dt;
	char buf[128];
	int written;
	
	while (1) {
		time(&dt);
		written = snprintf(buf, sizeof(buf), "%s",
				asctime(localtime(&dt)));
		assert(written > 0 && written < sizeof(buf));

		rc = bsend(s, buf, written, -1);
		if (rc != 0) break;

		rc = msleep(now() + 1000);
		assert(rc == 0);
	}

	printf("Disonnecting from socket: %d\n", s);

	rc = tcp_close(s, -1);
	if (rc != 0 && errno != ECONNRESET)
		perror("tcp_close");
}

int main(int argc, char *argv[])
{
	int port = 8000;

	if (argc > 1)
		port = atoi(argv[1]);

	struct ipaddr addr;
	int rc = ipaddr_local(&addr, NULL, port, 0);
	if (rc < 0)
		log_fatal("ipaddr_local localhost:%d", port);

	int ls = tcp_listen(&addr, 10);
	if (ls < 0)
		log_fatal("tcp_listen on port %d", port);
{'fraction_non_alphanumeric': 0.17100694444444445, 'fraction_numerical': 0.024305555555555556, 'mean_word_length': 3.7920997920997923, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '1141344', 'n_tokens_mistral': 977, 'n_tokens_neox': 869, 'n_words': 270}