Commit 2551123e authored by Maia Jejelava's avatar Maia Jejelava
Browse files

Aufgabe 3 and 4 implemented.

parent 080bdafa
bin_PROGRAMS = client daytime_server_TCP discard_server_TCP echo_server
bin_PROGRAMS = client chargen_server_TCP get_addresses echo_server_IPv6
client_SOURCES = client.c Socket.c
daytime_server_TCP_SOURCES = daytime_server_TCP.c Socket.c
discard_server_TCP_SOURCES = discard_server_TCP.c Socket.c
echo_server_SOURCES = echo_server.c Socket.c
echo_server_LDFLAGS = -l pthread
chargen_server_TCP_SOURCES = discard_server_TCP.c Socket.c
get_addresses_SOURCES = get_addresses.c Socket.c
echo_server_IPv6_SOURCES = echo_server.c Socket.c
echo_server_IPv6_LDFLAGS = -l pthread
......@@ -24,6 +24,7 @@
#include <sys/time.h>
#include <pthread.h>
#include <stdio.h>
#include <netdb.h>
int socket_wrapper(int family, int type, int protocol);
int bind_wrapper(int fd, const struct sockaddr *addr, socklen_t addrlen);
......
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include "Socket.h"
#include <unistd.h>
int main(int argc, char **argv) {
int fd, done = 0, stdin_open = 0;
int fd, 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);
struct addrinfo *result, hints, *entry;
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");
bzero(&hints, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_UNSPEC;
hints.ai_protocol = IPPROTO_TCP;
int ret;
if ((ret = getaddrinfo(argv[1], argv[2], &hints, &result))) {
fprintf(stderr, "Getaddrinfo: %s\n", gai_strerror(ret));
return 2;
}
server_addr.sin_port = htons(strtol(argv[2], NULL, 10));
connect_wrapper(fd, (const struct sockaddr *)&server_addr, (socklen_t)sizeof(server_addr));
for (entry = result; entry != NULL; entry = entry->ai_next) {
fd = socket_wrapper(entry->ai_family, entry->ai_socktype, entry->ai_protocol);
if (fd == -1) {
continue;
}
if (connect_wrapper(fd, entry->ai_addr, entry->ai_addrlen) == -1) {
close_wrapper(fd);
continue;
}
break;
}
FD_ZERO(&rset);
//Schleife um den Secelct call (schläft meistens)
while(!done) {
while(1) {
if (!stdin_open)
FD_SET(fileno(stdin), &rset);
//Wo für intressiere ich mich beim select? das wird in readset aufgenommen
//kann ich was von stin oder TCP Verbindung lesen?
FD_SET(0, &rset); //stdin
//kann ich was von stin oder TCP Verbindung lesen?
FD_SET(fd, &rset); //TCP Verbindung
//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) && stdin_open == 0) {
if (FD_ISSET(fileno(stdin), &rset)) {
//lese von stdin
len = read(0, (void *) buf, sizeof(buf));
len = read(fileno(stdin), (void *) buf, sizeof(buf));
//solange lesen bis stin zuende ist (Strg+D )
//hier Shoutdown?? um das lesen zu stoppen?
if (len==0) {
......@@ -55,13 +72,11 @@ int main(int argc, char **argv) {
len = recv_wrapper(fd, (void *) buf, sizeof(buf), 0);
//wenn das Fin gekommen ist
if (len == 0)
done = 1;
break;
else
printf("%.*s", (int)len, buf);
}
}
close_wrapper(fd);
return (0);
}
......@@ -6,7 +6,7 @@ AM_INIT_AUTOMAKE
AC_CANONICAL_HOST
CFLAGS="$CFLAGS -Wall -std=c99 -pedantic"
CFLAGS="$CFLAGS -Wall -std=gnu99 -pedantic"
case $host_os in
solaris*)
......
......@@ -27,15 +27,17 @@ int main (int argc, char **argv) {
pthread_t client_thread;
struct sockaddr_in server_addr;
server_fd = socket_wrapper(AF_INET, SOCK_STREAM, IPPROTO_TCP);
server_fd = socket_wrapper(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_family = AF_INET6;
#ifdef HAVE_SIN_LEN
server_addr.sin_len = sizeof(struct sockaddr_in);
#endif
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(strtol(argv[1], NULL, 10));
server_addr.sin6_addr = in6addr_any;
server_addr.sin6_port = htons(strtol(argv[1], NULL, 10));
int *option = 0;
setsockopt(server_fd, IPPROTO_IPV6, IPV6_V6ONLY, &option, sizeof(int));
bind_wrapper(server_fd, (const struct sockaddr *)&server_addr, (socklen_t)sizeof(server_addr));
listen_wrapper(server_fd,1);
......@@ -53,6 +55,8 @@ int main (int argc, char **argv) {
pthread_create(&client_thread, NULL, &handle_connection, (void *) thread_fd);
pthread_detach(client_thread);
}
if (close_wrapper(server_fd) < 0)
perror("close");
return (0);
}
\ No newline at end of file
}
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