Compare commits
5 Commits
master
...
dev-multit
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e23582ac62 | ||
|
|
252493b4ea | ||
|
|
cea7131f77 | ||
|
|
ee2eecf6a2 | ||
|
|
195d4352a0 |
@ -3,4 +3,9 @@ project(qotd C)
|
|||||||
|
|
||||||
set(CMAKE_C_STANDARD 99)
|
set(CMAKE_C_STANDARD 99)
|
||||||
|
|
||||||
add_executable(qotd main.c quote.c quote.h server.c server.h config.h)
|
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
|
||||||
|
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
|
||||||
|
find_package(Threads REQUIRED)
|
||||||
|
|
||||||
|
add_executable(qotd main.c quote.c quote.h server.c server.h config.h)
|
||||||
|
target_link_libraries(qotd Threads::Threads)
|
||||||
4
config.h
4
config.h
@ -5,8 +5,8 @@
|
|||||||
#ifndef QOTD_CONFIG_H
|
#ifndef QOTD_CONFIG_H
|
||||||
#define QOTD_CONFIG_H
|
#define QOTD_CONFIG_H
|
||||||
|
|
||||||
#define QUOTES_FILE "./quotes.txt"
|
#define QUOTES_FILE "./qotd.txt"
|
||||||
#define MESSAGE_STRING_LENGTH 2048
|
#define MESSAGE_STRING_LENGTH 2048
|
||||||
#define DEFAULT_LISTEN_PORT 17 // should be 17 to be RFC compliant
|
#define DEFAULT_LISTEN_PORT 7878 // should be 17 to be RFC compliant
|
||||||
|
|
||||||
#endif //QOTD_CONFIG_H
|
#endif //QOTD_CONFIG_H
|
||||||
|
|||||||
2
main.c
2
main.c
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
// TODO: shell args
|
// TODO: shell args
|
||||||
server(1, 1);
|
server();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
2
quote.c
2
quote.c
@ -5,6 +5,8 @@
|
|||||||
#include "quote.h"
|
#include "quote.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
//TODO: error handling
|
||||||
|
|
||||||
void getRandomQuote(char quote[2048]) {
|
void getRandomQuote(char quote[2048]) {
|
||||||
int lineCount;
|
int lineCount;
|
||||||
long line;
|
long line;
|
||||||
|
|||||||
146
server.c
146
server.c
@ -6,122 +6,128 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
//TODO: object-like C
|
//TODO: object-like C
|
||||||
|
//TODO: refactoring, cleanup
|
||||||
|
|
||||||
extern int errno;
|
extern int errno;
|
||||||
|
|
||||||
_Noreturn void udpListen(int serverSocket, struct sockaddr * clientAddress, unsigned long addressLength) {
|
_Noreturn void udpListen(void *args[]) {
|
||||||
char clientMessage[MESSAGE_STRING_LENGTH], quote[MESSAGE_STRING_LENGTH];
|
struct sockinfo *server = (struct sockinfo *) args[0];
|
||||||
|
struct sockinfo *client = (struct sockinfo *) args[1];
|
||||||
|
char quote[MESSAGE_STRING_LENGTH] = "", clientMessage[MESSAGE_STRING_LENGTH] = "";
|
||||||
|
|
||||||
|
// create args
|
||||||
|
server->handle = socket(server->address->sa_family, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
|
if (server->handle == -1) {
|
||||||
|
perror("socket not created");
|
||||||
|
pthread_exit((void *) 1);
|
||||||
|
}
|
||||||
|
puts("socket created");
|
||||||
|
|
||||||
|
// bind args
|
||||||
|
if (bind(server->handle, server->address, server->addressLength) < 0) {
|
||||||
|
perror("bind failed");
|
||||||
|
pthread_exit((void *) 2);
|
||||||
|
}
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
// receive data
|
// receive data
|
||||||
|
if (recvfrom(server->handle, clientMessage, MESSAGE_STRING_LENGTH, 0, client->address,
|
||||||
if (recvfrom(serverSocket, clientMessage, MESSAGE_STRING_LENGTH, 0, clientAddress, (socklen_t *) &addressLength) >= 0) {
|
&client->addressLength) >= 0) {
|
||||||
printf("received: %s\n", clientMessage);
|
printf("received: %s\n", clientMessage);
|
||||||
|
|
||||||
// send data
|
// send data
|
||||||
getRandomQuote(quote);
|
getRandomQuote(quote);
|
||||||
printf("sending: %s\n", quote);
|
printf("sending: %s\n", quote);
|
||||||
if (sendto(serverSocket, quote, strlen(quote), 0, clientAddress, addressLength) == -1) {
|
if (sendto(server->handle, quote, (long) strlen(quote), 0, client->address, client->addressLength) == -1) {
|
||||||
perror("udp: sendto");
|
perror("[UDP: SENDTO]");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
perror("udp: recvfrom");
|
perror("[UDP: RECVFROM]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void tcpListen(int serverSocket, struct sockaddr * clientAddress, unsigned long addressLength) {
|
void tcpListen(void *args[]) {
|
||||||
int clientSocket;
|
struct sockinfo *server = (struct sockinfo *) args[0];
|
||||||
|
struct sockinfo *client = (struct sockinfo *) args[1];
|
||||||
char quote[MESSAGE_STRING_LENGTH] = "", clientMessage[MESSAGE_STRING_LENGTH] = "";
|
char quote[MESSAGE_STRING_LENGTH] = "", clientMessage[MESSAGE_STRING_LENGTH] = "";
|
||||||
|
|
||||||
listen(serverSocket, 5);
|
// create args
|
||||||
|
server->handle = socket(server->address->sa_family, SOCK_STREAM, IPPROTO_TCP);
|
||||||
|
if (server->handle == -1) {
|
||||||
|
perror("socket not created");
|
||||||
|
pthread_exit((void *) 1);
|
||||||
|
}
|
||||||
|
puts("socket created");
|
||||||
|
|
||||||
|
// bind args
|
||||||
|
if (bind(server->handle, server->address, server->addressLength) < 0) {
|
||||||
|
perror("bind failed");
|
||||||
|
pthread_exit((void *) 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
listen(server->handle, 5);
|
||||||
puts("server listening");
|
puts("server listening");
|
||||||
|
|
||||||
// accept connections
|
// accept connections
|
||||||
//TODO: t h r e a d s
|
//TODO: t h r e a d s
|
||||||
while ((clientSocket = accept(serverSocket, clientAddress, (socklen_t *) &addressLength)) >= 0) {
|
while ((client->handle = accept(server->handle, client->address, &client->addressLength)) >= 0) {
|
||||||
puts("client connected");
|
puts("client connected");
|
||||||
|
|
||||||
// receive data
|
// receive data
|
||||||
if (recv(clientSocket, clientMessage, MESSAGE_STRING_LENGTH, 0) >= 0) {
|
if (recv(client->handle, clientMessage, MESSAGE_STRING_LENGTH, 0) >= 0) {
|
||||||
printf("client says: %s\n", clientMessage);
|
printf("client says: %s\n", clientMessage);
|
||||||
|
|
||||||
// send data
|
// send data
|
||||||
getRandomQuote(quote);
|
getRandomQuote(quote);
|
||||||
printf("responding: \"%s\"\n", quote);
|
printf("responding: \"%s\"\n", quote);
|
||||||
if (send(clientSocket, quote, strlen(quote), 0) == -1) {
|
if (send(client->handle, quote, (long) strlen(quote), 0) == -1) {
|
||||||
perror("tcp: send");
|
perror("[TCP: SEND]");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
perror("tcp: recv");
|
perror("[TCP: RECV]");
|
||||||
}
|
}
|
||||||
|
|
||||||
close(clientSocket);
|
close(client->handle);
|
||||||
|
|
||||||
puts("closed connection");
|
puts("closed connection");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clientSocket < 0) {
|
if (client->handle < 0) {
|
||||||
puts("client connection failed");
|
perror("client connection failed");
|
||||||
exit(3);
|
pthread_exit((void *) 3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void server(int tcp, int ipv6) {
|
void server() {
|
||||||
int serverSocket, socketType, socketProtocol;
|
struct sockinfo client;
|
||||||
unsigned long serverAddressLength, clientAddressLength;
|
struct sockinfo udp, tcp;
|
||||||
struct sockaddr *serverAddress, *clientAddress;
|
struct sockaddr_in6 serverAddress, clientAddress;
|
||||||
struct sockaddr_in serverAddress4, clientAddress4;
|
|
||||||
struct sockaddr_in6 serverAddress6, clientAddress6;
|
|
||||||
|
|
||||||
if (ipv6) {
|
serverAddress.sin6_family = AF_INET6;
|
||||||
serverAddress6.sin6_family = AF_INET6;
|
serverAddress.sin6_addr = in6addr_any;
|
||||||
serverAddress6.sin6_addr = in6addr_any;
|
serverAddress.sin6_port = htons(DEFAULT_LISTEN_PORT);
|
||||||
serverAddress6.sin6_port = htons(DEFAULT_LISTEN_PORT);
|
|
||||||
serverAddress = (struct sockaddr *) &serverAddress6;
|
|
||||||
serverAddressLength = sizeof serverAddress6;
|
|
||||||
|
|
||||||
clientAddress6.sin6_family = AF_INET6;
|
tcp.address = (struct sockaddr *) &serverAddress;
|
||||||
clientAddress = (struct sockaddr *) &clientAddress6;
|
udp.address = (struct sockaddr *) &serverAddress;
|
||||||
clientAddressLength = sizeof clientAddress6;
|
tcp.addressLength = sizeof serverAddress;
|
||||||
} else {
|
udp.addressLength = sizeof serverAddress;
|
||||||
serverAddress4.sin_family = AF_INET;
|
|
||||||
serverAddress4.sin_addr.s_addr = INADDR_ANY;
|
|
||||||
serverAddress4.sin_port = htons(DEFAULT_LISTEN_PORT);
|
|
||||||
serverAddress = (struct sockaddr *) &serverAddress4;
|
|
||||||
serverAddressLength = sizeof serverAddress4;
|
|
||||||
|
|
||||||
clientAddress4.sin_family = AF_INET;
|
clientAddress.sin6_family = AF_INET6;
|
||||||
clientAddress = (struct sockaddr *) &clientAddress4;
|
client.address = (struct sockaddr *) &clientAddress;
|
||||||
clientAddressLength = sizeof clientAddress4;
|
client.addressLength = sizeof clientAddress;
|
||||||
}
|
|
||||||
|
|
||||||
if (tcp) {
|
void *tcpArgs[] = {&tcp, &client};
|
||||||
socketType = SOCK_STREAM;
|
void *udpArgs[] = {&udp, &client};
|
||||||
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
|
// TODO: threads to run both at the same time
|
||||||
if (tcp) {
|
pthread_t tcpHandle, udpHandle;
|
||||||
tcpListen(serverSocket, clientAddress, clientAddressLength);
|
pthread_attr_t attr;
|
||||||
} else {
|
pthread_attr_init(&attr);
|
||||||
udpListen(serverSocket, clientAddress, clientAddressLength);
|
pthread_create(&tcpHandle, &attr, (void *(*)(void *)) tcpListen, tcpArgs);
|
||||||
}
|
pthread_create(&udpHandle, &attr, (void *(*)(void *)) udpListen, udpArgs);
|
||||||
|
|
||||||
|
void *tcpReturn, *udpReturn;
|
||||||
|
pthread_join(tcpHandle, tcpReturn);
|
||||||
|
pthread_join(udpHandle, udpReturn);
|
||||||
}
|
}
|
||||||
|
|||||||
13
server.h
13
server.h
@ -14,13 +14,20 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
#include "quote.h"
|
#include "quote.h"
|
||||||
|
|
||||||
void server(int tcp, int ipv6);
|
struct sockinfo {
|
||||||
|
int handle;
|
||||||
|
struct sockaddr * address;
|
||||||
|
socklen_t addressLength;
|
||||||
|
};
|
||||||
|
|
||||||
void tcpListen(int serverSocket, struct sockaddr *clientAddress, unsigned long addressLength);
|
void server();
|
||||||
|
|
||||||
_Noreturn void udpListen(int serverSocket, struct sockaddr *clientAddress, unsigned long addressLength);
|
void tcpListen(void * args[]);
|
||||||
|
|
||||||
|
_Noreturn void udpListen(void * args[]);
|
||||||
|
|
||||||
#endif //QOTD_SERVER_H
|
#endif //QOTD_SERVER_H
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user