cleanup
This commit is contained in:
parent
252493b4ea
commit
e23582ac62
4
main.c
4
main.c
@ -1,10 +1,8 @@
|
||||
#include "server.h"
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// TODO: shell args
|
||||
server(1, 0);
|
||||
server();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
95
server.c
95
server.c
@ -10,34 +10,35 @@
|
||||
|
||||
extern int errno;
|
||||
|
||||
_Noreturn void udpListen(void * args[]) {
|
||||
struct sockinfo * server = (struct sockinfo *) args[0];
|
||||
struct sockinfo * client = (struct sockinfo *) args[1];
|
||||
_Noreturn void udpListen(void *args[]) {
|
||||
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");
|
||||
exit(1);
|
||||
pthread_exit((void *) 1);
|
||||
}
|
||||
puts("socket created");
|
||||
|
||||
// bind args
|
||||
if (bind(server->handle, server->address, server->addressLength) < 0) {
|
||||
perror("bind failed");
|
||||
exit(2);
|
||||
pthread_exit((void *) 2);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
// receive data
|
||||
if (recvfrom(server->handle, clientMessage, MESSAGE_STRING_LENGTH, IPV6_V6ONLY, client->address, &client->addressLength) >= 0) {
|
||||
if (recvfrom(server->handle, clientMessage, MESSAGE_STRING_LENGTH, 0, client->address,
|
||||
&client->addressLength) >= 0) {
|
||||
printf("received: %s\n", clientMessage);
|
||||
|
||||
// send data
|
||||
getRandomQuote(quote);
|
||||
printf("sending: %s\n", quote);
|
||||
if (sendto(server->handle, quote, (long) strlen(quote), IPV6_V6ONLY, client->address, client->addressLength) == -1) {
|
||||
if (sendto(server->handle, quote, (long) strlen(quote), 0, client->address, client->addressLength) == -1) {
|
||||
perror("[UDP: SENDTO]");
|
||||
}
|
||||
} else {
|
||||
@ -46,23 +47,23 @@ _Noreturn void udpListen(void * args[]) {
|
||||
}
|
||||
}
|
||||
|
||||
void tcpListen(void * args[]) {
|
||||
struct sockinfo * server = (struct sockinfo *) args[0];
|
||||
struct sockinfo * client = (struct sockinfo *) args[1];
|
||||
void tcpListen(void *args[]) {
|
||||
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_STREAM, IPPROTO_TCP);
|
||||
if (server->handle == -1) {
|
||||
perror("socket not created");
|
||||
exit(1);
|
||||
pthread_exit((void *) 1);
|
||||
}
|
||||
puts("socket created");
|
||||
|
||||
// bind args
|
||||
if (bind(server->handle, server->address, server->addressLength) < 0) {
|
||||
perror("bind failed");
|
||||
exit(2);
|
||||
pthread_exit((void *) 2);
|
||||
}
|
||||
|
||||
listen(server->handle, 5);
|
||||
@ -74,13 +75,13 @@ void tcpListen(void * args[]) {
|
||||
puts("client connected");
|
||||
|
||||
// receive data
|
||||
if (recv(client->handle, clientMessage, MESSAGE_STRING_LENGTH, IPV6_V6ONLY) >= 0) {
|
||||
if (recv(client->handle, clientMessage, MESSAGE_STRING_LENGTH, 0) >= 0) {
|
||||
printf("client says: %s\n", clientMessage);
|
||||
|
||||
// send data
|
||||
getRandomQuote(quote);
|
||||
printf("responding: \"%s\"\n", quote);
|
||||
if (send(client->handle, quote, (long) strlen(quote), IPV6_V6ONLY) == -1) {
|
||||
if (send(client->handle, quote, (long) strlen(quote), 0) == -1) {
|
||||
perror("[TCP: SEND]");
|
||||
}
|
||||
} else {
|
||||
@ -94,59 +95,39 @@ void tcpListen(void * args[]) {
|
||||
|
||||
if (client->handle < 0) {
|
||||
perror("client connection failed");
|
||||
exit(3);
|
||||
pthread_exit((void *) 3);
|
||||
}
|
||||
}
|
||||
|
||||
void server(int tcp, int ipv6) {
|
||||
struct sockinfo client4, client6;
|
||||
struct sockinfo udp4, udp6, tcp4, tcp6;
|
||||
struct sockaddr_in serverAddress4, clientAddress4;
|
||||
struct sockaddr_in6 serverAddress6, clientAddress6;
|
||||
void server() {
|
||||
struct sockinfo client;
|
||||
struct sockinfo udp, tcp;
|
||||
struct sockaddr_in6 serverAddress, clientAddress;
|
||||
|
||||
serverAddress6.sin6_family = AF_INET6;
|
||||
serverAddress6.sin6_addr = in6addr_any;
|
||||
serverAddress6.sin6_port = htons(DEFAULT_LISTEN_PORT);
|
||||
serverAddress.sin6_family = AF_INET6;
|
||||
serverAddress.sin6_addr = in6addr_any;
|
||||
serverAddress.sin6_port = htons(DEFAULT_LISTEN_PORT);
|
||||
|
||||
tcp6.address = (struct sockaddr *) &serverAddress6;
|
||||
udp6.address = (struct sockaddr *) &serverAddress6;
|
||||
tcp6.addressLength = sizeof serverAddress6;
|
||||
udp6.addressLength = sizeof serverAddress6;
|
||||
tcp.address = (struct sockaddr *) &serverAddress;
|
||||
udp.address = (struct sockaddr *) &serverAddress;
|
||||
tcp.addressLength = sizeof serverAddress;
|
||||
udp.addressLength = sizeof serverAddress;
|
||||
|
||||
clientAddress6.sin6_family = AF_INET6;
|
||||
client6.address = (struct sockaddr *) &clientAddress6;
|
||||
client6.addressLength = sizeof clientAddress6;
|
||||
clientAddress.sin6_family = AF_INET6;
|
||||
client.address = (struct sockaddr *) &clientAddress;
|
||||
client.addressLength = sizeof clientAddress;
|
||||
|
||||
serverAddress4.sin_family = AF_INET;
|
||||
serverAddress4.sin_addr.s_addr = INADDR_ANY;
|
||||
serverAddress4.sin_port = htons(DEFAULT_LISTEN_PORT);
|
||||
|
||||
tcp4.address = (struct sockaddr *) &serverAddress4;
|
||||
udp4.address = (struct sockaddr *) &serverAddress4;
|
||||
tcp4.addressLength = sizeof serverAddress4;
|
||||
udp4.addressLength = sizeof serverAddress4;
|
||||
|
||||
clientAddress4.sin_family = AF_INET;
|
||||
client4.address = (struct sockaddr *) &clientAddress4;
|
||||
client4.addressLength = sizeof clientAddress4;
|
||||
|
||||
void * tcp6Args[] = {&tcp6, &client6};
|
||||
void * udp6Args[] = {&udp6, &client6};
|
||||
void * tcp4Args[] = {&tcp4, &client4};
|
||||
void * udp4Args[] = {&udp4, &client4};
|
||||
void *tcpArgs[] = {&tcp, &client};
|
||||
void *udpArgs[] = {&udp, &client};
|
||||
|
||||
// TODO: threads to run both at the same time
|
||||
pthread_t tcp6Handle, udp6Handle, tcp4Handle, udp4Handle;
|
||||
pthread_t tcpHandle, udpHandle;
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
pthread_create(&tcp6Handle, &attr, (void *(*)(void *)) tcpListen, tcp6Args);
|
||||
pthread_create(&udp6Handle, &attr, (void *(*)(void *)) udpListen, udp6Args);
|
||||
// pthread_create(&tcp4Handle, &attr, (void *(*)(void *)) tcpListen, tcp4Args);
|
||||
// pthread_create(&udp4Handle, &attr, (void *(*)(void *)) udpListen, udp4Args);
|
||||
pthread_create(&tcpHandle, &attr, (void *(*)(void *)) tcpListen, tcpArgs);
|
||||
pthread_create(&udpHandle, &attr, (void *(*)(void *)) udpListen, udpArgs);
|
||||
|
||||
void * tcp6Return, * udp6Return, * tcp4Return, * udp4Return;
|
||||
pthread_join(tcp6Handle, tcp6Return);
|
||||
pthread_join(udp6Handle, udp6Return);
|
||||
// pthread_join(tcp4Handle, tcp4Return);
|
||||
// pthread_join(udp4Handle, udp4Return);
|
||||
void *tcpReturn, *udpReturn;
|
||||
pthread_join(tcpHandle, tcpReturn);
|
||||
pthread_join(udpHandle, udpReturn);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user