7 Commits
Author SHA1 Message Date
legonzaur 8c1acfce45 fix server dockerfile
CMake / build (push) Failing after 4s
2025-02-28 12:39:10 +01:00
legonzaur bf7020f791 first version of client
CMake / build (push) Failing after 4s
2025-02-28 12:34:45 +01:00
legonzaur e078a1023a Move into directory
CMake / build (push) Failing after 4s
2025-02-28 11:40:57 +01:00
legonzaur 294389980b fix : send qotd message directly after TCP connection
CMake / build (push) Failing after 4s
2025-02-28 11:21:33 +01:00
legonzaur bfd1f305e8 Add dockerfile
CMake / build (push) Failing after 35s
2025-02-28 11:15:37 +01:00
Arthur Coppey 9ea6089327 Update cmake.yml 2021-11-30 13:08:31 +00:00
Arthur Coppey 6c834e05f9 Create cmake.yml 2021-11-30 12:25:52 +00:00
18 changed files with 217 additions and 54 deletions
+38
View File
@@ -0,0 +1,38 @@
name: CMake
on:
push:
branches: [ master, dev ]
pull_request:
branches: [ master, dev ]
env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: RelWithDebInfo
jobs:
build:
# The CMake configure and build commands are platform agnostic and should work equally
# well on Windows or Mac. You can convert this to a matrix build if you need
# cross-platform coverage.
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
- name: Build
# Build your program with the given configuration
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
- name: Test
working-directory: ${{github.workspace}}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest -C ${{env.BUILD_TYPE}}
+1
View File
@@ -1,3 +1,4 @@
# Project exclude paths
/cmake-build-debug/
/.idea/
build
+7
View File
@@ -0,0 +1,7 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}
+13
View File
@@ -0,0 +1,13 @@
FROM git.legonzaur.fr/legonzaur/qotd/builder:latest AS builder
WORKDIR /opt/builder
COPY ./server .
RUN cmake -B build -DSTATIC_BUILD=true
WORKDIR build
RUN make
FROM scratch
COPY --from=builder /opt/builder/build/qotd /
COPY quotes.txt /
ENTRYPOINT ["/qotd"]
+3
View File
@@ -0,0 +1,3 @@
FROM alpine:latest
RUN apk add --no-cache cmake make gcc musl-dev
+12
View File
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.19)
project(qotd C)
message("STATIC_BUILD=${STATIC_BUILD}")
set(CMAKE_C_STANDARD 99)
add_executable(qotd_c main.c)
if ( STATIC_BUILD )
target_link_libraries(qotd -static)
endif()
+1
View File
@@ -0,0 +1 @@
#include "client.h"
+3
View File
@@ -0,0 +1,3 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+1
View File
@@ -0,0 +1 @@
../server/config.h
+57
View File
@@ -0,0 +1,57 @@
#include <arpa/inet.h> // inet_addr()
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h> // bzero()
#include <sys/socket.h>
#include <unistd.h> // read(), write(), close()
#include <errno.h>
#include "config.h"
#define MAX 80
#define PORT 17
#define SA struct sockaddr
int main()
{
int sockfd, connfd;
struct sockaddr_in servaddr4, cli;
unsigned int servaddrlength;
// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
// assign IP, PORT
servaddr4.sin_family = AF_INET;
servaddr4.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr4.sin_port = htons(PORT);
int status = connect(sockfd, (SA *)&servaddr4, sizeof servaddr4);
// connect the client socket to server socket
if (status != 0)
{
printf("%i\n", errno);
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");
struct sockaddr *servaddr = (SA *)&servaddr4;
unsigned int ss = sizeof servaddr;
// function for chat
char clientMessage[MESSAGE_STRING_LENGTH] = {0};
recvfrom(sockfd, clientMessage, MESSAGE_STRING_LENGTH, 0, servaddr, &ss);
printf("From Server : %s", clientMessage);
// close the socket
close(sockfd);
}
+1
View File
@@ -0,0 +1 @@
première ligne \n Deuxième ligne
+6
View File
@@ -1,6 +1,12 @@
cmake_minimum_required(VERSION 3.19)
project(qotd C)
message("STATIC_BUILD=${STATIC_BUILD}")
set(CMAKE_C_STANDARD 99)
add_executable(qotd main.c quote.c quote.h server.c server.h config.h)
if ( STATIC_BUILD )
target_link_libraries(qotd -static)
endif()
View File
View File
+19 -10
View File
@@ -5,28 +5,33 @@
#include "quote.h"
#include "config.h"
void getRandomQuote(char quote[2048]) {
void getRandomQuote(char quote[2048])
{
int lineCount;
long line;
if ((lineCount = getFileLineCount(QUOTES_FILE)) == -1) {
if ((lineCount = getFileLineCount(QUOTES_FILE)) == -1)
{
puts("file does not exist");
exit(-1);
}
line = random() % lineCount;
line = rand() % lineCount;
getQuote(line, quote);
}
void getQuote(long line, char quote[2048]) {
void getQuote(long line, char quote[2048])
{
FILE *list;
list = fopen(QUOTES_FILE, "r");
/*skip lines*/
while (line > 0) {
if (fgetc(list) == '\n') {
while (line > 0)
{
if (fgetc(list) == '\n')
{
line--;
}
}
@@ -36,18 +41,22 @@ void getQuote(long line, char quote[2048]) {
fclose(list);
}
int getFileLineCount(char *filename) {
int getFileLineCount(char *filename)
{
char c;
int lineCount = 0;
FILE *quotesFile = fopen(filename, "r");
if (quotesFile == NULL) {
if (quotesFile == NULL)
{
printf("no quote file found at \"%s\"\n", filename);
return -1;
}
while ((c = (char) fgetc(quotesFile)) != EOF) {
if (c == '\n') {
while ((c = (char)fgetc(quotesFile)) != EOF)
{
if (c == '\n')
{
lineCount++;
}
}
-2
View File
@@ -8,8 +8,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bits/types/FILE.h>
#include <bits/types/struct_FILE.h>
void getRandomQuote(char quote[2048]);
+40 -27
View File
@@ -9,28 +9,35 @@
extern int errno;
_Noreturn void udpListen(int serverSocket, struct sockaddr * clientAddress, unsigned long addressLength) {
_Noreturn void udpListen(int serverSocket, struct sockaddr *clientAddress, unsigned long addressLength)
{
char clientMessage[MESSAGE_STRING_LENGTH], quote[MESSAGE_STRING_LENGTH];
for (;;) {
for (;;)
{
// receive data
if (recvfrom(serverSocket, clientMessage, MESSAGE_STRING_LENGTH, 0, clientAddress, (socklen_t *) &addressLength) >= 0) {
if (recvfrom(serverSocket, clientMessage, MESSAGE_STRING_LENGTH, 0, clientAddress, (socklen_t *)&addressLength) >= 0)
{
printf("received: %s\n", clientMessage);
// send data
getRandomQuote(quote);
printf("sending: %s\n", quote);
if (sendto(serverSocket, quote, strlen(quote), 0, clientAddress, addressLength) == -1) {
if (sendto(serverSocket, quote, strlen(quote), 0, clientAddress, addressLength) == -1)
{
perror("udp: sendto");
}
} else {
}
else
{
perror("udp: recvfrom");
}
}
}
void tcpListen(int serverSocket, struct sockaddr * clientAddress, unsigned long addressLength) {
void tcpListen(int serverSocket, struct sockaddr *clientAddress, unsigned long addressLength)
{
int clientSocket;
char quote[MESSAGE_STRING_LENGTH] = "", clientMessage[MESSAGE_STRING_LENGTH] = "";
@@ -39,42 +46,38 @@ void tcpListen(int serverSocket, struct sockaddr * clientAddress, unsigned long
// accept connections
// TODO: t h r e a d s
while ((clientSocket = accept(serverSocket, clientAddress, (socklen_t *) &addressLength)) >= 0) {
while ((clientSocket = accept(serverSocket, clientAddress, (socklen_t *)&addressLength)) >= 0)
{
puts("client connected");
// receive data
if (recv(clientSocket, clientMessage, MESSAGE_STRING_LENGTH, 0) >= 0) {
printf("client says: %s\n", clientMessage);
// send data
getRandomQuote(quote);
printf("responding: \"%s\"\n", quote);
if (send(clientSocket, quote, strlen(quote), 0) == -1) {
if (send(clientSocket, quote, strlen(quote), 0) == -1)
{
perror("tcp: send");
}
} else {
perror("tcp: recv");
}
close(clientSocket);
puts("closed connection");
}
if (clientSocket < 0) {
if (clientSocket < 0)
{
puts("client connection failed");
exit(3);
}
}
void server(int tcp, int ipv6) {
void server(int tcp, int ipv6)
{
int serverSocket, socketType, socketProtocol;
unsigned long serverAddressLength, clientAddressLength;
struct sockaddr *serverAddress, *clientAddress;
struct sockaddr_in serverAddress4, clientAddress4;
struct sockaddr_in6 serverAddress6, clientAddress6;
if (ipv6) {
if (ipv6)
{
serverAddress6.sin6_family = AF_INET6;
serverAddress6.sin6_addr = in6addr_any;
serverAddress6.sin6_port = htons(DEFAULT_LISTEN_PORT);
@@ -84,7 +87,9 @@ void server(int tcp, int ipv6) {
clientAddress6.sin6_family = AF_INET6;
clientAddress = (struct sockaddr *)&clientAddress6;
clientAddressLength = sizeof clientAddress6;
} else {
}
else
{
serverAddress4.sin_family = AF_INET;
serverAddress4.sin_addr.s_addr = INADDR_ANY;
serverAddress4.sin_port = htons(DEFAULT_LISTEN_PORT);
@@ -96,32 +101,40 @@ void server(int tcp, int ipv6) {
clientAddressLength = sizeof clientAddress4;
}
if (tcp) {
if (tcp)
{
socketType = SOCK_STREAM;
socketProtocol = IPPROTO_TCP;
} else {
}
else
{
socketType = SOCK_DGRAM;
socketProtocol = IPPROTO_UDP;
}
// create sock
serverSocket = socket(serverAddress->sa_family, socketType, socketProtocol);
if (serverSocket == -1) {
if (serverSocket == -1)
{
puts("socket not created");
exit(1);
}
puts("socket created");
// bind sock
if (bind(serverSocket, serverAddress, serverAddressLength) < 0) {
if (bind(serverSocket, serverAddress, serverAddressLength) < 0)
{
puts("bind failed");
exit(2);
}
// TODO: threads to run both at the same time
if (tcp) {
if (tcp)
{
tcpListen(serverSocket, clientAddress, clientAddressLength);
} else {
}
else
{
udpListen(serverSocket, clientAddress, clientAddressLength);
}
}
View File