Commit c1e2aee1 authored by Maia Jejelava's avatar Maia Jejelava
Browse files

Merge remote-tracking branch 'origin/master'

# Conflicts:
#	echo_server_IPv6.c
parents 792686ca 9fe29c26
......@@ -35,83 +35,49 @@ TCP Based Character Generator Service
#include "Socket.h"
//structure for arguments for the thread_handler
struct Args {
int client_fd;
};
//thread handler that basically just takes client_fd as a parameter
void *thread_handler(void *args) {
char *buf;
//allocate memmory init it with 0
buf = calloc(1, BUFFER_SIZE);
//sets the client_fd to the one that was given by the parameter
int client_fd = ((struct Args *) args)->client_fd;
uint8_t off = 0;
int client_fd = *(int*) args;
free(args);
//sets. An fd_set is a fixed size buffer.
fd_set read, write;
//FD_ZERO() clears a set
FD_ZERO(&read);
FD_ZERO(&write);
pthread_detach(pthread_self());
int offset = 0;
// allocate memmory init it with 0
uint8_t *buf = (uint8_t *) calloc(1, 74 * sizeof(uint8_t));
if (buf == NULL)
perror("error on calloc");
while (1) {
//put client in read write
FD_SET(client_fd, &read);
FD_SET(client_fd, &write);
//TODO select_wrapper
//pick one read or write connection, double check if the connection is still ready
select(client_fd + 1, &read, &write, 0, 0);
//If the client is part of the read set, then receive
if (FD_ISSET(client_fd, &read)) {
//when all is read
if (recv_wrapper(client_fd, buf, sizeof(buf), 0) == 0) {
printf("Client fd %i closed a connection in thread id: %lu\n", client_fd, (unsigned long int) pthread_self());
close_wrapper(client_fd);
break;
}
}
//is client_fd in write set ?
if (FD_ISSET(client_fd, &write)) {
//rfc specifies this kind of output
//rfc wants 72char in a line
for (int i = 0; i < 72; i++) {
//generate the chars
buf[i] = ((i + offset) % (126 - 32)) + 32;
}
//stop after 72 lines
buf[BUFFER_SIZE - 1] = '\n';
for (uint8_t i = 0; i < 72; i++)
buf[i] = ((i + off) % (126 - 32)) + 32;
//increase the offset and make sure it doesn't go over 95
offset++;
offset = offset % 95;
buf[72] = 13;
buf[73] = 10;
off= (off+1) % 95;
send_wrapper(client_fd, buf, strlen(buf), 0);
sleep(1);
}
if (write(client_fd, buf, sizeof(buf)) == -1)
break;
usleep(100000);
}
close_wrapper(client_fd);
free(buf);
return NULL;
}
int main(int argc, char **argv) {
int server_fd, client_fd;
struct sockaddr_in server_addr;
char buf[BUFFER_SIZE];
int server_fd, *client_fd;
struct sockaddr_in server_addr, *client_addr;
pthread_t thread_id;
//(1)The creation of the TCP socket is identical to the client code
server_fd = socket_wrapper(AF_INET, SOCK_STREAM, 0);
memset(&server_addr, 0, sizeof(server_addr)); //socket address structure
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(
INADDR_ANY); //specify the IP address allows the server to accept a client //connection on any interface,
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); //specify the IP address allows the server to accept a client //connection on any interface,
server_addr.sin_port = htons(strtol(argv[1], NULL, 10));
bind_wrapper(server_fd, (const struct sockaddr *) &server_addr, (socklen_t) sizeof(server_addr));
......@@ -132,16 +98,12 @@ int main(int argc, char **argv) {
while (1) {
//accept connection
client_fd = accept_wrapper(fd, (struct sockaddr *) &client, (socklen_t *) &client_size);
//struct for the thread_arguments
thread_arguments->client_fd = client_fd;
//cerate thread
pthread_create(&thread_id, NULL, thread_handler, (void *) thread_arguments);
client_fd = calloc(1, sizeof(int));
*client_fd = accept_wrapper(server_fd, NULL, NULL);
//cerate thread
pthread_create(&thread_id, NULL, &thread_handler, client_fd);
printf("Client fd %i is connected to thread id: %lu\n", client_fd, (unsigned long int) thread_id);
}
close_wrapper(fd);
close_wrapper(server_fd);
exit(0);
}
#include <stdio.h>
#include <stdlib.h>
#include "Socket.h"
int main(int argc, char **argv) {
int fd, done = 0, stdin_open = 0;
fd_set rset;
struct sockaddr_in server_addr;
ssize_t len;
char buf[BUFFER_SIZE];
fd = socket_wrapper(AF_INET, SOCK_STREAM, IPPROTO_TCP);
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
#ifdef HAVE_SIN_LEN
server_addr.sin_len = sizeof(struct sockaddr_in);
#endif
if ((server_addr.sin_addr.s_addr = (in_addr_t)inet_addr(argv[1])) == INADDR_NONE) {
fprintf(stderr, "Invalid address\n");
}
server_addr.sin_port = htons(strtol(argv[2], NULL, 10));
connect_wrapper(fd, (const struct sockaddr *)&server_addr, (socklen_t)sizeof(server_addr));
FD_ZERO(&rset);
//Schleife um den Secelct call (schläft meistens)
while(!done) {
//Wo für intressiere ich mich beim select? das wird in readset aufgenommen
//kann ich was von stin oder TCP Verbindung lesen?
FD_SET(fd, &rset); //TCP Verbindung
if (stdin_open == 0)
FD_SET(0, &rset); //stdin
//Größter fd +1 , Readset intessiert uns
select(fd + 1, &rset, NULL, NULL, NULL);
//0 ist lesbar, also wenn von stdin was lesbar ist
if (FD_ISSET(0, &rset)) {
//lese von stdin
len = read(0, (void *) buf, sizeof(buf));
//solange lesen bis stin zuende ist (Strg+D )
//hier Shoutdown?? um das lesen zu stoppen?
if (len==0) {
//lese stdin
shutdown_wrapper(fd, SHUT_WR);
stdin_open = 1;
}
else
//sende
send_wrapper(fd, (void *) buf, (size_t) len, 0);
}
//wenn was von der TCP Verbindung kommmt
if (FD_ISSET(fd, &rset)) {
//wenn ein FIN geschicht wird, kommt ein recv mit Null zurück
len = recv_wrapper(fd, (void *) buf, sizeof(buf), 0);
//wenn das Fin gekommen ist
if (len == 0)
done = 1;
else
printf("%.*s", (int)len, buf);
// write soll genommen werden, buggt aber
}
}
close_wrapper(fd);
return (0);
}
/*TODO Wie geht man aus dieser Schleife raus
wenn Strg +D also stdin geschlossen, dann ein FIN senden Shoutdown "shout br"
aber man muss weiter bereit sein zu lesen
erst wenn read /recive 0 liefern rausgehen
wenn von stin eine 0 kommt als return Wert, nicht weiter auf stin betrachen, sonst Endlosschleife(select sagt dann immer stin ist immer noch lesbar)
wichtig wenn man char.gen.server hat der so lange redet bis fin kommt
*/
......@@ -8,67 +8,42 @@
#include "Socket.h"
//called for each connection
void *handle_connection(void *arg) {
ssize_t read_len, send_len;
char buf[BUFFER_SIZE];
int client_fd = *(int*)arg;
free(arg);
do {
//read from client
read_len = recv_wrapper(client_fd, (void *) buf, sizeof(buf), 0);
send_len = 0;
//send back to client
while (send_len < read_len) {
send_len += send_wrapper(client_fd, (void *) (buf+send_len), read_len - send_len, 0);
}
} while(read_len > 0);
close_wrapper(client_fd);
return 0;
}
void *thread_handler(void *arg);
int main (int argc, char **argv) {
int server_fd;
int* thread_fd;
pthread_t client_thread;
int server_fd, on = 1, off = 0, *client_fd;
pthread_t thread_id;
struct sockaddr_in6 server_addr;
//create an IPv6 socket
server_fd = socket_wrapper(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
memset(&server_addr, 0, sizeof(server_addr));
server_fd = socket_wrapper(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
server_addr.sin6_family = AF_INET6;
server_addr.sin6_addr = in6addr_any;
server_addr.sin6_port = htons(strtol(argv[1], NULL, 10));
#ifdef HAVE_SIN_LEN
server_addr.sin_len = sizeof(struct sockaddr_in);
#endif
server_addr.sin6_addr = in6addr_any;
server_addr.sin6_port = htons(strtol(argv[1], NULL, 10));
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int));
//accept Ipv4 and Ipv6
int *option = 0;
setsockopt(server_fd, IPPROTO_IPV6, IPV6_V6ONLY, &option, sizeof(int));
setsockopt(server_fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(int));
//bind the socket
bind_wrapper(server_fd, (const struct sockaddr *)&server_addr, (socklen_t)sizeof(server_addr));
//wait for incumming connection
//wait for incomming connection
listen_wrapper(server_fd,1);
while(1) {
// Speicher allozieren für client_fd
thread_fd = malloc(sizeof(int));
if(thread_fd == NULL) {
continue;
}
*thread_fd = accept_wrapper(server_fd, NULL, NULL);
client_fd = calloc(1, sizeof(int));
*client_fd = accept_wrapper(server_fd, NULL, NULL);
//create a thread for each client
//for each is handle_connection called
//(void *) thread_fd) argument for called funktion
pthread_create(&client_thread, NULL, &handle_connection, (void *) thread_fd);
//(void *) client_fd) argument for called funktion
pthread_create(&thread_id, NULL, &thread_handler, (void *) client_fd);
//destroy thread after performance and free memmory
pthread_detach(client_thread);
}
//close fd
if (close_wrapper(server_fd) < 0)
......@@ -76,3 +51,25 @@ int main (int argc, char **argv) {
return (0);
}
void *thread_handler(void *arg) {
ssize_t read_len, send_len;
char buf[BUFFER_SIZE];
int client_fd = *(int*)arg;
free(arg);
pthread_detach(pthread_self());
do {
//read from client
read_len = recv_wrapper(client_fd, (void *) buf, sizeof(buf), 0);
send_len = 0;
//send back to client
while (send_len < read_len) {
send_len += send_wrapper(client_fd, (void *) (buf+send_len), read_len - send_len, 0);
}
} while(read_len > 0);
close_wrapper(client_fd);
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment