forked from mirrors/qotd
removed client code and executable, use ncat or similar program to communicate with the server
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
#include "server.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// TODO: shell args
|
||||
server(1, 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
//
|
||||
// Created by k0rb4k on 30/03/2021.
|
||||
//
|
||||
|
||||
#include "quote.h"
|
||||
|
||||
void getRandomQuote(char quote[2048]) {
|
||||
int lineCount;
|
||||
long line;
|
||||
|
||||
if ((lineCount = getFileLineCount(QUOTES_FILE)) == -1) {
|
||||
puts("file does not exist");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
line = random() % lineCount;
|
||||
|
||||
getQuote(line, quote);
|
||||
}
|
||||
|
||||
void getQuote(long line, char quote[2048]) {
|
||||
FILE* list;
|
||||
|
||||
list = fopen(QUOTES_FILE, "r");
|
||||
|
||||
/*skip lines*/
|
||||
while (line > 0) {
|
||||
if (fgetc(list) == '\n') {
|
||||
line--;
|
||||
}
|
||||
}
|
||||
fgets(quote, 2048, list);
|
||||
quote[strlen(quote) - 1] = '\0';
|
||||
|
||||
fclose(list);
|
||||
}
|
||||
|
||||
int getFileLineCount(char *filename) {
|
||||
char c;
|
||||
int lineCount = 0;
|
||||
FILE* quotesFile = fopen(filename, "r");
|
||||
|
||||
if (quotesFile == NULL) {
|
||||
printf("no quote file found at \"%s\"\n", filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
while ((c = (char) fgetc(quotesFile)) != EOF) {
|
||||
if (c == '\n') {
|
||||
lineCount++;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(quotesFile);
|
||||
|
||||
return lineCount;
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
//
|
||||
// Created by k0rb4k on 30/03/2021.
|
||||
//
|
||||
|
||||
#ifndef QOTD_QUOTE_H
|
||||
#define QOTD_QUOTE_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <bits/types/FILE.h>
|
||||
#include <bits/types/struct_FILE.h>
|
||||
|
||||
#define QUOTES_FILE "./quotes.txt"
|
||||
|
||||
void getRandomQuote(char quote[2048]);
|
||||
|
||||
void getQuote(long line, char quote[2048]);
|
||||
|
||||
int getFileLineCount(char* filename);
|
||||
|
||||
#endif //QOTD_QUOTE_H
|
||||
-121
@@ -1,121 +0,0 @@
|
||||
//
|
||||
// Created by k0rb4k on 30/03/2021.
|
||||
//
|
||||
|
||||
#include "server.h"
|
||||
|
||||
//TODO: object-like C
|
||||
|
||||
extern int errno;
|
||||
|
||||
_Noreturn void udpListen(int serverSocket, struct sockaddr * clientAddress, unsigned long addressLength) {
|
||||
int recvLen;
|
||||
char clientMessage[MESSAGE_STRING_LENGTH], quote[MESSAGE_STRING_LENGTH];
|
||||
|
||||
for (;;) {
|
||||
// receive data
|
||||
recvLen = recvfrom(serverSocket, clientMessage, MESSAGE_STRING_LENGTH, 0, clientAddress, (socklen_t *) &addressLength);
|
||||
|
||||
perror("recvfrom");
|
||||
if (recvLen >= 0) {
|
||||
printf("client says: %s\n", clientMessage);
|
||||
|
||||
// send data
|
||||
getRandomQuote(quote);
|
||||
printf("responding: `%s`\n", quote);
|
||||
sendto(serverSocket, quote, strlen(quote), 0, clientAddress, addressLength);
|
||||
perror("sendto");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void tcpListen(int serverSocket, struct sockaddr * clientAddress, unsigned long addressLength) {
|
||||
int clientSocket;
|
||||
char quote[MESSAGE_STRING_LENGTH] = "", clientMessage[MESSAGE_STRING_LENGTH] = "";
|
||||
|
||||
listen(serverSocket, 5);
|
||||
puts("server listening");
|
||||
|
||||
// accept connections
|
||||
//TODO: t h r e a d s
|
||||
while ((clientSocket = accept(serverSocket, clientAddress, (socklen_t *) &addressLength)) >=
|
||||
0) {
|
||||
puts("client connected");
|
||||
|
||||
// receive data
|
||||
while (recv(clientSocket, clientMessage, MESSAGE_STRING_LENGTH, 0) >= 0) {
|
||||
printf("client says: %s\n", clientMessage);
|
||||
|
||||
// send data
|
||||
getRandomQuote(quote);
|
||||
printf("responding: \"%s\"\n", quote);
|
||||
send(clientSocket, quote, strlen(quote), 0);
|
||||
}
|
||||
|
||||
puts("client disconnected");
|
||||
}
|
||||
|
||||
if (clientSocket < 0) {
|
||||
puts("client connection failed");
|
||||
exit(3);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
serverAddress6.sin6_family = AF_INET6;
|
||||
serverAddress6.sin6_addr = in6addr_any;
|
||||
serverAddress6.sin6_port = htons(17);
|
||||
serverAddress = (struct sockaddr *) &serverAddress6;
|
||||
serverAddressLength = sizeof serverAddress6;
|
||||
|
||||
clientAddress6.sin6_family = AF_INET6;
|
||||
clientAddress = (struct sockaddr *) &clientAddress6;
|
||||
clientAddressLength = sizeof clientAddress6;
|
||||
} else {
|
||||
serverAddress4.sin_family = AF_INET;
|
||||
serverAddress4.sin_addr.s_addr = INADDR_ANY;
|
||||
serverAddress4.sin_port = htons(17);
|
||||
serverAddress = (struct sockaddr *) &serverAddress4;
|
||||
serverAddressLength = sizeof serverAddress4;
|
||||
|
||||
clientAddress4.sin_family = AF_INET;
|
||||
clientAddress = (struct sockaddr *) &clientAddress4;
|
||||
clientAddressLength = sizeof clientAddress4;
|
||||
}
|
||||
|
||||
if (tcp) {
|
||||
socketType = SOCK_STREAM;
|
||||
socketProtocol = IPPROTO_TCP;
|
||||
} else {
|
||||
socketType = SOCK_DGRAM;
|
||||
socketProtocol = IPPROTO_UDP;
|
||||
}
|
||||
|
||||
// create sock
|
||||
serverSocket = socket(serverAddress->sa_family, socketType, socketProtocol);
|
||||
if (serverSocket == -1) {
|
||||
puts("socket not created");
|
||||
exit(1);
|
||||
}
|
||||
puts("socket created");
|
||||
|
||||
// bind sock
|
||||
if (bind(serverSocket, serverAddress, serverAddressLength) < 0) {
|
||||
puts("bind failed");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
// TODO: threads to run both at the same time
|
||||
if (tcp) {
|
||||
tcpListen(serverSocket, clientAddress, clientAddressLength);
|
||||
} else {
|
||||
udpListen(serverSocket, clientAddress, clientAddressLength);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
//
|
||||
// Created by k0rb4k on 30/03/2021.
|
||||
//
|
||||
|
||||
#ifndef QOTD_SERVER_H
|
||||
#define QOTD_SERVER_H
|
||||
|
||||
#include <sys/socket.h>
|
||||
//#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "quote.h"
|
||||
|
||||
#define MESSAGE_STRING_LENGTH 2048
|
||||
|
||||
void server(int tcp, int ipv6);
|
||||
|
||||
void tcpServer();
|
||||
|
||||
_Noreturn void udpServer();
|
||||
|
||||
#endif //QOTD_SERVER_H
|
||||
Reference in New Issue
Block a user