added test client
This commit is contained in:
11
server/main.c
Normal file
11
server/main.c
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "quote.h"
|
||||
#include "server.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// char quote[1024];
|
||||
// getRandomQuote(quote);
|
||||
// printf("%s\n", quote);
|
||||
tcpServer();
|
||||
|
||||
return 0;
|
||||
}
|
||||
24
server/quote.c
Normal file
24
server/quote.c
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by k0rb4k on 30/03/2021.
|
||||
//
|
||||
|
||||
#include "quote.h"
|
||||
|
||||
void getRandomQuote(char quote[1024]) {
|
||||
// TODO: true random between 0 and number of lines in file
|
||||
int rand = 0;
|
||||
getQuote(rand, quote);
|
||||
}
|
||||
|
||||
void getQuote(int line, char quote[1024]) {
|
||||
FILE* list;
|
||||
// char quote[1024];
|
||||
|
||||
// TODO: get specified x line
|
||||
|
||||
list = fopen("quotes.txt", "r");
|
||||
|
||||
fgets(quote, 1024, list);
|
||||
|
||||
fclose(list);
|
||||
}
|
||||
16
server/quote.h
Normal file
16
server/quote.h
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// Created by k0rb4k on 30/03/2021.
|
||||
//
|
||||
|
||||
#ifndef QOTD_QUOTE_H
|
||||
#define QOTD_QUOTE_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <bits/types/FILE.h>
|
||||
#include <bits/types/struct_FILE.h>
|
||||
|
||||
void getRandomQuote(char quote[1024]);
|
||||
|
||||
void getQuote(int line, char quote[1024]);
|
||||
|
||||
#endif //QOTD_QUOTE_H
|
||||
44
server/server.c
Normal file
44
server/server.c
Normal file
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// Created by k0rb4k on 30/03/2021.
|
||||
//
|
||||
|
||||
#include "server.h"
|
||||
|
||||
void tcpServer() {
|
||||
int serverSocket, clientSocket;
|
||||
unsigned long addressLength;
|
||||
struct sockaddr_in serverAddress, clientAddress;
|
||||
|
||||
serverAddress.sin_family = AF_INET;
|
||||
serverAddress.sin_addr.s_addr = INADDR_ANY;
|
||||
serverAddress.sin_port = htons(17);
|
||||
|
||||
serverSocket = socket(serverAddress.sin_family, SOCK_STREAM, IPPROTO_IP);
|
||||
if (serverSocket == -1) {
|
||||
puts("socket not created");
|
||||
exit(1);
|
||||
}
|
||||
puts("socket created");
|
||||
|
||||
if (bind(serverSocket, (const struct sockaddr *) &serverAddress, sizeof serverAddress) < 0) {
|
||||
puts("bind failed");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
listen(serverSocket, 5);
|
||||
|
||||
addressLength = sizeof(struct sockaddr_in);
|
||||
clientSocket = accept(serverSocket, (struct sockaddr *) &clientAddress, (socklen_t *) addressLength);
|
||||
if (clientSocket < 0) {
|
||||
puts("client connection failed");
|
||||
exit(3);
|
||||
}
|
||||
puts("client connected");
|
||||
|
||||
write(clientSocket, "test", strlen("test"));
|
||||
// TODO: listen and respond
|
||||
}
|
||||
|
||||
void udpServer() {
|
||||
|
||||
}
|
||||
19
server/server.h
Normal file
19
server/server.h
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by k0rb4k on 30/03/2021.
|
||||
//
|
||||
|
||||
#ifndef QOTD_SERVER_H
|
||||
#define QOTD_SERVER_H
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void tcpServer();
|
||||
|
||||
void udpServer();
|
||||
|
||||
#endif //QOTD_SERVER_H
|
||||
Reference in New Issue
Block a user