This commit is contained in:
justuser-31 2025-02-07 11:00:59 +03:00
parent b4079c7ce0
commit 045457f520
5 changed files with 271 additions and 0 deletions

36
api.c Normal file
View File

@ -0,0 +1,36 @@
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include "api.h"
int main() {
void* thread_handle;
start_api(&thread_handle);
char* command;
while(1) {
// Clear buffer
command = get_command();
printf("Recieved: %s\n", command);
if (cmp(command, "ping")) {
send_reply("pong");
printf("Sended: pong\n");
}
else if (cmp(command, "hello")) {
send_reply("hi!");
printf("Sended: hello\n");
}
else {
send_reply("Unknown command.");
printf("Sended: Unknown command.\n");
}
sleep(0.01);
}
stop_api(thread_handle);
return 0;
}

123
api.h Normal file
View File

@ -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);
}

59
client_api.h Normal file
View File

@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdbool.h>
#define PORT 8080
#define BUFFER_SIZE 1024
char BUFFER_REPLY[BUFFER_SIZE] = "";
char* send_command(char* command) {
int sock = 0;
struct sockaddr_in serv_addr;
// Clear buffer
memset(BUFFER_REPLY, 0, BUFFER_SIZE);
// Creating socket file descriptor
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return "";
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Converting IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported \n");
return "";
}
// Connecting to the server
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed \n");
return "";
}
// Sending data to the server
send(sock, command, strlen(command), 0);
// Receiving data from the server
read(sock, BUFFER_REPLY, BUFFER_SIZE);
// Closing the socket
close(sock);
return BUFFER_REPLY;
}
bool cmp(char* reply, char* command) {
if (strcmp(reply, command) == 0) {
return true;
} else {
return false;
}
}

17
main.c Normal file
View File

@ -0,0 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include "client_api.h"
int main() {
printf("Sended: hello\n");
printf("Received: %s\n", send_command("hello"));
printf("Sended: ping\n");
printf("Received: %s\n", send_command("ping"));
return 0;
}

36
main.py Normal file
View File

@ -0,0 +1,36 @@
import socket
HOST = '127.0.0.1'
PORT = 8080
BUFFER_SIZE = 1024
def send_command(command):
# Create a socket object
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Connect to the server
client_socket.connect((HOST, PORT))
# Send the command
client_socket.sendall(command.encode('utf-8'))
# Receive the response
response = client_socket.recv(BUFFER_SIZE).decode('utf-8')
# Close the socket
client_socket.close()
return response
except Exception as e:
print(f"An error occurred: {e}")
def main():
print('Sended: ping')
print(f'Received: {send_command("ping")}')
print('Sended: hello')
print(f'Received: {send_command("hello")}')
if __name__ == "__main__":
main()