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