CHATGPT AI
include
"../../util/logger.h"
#include "../../util/configuration.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>


int main(int argc, char *argv[]) {

if (argc != 2) {
printf("Usage: %s [config_file]\n", argv[0]);
exit(EXIT_FAILURE);
}

Configuration config;

if (!config.load(argv[1])) {
printf("Error loading configuration file\n");
exit(EXIT_FAILURE);
}

Logger logger;

if (!logger.init(&config)) {
printf("Error initializing logger\n");
exit(EXIT_FAILURE); // TODO: Cleanup resources? Close files? Free memory? etc... (see destructors) Or just rely on OS to clean up after us? (not sure which is better) Maybe have a cleanup function in each class that gets called in main() before exit() ? Or maybe not needed since we're exiting anyway? I don't know... I'm just rambling now... lol :) -Jared 12/13/19 11:47pm PST

}

// Create a socket and bind it to the port specified in the config file, then listen for connections from clients on that port:

int sockfd; // socket file descriptor (returned by socket())

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { // create a new IPv4 TCP socket and store its file descriptor in sockfd (if creation fails, sockfd will be set to -1)

printf("Error creating socket\n"); // print error message if creation failed and exit with failure status code:

exit(EXIT_FAILURE); // TODO: Cleanup resources? Close files? Free memory? etc... (see destructors) Or just rely on OS to clean up after us? (not sure which is better) Maybe have a cleanup function in each class that gets called in main() before exit() ? Or maybe not needed since we're exiting anyway? I don't know... I'm just rambling now... lol :) -Jared 12/14/19 12:01am PST

} else { // if creation succeeded:

struct sockaddr_in serveraddr; // server address struct (used by bind())

memset(&serveraddr, 0, sizeof(serveraddr)); // zero out server address struct before using it (for security reasons)

serveraddr.sin_family = AF_INET; // set address family to IPv4 internet protocols (AF stands for Address Family)

serveraddr.sin_port = htons((uint16_t)(atoi((const char*)config["port"]))); // convert port number from string to int and store it in network byte order format (htons stands for Host TO Network Short) NOTE: htons() is used because the network expects data in network byte order format while most systems store data internally in host byte order format NOTE 2: atoi() is used because config["port"] returns a string containing the port number as read from the config file NOTE 3: uint16_t is an unsigned 16-bit integer type defined by C99 standard (it's basically like an unsigned short int but guaranteed to be exactly 16 bits wide regardless of platform or compiler implementation details) NOTE 4: htons() must be used because some systems may use big-endian while others may use little-endian when storing short integers internally so this ensures consistent behavior across all platforms regardless of endianness NOTE 5: ntohs() can be used to convert back from network byte order format to host byte order format if needed -Jared 12/14/19 12:20am PST NOTE 6: For more info about endianness see https://en.wikipedia.org/wiki/Endianness and https://www.geeksforgeeks.org/little-and-big-endian-mystery/?ref=lbp -Jared 12/14/19 1:09am PST NOTE 7: For more info about C99 integer types see https://en.cppreference
"../../util/logger.h" #include "../../util/configuration.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int main(int argc, char *argv[]) { if (argc != 2) { printf("Usage: %s [config_file]\n", argv[0]); exit(EXIT_FAILURE); } Configuration config; if (!config.load(argv[1])) { printf("Error loading configuration file\n"); exit(EXIT_FAILURE); } Logger logger; if (!logger.init(&config)) { printf("Error initializing logger\n"); exit(EXIT_FAILURE); // TODO: Cleanup resources? Close files? Free memory? etc... (see destructors) Or just rely on OS to clean up after us? (not sure which is better) Maybe have a cleanup function in each class that gets called in main() before exit() ? Or maybe not needed since we're exiting anyway? I don't know... I'm just rambling now... lol :) -Jared 12/13/19 11:47pm PST } // Create a socket and bind it to the port specified in the config file, then listen for connections from clients on that port: int sockfd; // socket file descriptor (returned by socket()) if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { // create a new IPv4 TCP socket and store its file descriptor in sockfd (if creation fails, sockfd will be set to -1) printf("Error creating socket\n"); // print error message if creation failed and exit with failure status code: exit(EXIT_FAILURE); // TODO: Cleanup resources? Close files? Free memory? etc... (see destructors) Or just rely on OS to clean up after us? (not sure which is better) Maybe have a cleanup function in each class that gets called in main() before exit() ? Or maybe not needed since we're exiting anyway? I don't know... I'm just rambling now... lol :) -Jared 12/14/19 12:01am PST } else { // if creation succeeded: struct sockaddr_in serveraddr; // server address struct (used by bind()) memset(&serveraddr, 0, sizeof(serveraddr)); // zero out server address struct before using it (for security reasons) serveraddr.sin_family = AF_INET; // set address family to IPv4 internet protocols (AF stands for Address Family) serveraddr.sin_port = htons((uint16_t)(atoi((const char*)config["port"]))); // convert port number from string to int and store it in network byte order format (htons stands for Host TO Network Short) NOTE: htons() is used because the network expects data in network byte order format while most systems store data internally in host byte order format NOTE 2: atoi() is used because config["port"] returns a string containing the port number as read from the config file NOTE 3: uint16_t is an unsigned 16-bit integer type defined by C99 standard (it's basically like an unsigned short int but guaranteed to be exactly 16 bits wide regardless of platform or compiler implementation details) NOTE 4: htons() must be used because some systems may use big-endian while others may use little-endian when storing short integers internally so this ensures consistent behavior across all platforms regardless of endianness NOTE 5: ntohs() can be used to convert back from network byte order format to host byte order format if needed -Jared 12/14/19 12:20am PST NOTE 6: For more info about endianness see https://en.wikipedia.org/wiki/Endianness and https://www.geeksforgeeks.org/little-and-big-endian-mystery/?ref=lbp -Jared 12/14/19 1:09am PST NOTE 7: For more info about C99 integer types see https://en.cppreference
0 Comments & Tags 0 condivisioni 1 Views

Password Copied!

Please Wait....