mirror of
https://github.com/hmaxnl/netex.git
synced 2025-01-18 23:44:20 +01:00
Reworking socket fd creation, added simple http response to send to the client.
This commit is contained in:
parent
c43520a28d
commit
ed1d311dd9
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
void netex_init(void);
|
void netex_init(void);
|
||||||
void netex_shutdown(void);
|
void netex_shutdown(void);
|
||||||
|
// Creates a socket and bind to that socket on the given port and hostname. If done with the socket close it with 'close()'
|
||||||
int setup_socket(int port, const char* hostname);
|
int setup_socket(int port, const char* hostname);
|
||||||
|
|
||||||
#endif //NETEX_H
|
#endif //NETEX_H
|
|
@ -54,7 +54,8 @@ int setup_socket(const int port, const char* hostname)
|
||||||
ERROR("Could not get the address info of hostname: '%s'", hostname);
|
ERROR("Could not get the address info of hostname: '%s'", hostname);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
for (const struct addrinfo* tmp_addrinfo = cur_addrdinfo; tmp_addrinfo != NULL; tmp_addrinfo = tmp_addrinfo->ai_next)
|
//TODO: Need fihure out why data from getaddrinfo is not working.
|
||||||
|
/*for (const struct addrinfo* tmp_addrinfo = cur_addrdinfo; tmp_addrinfo != NULL; tmp_addrinfo = tmp_addrinfo->ai_next)
|
||||||
{
|
{
|
||||||
sock_fd = socket(tmp_addrinfo->ai_family, tmp_addrinfo->ai_socktype, tmp_addrinfo->ai_protocol);
|
sock_fd = socket(tmp_addrinfo->ai_family, tmp_addrinfo->ai_socktype, tmp_addrinfo->ai_protocol);
|
||||||
if (sock_fd == -1)
|
if (sock_fd == -1)
|
||||||
|
@ -62,7 +63,24 @@ int setup_socket(const int port, const char* hostname)
|
||||||
if (bind(sock_fd, tmp_addrinfo->ai_addr, tmp_addrinfo->ai_addrlen) == 0)
|
if (bind(sock_fd, tmp_addrinfo->ai_addr, tmp_addrinfo->ai_addrlen) == 0)
|
||||||
break; // Succesfull bind to the hostname
|
break; // Succesfull bind to the hostname
|
||||||
close(sock_fd); // Close the socket en if there are more 'addrinfo' retry in the while loop.
|
close(sock_fd); // Close the socket en if there are more 'addrinfo' retry in the while loop.
|
||||||
}
|
}*/
|
||||||
freeaddrinfo(cur_addrdinfo);
|
freeaddrinfo(cur_addrdinfo);
|
||||||
|
struct sockaddr_in sockaddr;
|
||||||
|
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if (sock_fd < 0)
|
||||||
|
{
|
||||||
|
ERROR("Failed to created socket");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
sockaddr.sin_family = AF_INET;
|
||||||
|
sockaddr.sin_port = htons(6920);
|
||||||
|
sockaddr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
const int bind_result = bind(sock_fd, (struct sockaddr*)&sockaddr, sizeof(sockaddr));
|
||||||
|
if (bind_result != 0)
|
||||||
|
{
|
||||||
|
ERROR("Could not bind!");
|
||||||
|
close(sock_fd);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
return sock_fd;
|
return sock_fd;
|
||||||
}
|
}
|
|
@ -3,14 +3,23 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "netex.h"
|
#include "netex.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#define CONFIG_PATH "srv_config.json"
|
#define CONFIG_PATH "srv_config.json"
|
||||||
|
|
||||||
CONFIGURATION* srv_configuration;
|
CONFIGURATION* srv_configuration;
|
||||||
int server_sockfd;
|
int server_sockfd;
|
||||||
struct sockaddr_in server_sockaddr;
|
struct sockaddr_in server_sockaddr;
|
||||||
|
pthread_t listen_thread;
|
||||||
|
bool b_listen;
|
||||||
|
|
||||||
|
void server_listen();
|
||||||
|
|
||||||
int initialize_server(int argc, char* argv[])
|
int initialize_server(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
|
@ -45,5 +54,48 @@ int initialize_server(int argc, char* argv[])
|
||||||
|
|
||||||
int server_execute(void)
|
int server_execute(void)
|
||||||
{
|
{
|
||||||
|
const int thread_create_result = pthread_create(&listen_thread, NULL, server_listen, NULL);
|
||||||
|
if (thread_create_result != 0)
|
||||||
|
{
|
||||||
|
ERROR("Failed to create main listening thread, pthread error: %i", thread_create_result);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int listen_thread_result;
|
||||||
|
const int join_result = pthread_join(listen_thread, (void**)&listen_thread_result);
|
||||||
|
if (join_result == 0)
|
||||||
|
WARN("Listen thread has exited with erros!");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void server_listen()
|
||||||
|
{
|
||||||
|
PRINT_LINE("Listening...");
|
||||||
|
int some_return_code = 0;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
const int listen_result = listen(server_sockfd, 1);
|
||||||
|
if (listen_result == -1)
|
||||||
|
{
|
||||||
|
some_return_code = errno;
|
||||||
|
WARN("Failed while listening, error: %i", some_return_code);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const int accepted_fd = accept(server_sockfd, NULL, NULL);
|
||||||
|
if (accepted_fd == -1)
|
||||||
|
{
|
||||||
|
const int accept_error = errno;
|
||||||
|
WARN("Could not establish connection, error: %i", accept_error);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// Test response
|
||||||
|
PRINT_LINE("Accepted clientfd: %i", accepted_fd);
|
||||||
|
const char* header = "HTTP/1.1 200 OK\r\nContent-Length: 14\r\nConnection: close\r\n\r\nHello, world!\n";
|
||||||
|
const int header_len = strlen(header);
|
||||||
|
const int send_result = send(accepted_fd, header, header_len, 0);
|
||||||
|
if (send_result == -1)
|
||||||
|
WARN("Error sending message to client!");
|
||||||
|
close(accepted_fd);
|
||||||
|
//TODO: Send client to thread that will handle further connection things.
|
||||||
|
}
|
||||||
|
pthread_exit((void*)some_return_code);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user