up
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define PORT 8080
|
||||
#define BUFFER_SIZE 1024
|
||||
|
||||
char BUFFER[BUFFER_SIZE] = "";
|
||||
char BUFFER_REPLY[BUFFER_SIZE] = "";
|
||||
|
||||
void* run_api() {
|
||||
int server_fd, new_socket;
|
||||
struct sockaddr_in address;
|
||||
int addrlen = sizeof(address);
|
||||
|
||||
// Creating socket file descriptor
|
||||
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
|
||||
perror("socket failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Setting up server address
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_addr.s_addr = INADDR_ANY;
|
||||
address.sin_port = htons(PORT);
|
||||
|
||||
// Binding socket to the port
|
||||
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
|
||||
perror("bind failed");
|
||||
close(server_fd);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Listening for connections
|
||||
if (listen(server_fd, 3) < 0) {
|
||||
perror("listen");
|
||||
close(server_fd);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("API is listening on port %d\n", PORT);
|
||||
|
||||
// Loop to accept and handle multiple connections
|
||||
while (1) {
|
||||
// Accepting a new connection
|
||||
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
|
||||
perror("accept");
|
||||
close(server_fd);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Clear buffer
|
||||
memset(BUFFER_REPLY, 0, BUFFER_SIZE);
|
||||
|
||||
// Reading data from the client
|
||||
int bytes_read = read(new_socket, BUFFER, BUFFER_SIZE - 1);
|
||||
if (bytes_read < 0) {
|
||||
perror("read");
|
||||
// handle error
|
||||
} else {
|
||||
BUFFER[bytes_read] = '\0'; // Properly terminate the string
|
||||
}
|
||||
|
||||
// Sending response to the client from main file
|
||||
while (1) {
|
||||
if (strcmp("", BUFFER_REPLY) != 0) {
|
||||
send(new_socket, BUFFER_REPLY, strlen(BUFFER_REPLY), 0);
|
||||
break;
|
||||
}
|
||||
sleep(0.01);
|
||||
}
|
||||
|
||||
// Closing the current socket
|
||||
close(new_socket);
|
||||
}
|
||||
|
||||
// Closing the server socket
|
||||
close(server_fd);
|
||||
}
|
||||
|
||||
char* get_command() {
|
||||
while (1) {
|
||||
if (strcmp("", BUFFER) != 0) {
|
||||
char* command;
|
||||
strcpy(command, BUFFER);
|
||||
// clear buffer
|
||||
memset(BUFFER, 0, BUFFER_SIZE);
|
||||
return command;
|
||||
}
|
||||
sleep(0.01);
|
||||
}
|
||||
}
|
||||
|
||||
void send_reply(char* reply) {
|
||||
strcpy(BUFFER_REPLY, reply);
|
||||
}
|
||||
|
||||
bool cmp(char* reply, char* command) {
|
||||
if (strcmp(reply, command) == 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void start_api(void** thread_handle) {
|
||||
pthread_t thread;
|
||||
if (pthread_create(&thread, NULL, run_api, NULL) != 0) {
|
||||
fprintf(stderr, "Failed to create thread\n");
|
||||
}
|
||||
pthread_detach(thread);
|
||||
*thread_handle = (void*)thread;
|
||||
}
|
||||
|
||||
void stop_api(void** thread_handle) {
|
||||
pthread_cancel((pthread_t)thread_handle);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user